System.load / System.loadLibrary

Discussion in 'Java' started by Philipp Kraus, Oct 9, 2011.

  1. 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
     
    Philipp Kraus, Oct 9, 2011
    #1
    1. Advertising

  2. Philipp Kraus

    Roedy Green Guest

    On Sun, 9 Oct 2011 12:45:04 +0200, Philipp Kraus
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >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.
     
    Roedy Green, Oct 9, 2011
    #2
    1. Advertising

  3. Philipp Kraus

    Roedy Green Guest

    On Sun, 9 Oct 2011 12:45:04 +0200, Philipp Kraus
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >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.
     
    Roedy Green, Oct 9, 2011
    #3
  4. On 2011-10-09 13:22:39 +0200, Roedy Green said:

    > On Sun, 9 Oct 2011 12:45:04 +0200, Philipp Kraus
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >>
    >> 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.


    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
     
    Philipp Kraus, Oct 9, 2011
    #4
  5. Philipp Kraus

    Roedy Green Guest

    On Sun, 9 Oct 2011 14:09:54 +0200, Philipp Kraus
    <> wrote, quoted or indirectly quoted
    someone who said :

    >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.
     
    Roedy Green, Oct 9, 2011
    #5
  6. Philipp Kraus

    markspace Guest

    On 10/9/2011 7:54 AM, Roedy Green wrote:
    > 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.
     
    markspace, Oct 9, 2011
    #6
  7. On 2011-10-09 18:32:08 +0200, markspace said:

    > On 10/9/2011 7:54 AM, Roedy Green wrote:
    >> 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.


    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
     
    Philipp Kraus, Oct 9, 2011
    #7
  8. Philipp Kraus

    markspace Guest

    On 10/9/2011 9:41 AM, Philipp Kraus wrote:

    > 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.
     
    markspace, Oct 9, 2011
    #8
  9. In article <j6rtvg$g6t$>,
    Philipp Kraus <> wrote:

    > 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>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Oct 10, 2011
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Zimmer

    System.loadLibrary problem

    Matthew Zimmer, Dec 16, 2003, in forum: Java
    Replies:
    6
    Views:
    16,240
    Matthew Zimmer
    Dec 17, 2003
  2. Roedy Green

    System.loadLibrary

    Roedy Green, Aug 1, 2005, in forum: Java
    Replies:
    0
    Views:
    757
    Roedy Green
    Aug 1, 2005
  3. Replies:
    1
    Views:
    520
    Gordon Beaton
    Feb 6, 2006
  4. Replies:
    2
    Views:
    847
    Nigel Wade
    Nov 24, 2006
  5. Replies:
    4
    Views:
    1,848
    Gordon Beaton
    Mar 28, 2007
Loading...

Share This Page