0

I have a C++ library that I have compiled for OS X, Windows & Unix. For each one I have created a separate project in Eclipse and have exported them with the name convention: library-OS.jar.

Each jar basically contains:

library-OS.jar |

-> library.ext

What I was thinking was from my main Jar applet to load them by extracting them to a temp location and using system.loadlibrary.

Unfortunately I don't know how to get the resource.

I was trying to do it using Extract and load DLL from JAR but it is for relative paths and not online browser applets.

So my question is how to load the C library based on the OS of the client. I'm using JNI to talk to it and that one is in a separate JAR as well which loads the library. I was looking into JNLP but I don't know how to get the Jar name if I use JNLP. :/

EDIT:

public static void loadJarDll(String name) throws IOException {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
    byte[] buffer = new byte[1024];
    int read = -1;
    File temp = new File(new File(System.getProperty("java.io.tmpdir")), name);
    FileOutputStream fos = new FileOutputStream(temp);

    while((read = in.read(buffer)) != -1) {
        fos.write(buffer, 0, read);
    }
    fos.close();
    in.close();

    System.load(temp.getAbsolutePath());
}

This is the code I'm currently using. In order to load the library I pass the name variable which MUST contain the extension of the library. What I want to do is just request the library with it's generic name for each OS (like if it's foobar.dll for windows and foobar.so for unix to just call it foobar when loading). I want to load it like this because when I use JNLP I will only have one JAR containing the native library for that specific OS and platform.

Community
  • 1
  • 1
Marti Markov
  • 746
  • 6
  • 25
  • Are you aware that this operation will require your applet to be signed, and require all-permissions authorization from the user? –  Nov 09 '14 at 03:07
  • Yes, I am. I have no problem with that. I just don't know how to write my code for this. This is my first time use of JNLP and loading of native libraries from JARs. – Marti Markov Nov 09 '14 at 03:14
  • Basically my question is what should I write in order to load the native library which is stored in another JAR file? Also do I need to know the name of it (the JAR file) or is it when I request a file from a JAR using: ACWrapper.class.getResourceAsStream(libraryNameWithouExtension); it goes through all the downloaded JAR files and finds that file? – Marti Markov Nov 09 '14 at 03:18

1 Answers1

1

Java Web Start already has inbuilt support for partitioning and loading natives.

  1. Partitioning: Add the Jar to a system specific resources section. Each OS will only download the resources intended for it.
  2. Loading: Put the natives in the root of the Jar mentioned above, and it will be extracted and placed on the run-time class-path of the app.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Are you sure about 2? I ended up using System.Load(temp.getAbsolutePath()) but before that I checked the OS and saved the name of the library based on the system. That is the value I pass to loadJarDll. – Marti Markov Jan 28 '15 at 10:36
  • 1
    *"Are you sure about 2?"* Yes. But wait, let me reconsi.. ***YES!*** – Andrew Thompson Jan 28 '15 at 10:37
  • Well if that is the case then I can use System.Load() without the absolute path. But without it the library never gets loaded. So am I doing something wrong or what? I really got confused by the run-time class-path... :D – Marti Markov Jan 28 '15 at 10:40
  • *"So am I doing something wrong or what?"* Probably. If you wish to pursue it, ask another question and add the JNLP file content as well as the structure of the file system that contains the JNLP & Jars. – Andrew Thompson Jan 28 '15 at 11:53