Java Native Access

Only available on StudyMode
  • Topic: Subroutine, Java, C++
  • Pages : 2 (266 words )
  • Download(s) : 52
  • Published : May 19, 2013
Open Document
Text Preview
Java Native Access

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

/** Simple example of native library declaration and usage. */
public class HelloWorld {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
(Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
void printf(String format, Object... args);
}

public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i = 0; i < args.length; i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}

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

/** Simple example of native C POSIX library declaration and usage. */ public class exampleOfPOSIX {
public interface POSIX extends Library {
public int chmod(String filename, int mode);
public int chown(String filename, int user, int group); public int rename(String oldpath, String newpath);
public int kill(int pid, int signal);
public int link(String oldpath, String newpath);
public int mkdir(String path, int mode);
public int rmdir(String path);
}

public static void main(String[] args) {
POSIX posix = (POSIX) Native.loadLibrary("c", POSIX.class); posix.mkdir("/tmp/newdir", 0777);
posix.rename("/tmp/newdir","/tmp/renamedir");
}
}

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

/** Simple example of Windows native library declaration and usage. */ public class BeepExample {
public...
tracking img