How to Add Native Library to "Java.Library.Path" with Eclipse Launch (Instead of Overriding It)

How to add native library to java.library.path with Eclipse launch (instead of overriding it)

Had forgotten this issue... I was actually asking with Eclipse, sorry for not stating that originally.
And the answer seems to be too simple (at least with 3.5; probably with older versions also):

Java run configuration's Arguments : VM arguments:

-Djava.library.path="${workspace_loc:project}\lib;${env_var:PATH}"

Must not forget the quotation marks, otherwise there are problems with spaces in PATH.

How to set the java.library.path from Eclipse

Don't mess with the library path! Eclipse builds it itself!

Instead, go into the library settings for your projects and, for each jar/etc that requires a native library, expand it in the Libraries tab. In the tree view there, each library has items for source/javadoc and native library locations.

Specifically: select Project, right click -> Properties / Java Build Path / Libraries tab, select a .jar, expand it, select Native library location, click Edit, folder chooser dialog will appear)

Messing with the library path on the command line should be your last ditch effort, because you might break something that is already properly set by eclipse.

Native library location

Adding new paths for native libraries at runtime in Java

[This solution don't work with Java 10+]

It seems impossible without little hacking (i.e. accessing private fields of the ClassLoader class)

This blog provide 2 ways of doing it.

For the record, here is the short version.

Option 1: fully replace java.library.path with the new value)

public static void setLibraryPath(String path) throws Exception {
System.setProperty("java.library.path", path);

//set sys_paths to null so that java.library.path will be reevalueted next time it is needed
final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);
}

Option 2: add a new path to the current java.library.path

/**
* Adds the specified path to the java library path
*
* @param pathToAdd the path to add
* @throws Exception
*/
public static void addLibraryPath(String pathToAdd) throws Exception{
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);

//get array of paths
final String[] paths = (String[])usrPathsField.get(null);

//check if the path to add is already present
for(String path : paths) {
if(path.equals(pathToAdd)) {
return;
}
}

//add the new path
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length-1] = pathToAdd;
usrPathsField.set(null, newPaths);
}

How to edit eclipse.ini to prepend existing java.library.path

After some more research and a few discussions with friends and colleagues, it seems that automatically extending the default java.library.path in eclipse.ini isn't possible. It seems you need to hard-code it.
I believe, however, just overwriting the old configuration is an undesirable way to go; one should at least extend the existing path. The following is a short description on how it was done on my system.

First, before modifying eclipse.ini (i.e. without the -Djava.library.path=... line), run eclipse, in the menu click "Help"->"About Eclipse", click "Installation Details", click on the tab "Configuration", wait until "retrieving system information..." disappears, scroll down to the second page and copy the line containing the java library path (in my case it is java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib).

Second, close eclipse, open eclipse.ini, and append a line like -Djava.library.path=<added path>:<previous path>. In my case this boils down to the line -Djava.library.path=/usr/lib/x86_64-linux-gnu/jni:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib.

Third, start eclipse again and check in the "Configurations" tab if the new path was properly set.

After this procedure my SVN plugin is working as desired. One hast to remember though to check the java.library.path settings manually in case there are relevant changes to the system.

Java Library Path in Eclipse - Add dll library to multiple jars at once?

In your case, manually editing the project's .classpath file might gain you a bit of efficiency. When I set the location of a JAR's native library, the following entry is added to my .classpath file:

<classpathentry exported="true" kind="lib" path="path/to/library.jar" sourcepath="/path/to/src">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="path/to/native/library.dll"/>
</attributes>
</classpathentry>

You might manually set up the first JAR (via the Eclipse UI), close Eclipse and copy/paste the generated classpath entries in .classpath with the appropriate modifications, and then restart Eclipse.



Related Topics



Leave a reply



Submit