Invoke Syscalls from Java

Exists a means to invoke syscalls straight from Java, or is it essential to first call an indigenous method?

2019-05-04 10:34:11
Source Share
Answers: 2

It is essential to make use of an indigenous method, or a collection that does so for you.

2019-05-08 02:28:11

You require to make use of an indigenous method, yet you do not require to implement it on your own. Java has a variant on JNI called JNA (Java Native Access), which allows you accessibility shared collections straight without requiring a JNI user interface twisted around them, so you can make use of that to user interface straight with glibc :

import com.sun.jna.Library;
import com.sun.jna.Native;

public class Test {
    public interface CStdLib extends Library {
        int syscall(int number, Object... args);

    public static void main(String[] args) {
        CStdLib c = (CStdLib)Native.loadLibrary("c", CStdLib.class);

        // WARNING: These syscall numbers are for x86 only
        System.out.println("PID: " + c.syscall(20));
        System.out.println("UID: " + c.syscall(24));
        System.out.println("GID: " + c.syscall(47));
        c.syscall(39, "/tmp/create-new-directory-here");
2019-05-07 22:59:38