1

I am loading a library into my java code. I have put the library in the sytem 32 folder and I have also set the -Djava.library.path.

Earlier this code was running

try{


        System.loadLibrary("resources/TecJNI");

        System.out.println("JNI library loaded \n");
    }
    catch(UnsatisfiedLinkError e){
        System.out.println("Did not load library");
        e.printStackTrace();
    }

but since last week it is showing

java.lang.UnsatisfiedLinkError: no resources/TecJNI in java.library.path.

Is this some file permission issue for the dll that I am loading in the java code OR dll are using by some other application.

Also all other my running applications that were using & loading the same dll in different workspace are not running now.

Could anyone suggest me?

EDIT: I am using -

Djava.library.path="${workspace_loc}/org.syntec.ivb.application/resources;${env_var:PATH}"

in my eclipse vm arguments. I think it is using this.

code_fish
  • 3,381
  • 5
  • 47
  • 90
  • loadLibrary expects a library name, whereas resources/TecJNI not really seems like a valid lib name. is 'resource' a directory in the system32 directory? Also how exactly do you define java.library.path? – Naytzyrhc Aug 13 '13 at 08:05
  • I think I am using eclipse vm arguments settings. Not the system 32. I have updated the quesiton. – code_fish Aug 13 '13 at 08:15
  • well then can you try using System.loadLibrary("TecJNI")? – Naytzyrhc Aug 13 '13 at 08:19

3 Answers3

2

when comes to load libs in jvm, I like to copy the libs to a temp directory, then load them from the temp directory. here is the code:

private synchronized static void loadLib(String dllPath,String libName) throws IOException {
    String osArch = System.getProperty("os.arch").contains("64")?"_X64":"_X86";
    String systemType = System.getProperty("os.name");
    String libExtension = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll"
            : ".so";
    String libFullName = libName+osArch+ libExtension;
    String nativeTempDir = System.getProperty("java.io.tmpdir");

    InputStream in = null;
    BufferedInputStream reader = null;
    FileOutputStream writer = null;

    File extractedLibFile = new File(nativeTempDir + File.separator
            + libFullName);
    if (!extractedLibFile.exists()) {
        try {
            in = new FileInputStream(dllPath+ File.separator+
                    libFullName);
            reader = new BufferedInputStream(in);
            writer = new FileOutputStream(extractedLibFile);

            byte[] buffer = new byte[1024];

            while (reader.read(buffer) > 0) {
                writer.write(buffer);
                buffer = new byte[1024];
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                in.close();
            if (writer != null)
                writer.close();
        }
    }
    System.load(extractedLibFile.toString());
}
Winston
  • 1,202
  • 1
  • 13
  • 19
1

Why do you need the additional "resources"?

When using System.loadLibrary("resources/TecJNI"); you are looking for TecJNI.dll in a subfolder "resources" of the java.library.path. So if you put C:\windows\system32 on the library-path (which you wouldn't need since it's on the search-path by default) your library should be C:\windows\system32\resources\TecJNI.dll

piet.t
  • 11,718
  • 21
  • 43
  • 52
  • I am using -Djava.library.path="${workspace_loc}/org.syntec.ivb.application/resources;${env_var:PATH}" in my eclipse vm arguments. I think it is using this. – code_fish Aug 13 '13 at 08:11
1

System.loadLibrary expects library name, not a path. The path to the directory containg the library should be set in PATH (Windows) env variable or in -Djava.library.path

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • I am using -Djava.library.path="${workspace_loc}/org.syntec.ivb.application/resources;${env_var:PATH}" in my eclipse vm arguments. I think it is using this. – code_fish Aug 13 '13 at 08:13
  • thanks. now its running smoothly. But dont know same configuration was running fine. I was having dll in the resources folder and by using -Djava.library.path="${workspace_loc}/org.syntec.ivb.application/resources;${env‌​_var:PATH}" it was picking up the library. Now I am using -Djava.library.path="C:/Windows/System32" and System.loadLibrary("TecJNI") – code_fish Aug 13 '13 at 09:28
  • actually C:/Windows/System32 is supposed to be in Windows PATH, try without -Djava.library.path="C:/Windows/System32" – Evgeniy Dorofeev Aug 13 '13 at 09:40
  • 1
    @arun It is a poor practice to put application files into system folders. It should be quite easy to avoid. The Eclipse IDE that you are using uses native libraries for SWT and it runs fine after just being unzipped. – Tom Blodget Aug 13 '13 at 13:40