Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a target library available to my Java app?

Tags:

java

jna

Using JNA, the documentation says:

Make your target library available to your Java program. There are two ways to do this: The preferred method is to set the jna.library.path system property to the path to your target library. This property is similar to java.library.path but only applies to libraries loaded by JNA.

What does this actually mean? How do I set the jna.library.path system property? My app needs to reference Kernel32.dll

Thanks

like image 492
MalcomTucker Avatar asked Mar 03 '10 10:03

MalcomTucker


People also ask

Where does Java look for libraries?

It's very common that a Java program depends on one or more external libraries (jar files). Use the flag -classpath(or -cp) to tell the compiler where to look for external libraries (by default, the compiler is looking in bootstrap classpath and in CLASSPATH environment variable).

What is shared library in Java?

Shared libraries are compiled for the actual ISA of the mobile so they do not need any VM to run. They are added into to apk file as they are.


1 Answers

You can set system properties by using the parameter "-D" when you invoke the Java Virtual Machine on the command line:

java -Djna.library.path=<path to your library> MainClass

You can also set this programmatically in your code at your applications's startup when it has been read from e.g. a config file:

System.setProperty("jna.library.path", <path to your library>);

I haven't used JNA myself, so I don't know if it is actually too late for the JVM when you set the value in code. In that case, go with the first option.

like image 128
Robert Petermeier Avatar answered Nov 15 '22 16:11

Robert Petermeier