System.load / System.loadLibrary

P

Philipp Kraus

Hello,

I use System.loadLibrary to load a shared DLL. This DLL requires other
depended DLLs. If all (under OSX DYLD_LIBRARY_PATH) is configurated the
System.loadLibrary loads my DLL and everything works fine. I have
tested the same code with the DYLD-Option is not set, so my Java code
should be break.
I would like to set the Path to the library like this code:
String[] l_libraries = { "boost_system", "mylib" };

try {
for( String i : l_libraries )
System.loadLibrary(i);
} catch (UnsatisfiedLinkError e_link1) {

File l_temp = new File(
System.getProperty("java.io.tmpdir") + "mytemp" );
if (!l_temp.isDirectory())
l_temp.mkdirs();


for( String i : l_libraries ) {
String l_lib = l_temp +
System.getProperty("file.separator") + System.mapLibraryName(i);
if
(System.getProperty("os.name").toLowerCase().indexOf("mac") >= 0)
l_lib = l_lib.substring(0,
l_lib.indexOf(".jnilib")) + ".dylib";

System.load(l_lib);
}
}

My target is: try to load the library and the depended from the default
system pathes, if not exists than load the libs from
a temporary path. mylib needs a depend boost library, so I load the
boost lib first, but I get the error:

Library not loaded: libboost_system.dylib Referenced from:
/mytemp/mylib.dylib Reason: image not found

Does anybody know a tip to load all libraries first, so the Java VM can
work with them from a "fixed" path?

Thanks

Phil
 
R

Roedy Green

Does anybody know a tip to load all libraries first, so the Java VM can
work with them from a "fixed" path?

I have better luck with load than loadLibrary. To me it felt like a
bug, but I have not seen bug reports so I suspect there is a mother of
a gotcha hidden in there not yet revealed.
..
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
R

Roedy Green

Does anybody know a tip to load all libraries first, so the Java VM can
work with them from a "fixed" path?

I am not sure what you are asking. You can just load them at the
start. See http://mindprod.com/jgloss/jni.html for a number of hints
on using load and loadLibrary.
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
P

Philipp Kraus

I am not sure what you are asking.

My problem is, that the System.load with a fully qualified filename
does not load my library and my depend library. If I use System.loadLibrary
with the OS path (unix LIBRARY_PATH, osx DYLD_LIBRARY_PATH)
the libs are loaded.

eg: mylib is linked again boost libraries, so I would like to load with
System.load
the boost libs first. I use the System.load command with a fqn path and
get the error
"image not found"


Thx

Phil
 
R

Roedy Green

My problem is, that the System.load with a fully qualified filename
does not load my library and my depend library. If I use System.loadLibrary
with the OS path (unix LIBRARY_PATH, osx DYLD_LIBRARY_PATH)
the libs are loaded.

eg: mylib is linked again boost libraries, so I would like to load with
System.load
the boost libs first. I use the System.load command with a fqn path and
get the error
"image not found"

Surely loadLibrary uses load itself, so if it can do it, surely you
can too. It has to be just a matter of feeding it the magic string.

Make sure you feed a fully qualified library name, with extension. Use
native platform punctuation for extra luck.

In Forth it would be trivial to insert so some code to monitor what
strings load was being fed. I have never figured out how to pull that
off in Java. If you had such a tool, I think you could crack this
problem quite quickly.
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
M

markspace

In Forth it would be trivial to insert so some code to monitor what
strings load was being fed. I have never figured out how to pull that
off in Java. If you had such a tool, I think you could crack this
problem quite quickly.


I seem to recall a tool on Unix system for monitoring the systems calls
made by a process. I guess you could use that on the JVM to see what
file calls where made by the JVM. Not sure how it works though.

It might be strace I'm thinking of. Not sure though, I'm no sysadmin.
 
P

Philipp Kraus

I seem to recall a tool on Unix system for monitoring the systems calls
made by a process. I guess you could use that on the JVM to see what
file calls where made by the JVM. Not sure how it works though.

It might be strace I'm thinking of. Not sure though, I'm no sysadmin.

If I do the call with System.load("/usr/lib/libmylib.so") under Linux it works,
under OSX with System.load("/usr/lib/libmylib.dylib") it does not. OSX
seems to have the
DYLD_LIBRARY_PATH environment variable.

Phil
 
M

markspace

If I do the call with System.load("/usr/lib/libmylib.so") under Linux it
works,
under OSX with System.load("/usr/lib/libmylib.dylib") it does not. OSX
seems to have the
DYLD_LIBRARY_PATH environment variable.


And I know even less about OSX than I do about other *nix. Sorry,
you're way past where I'm comfortable giving advice.
 
J

John B. Matthews

Philipp Kraus said:
Library not loaded: libboost_system.dylib Referenced from:
/mytemp/mylib.dylib Reason: image not found

Does anybody know a tip to load all libraries first, so the Java VM
can work with them from a "fixed" path?

The simplest approach is to let DYLD_LIBRARY_PATH include the full paths
to both mylib.dylib and libboost_system.dylib.

You can display the names and version numbers of the shared libraries
required by mylib.dylib using the -L option of otool. It may be
convenient to use the -install_name option of libtool when building
mylib.dylib. Both full and relative paths are supported. The -change
option of install_name_tool may be helpful, too. There's a related
example here:

<http://home.roadrunner.com/~jbmatthews/misc/shared.html>
 
Joined
Nov 18, 2016
Messages
4
Reaction score
0
Hello,

I am also facing similar issue .

Trying to migrate application from JBoss to tomcat8 .
We are using a .so native lib for a specific functionality .

Everytime this function is called we need to load .so file but it throws error :
Native code library failed to load. java.lang.UnsatisfiedLinkError:

I have tried multiple things
1. set -Djava.library.path = XX folder path of native lib explicitly through tomcat VM argument
2. copy xxx.so file to /usr/lib/ folder and give the -Djava.library.path= /usr/lib
3. Also tried installing tomcat-APR and tomcat-Native libraries
4. Tried to Load file using below line to check where native lib is getting loaded or not
System.loadLibrary(so filename );

5.Tried settign export LD_LIBRARY_PATH=/usr/local/apr/lib

But NO LUCK :(:(

Please someone help with this .
Why my xxx.so file is not getting loaded


-Varsha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top