Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Attach API: changing java.library.path dynamically

Tags:

java

windows

jvm

When using the com.sun.tools.attach API on my Windows machine, I get the following error when making a call to

VirtualMachine.list()

java.lang.UnsatisfiedLinkError: no attach in java.library.path

The reason is the missing attach.dll. The attach.dll is located in $JRE/bin/. When starting my Java program with -Djava.library.path=[Directory to the attach.dll] everything works out without error output.

Now, I don't want to add every Java program start this ugly JVM parameter. Therefore my questions are:

  1. Is my machine not configured right and the $JRE/bin/ should be in the library path anyway?

  2. If not, how can I add the path dynamically? System.setProperties("java.library.path",StringOfThePathToTheAttach.dll); does not work out. The library path is changed, but the error apperas anyway. Has this something to do with SecurityManager or JVM start up?

like image 713
Konrad Reiche Avatar asked Apr 06 '11 11:04

Konrad Reiche


People also ask

What is Djava library path?

java. library. path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by a project.

What is Native library location in eclipse?

Go to Project properties->Java Build Path->Source. You'll find a list of source-folders. Each entry under the the Source tab has Native library locations.


2 Answers

Your System.setProperty("java.library.path", StringOfThePathToTheAttach.dll); should work. My guess is that you're calling it too late. In other words, there is an attempt to access the DLL prior to you setting the property.

Can you output the current value for java.library.path after the property is set in code and again before the offending method call?

i.e. If you see "Before attach.dll call" output prior to seeing "After setting property", you know where your problem is.

Edit:

A better way to point to native libraries is to use System.load(StringOfThePathToTheAttach.dll) - again, before the offending line of code.

like image 26
Teddy Yueh Avatar answered Oct 27 '22 05:10

Teddy Yueh


Just found a link that might answer your question

"The java.library.path is read only once when the JVM starts up. If you change this property using System.setProperty, it won't make any difference."

http://fahdshariff.blogspot.jp/2011/08/changing-java-library-path-at-runtime.html

like image 62
nabito Avatar answered Oct 27 '22 07:10

nabito