75

What is the difference between System.load() and System.loadLibrary() in java?

I want to load a library but I don't want to add the path to environment variables. Will any one of these help?

javaMan
  • 6,352
  • 20
  • 67
  • 94

1 Answers1

91

The difference is there in the API documentation. System.loadLibrary(String libname) lets you load from the default path -- The Java library path.

The other System.load(String filename) lets you load it from an absolute path, which you must specify as your filename.

If you don't want to mess with you java.library.path environment variable, you should use System.load()

vitaut
  • 49,672
  • 25
  • 199
  • 336
Kal
  • 24,724
  • 7
  • 65
  • 65
  • 11
    Right now I am haing a library which I am loading using system.load("path to library"); And I am adding this path in the system environmebt variables. If I dont add then I am getting unsatisfied link error. Is there any way I can skip this adding this library path in system environment variable. – javaMan Aug 10 '11 at 19:43
  • The drawback of loadLibrary() is so that it returns the confusing error message "java.lang.UnsatisfiedLinkError: myLib (Not found in java.library.path)" even if it is found, but could not be loaded due to other reasons. In such case calling load()/loadLibrary() with the absolute path shows additional output like "rtld: Symbol myFunc was referenced ... but a runtime definition was not found." which is hidden by loadLibrary() with the library basename as argument. That is the best way is to write own method which iterates through java.library.path and calls load() per item. – Alexander Samoylov May 10 '22 at 16:36