Skip to main content

JNI in a module

4 replies [Last post]
shavnir
Offline
Joined: 2008-05-06
Points: 0

For a module I'm working on I need to load some JNI code. This will either require me to be able to pass in an argument to java.library.path or to know the absolute path of the module at runtime.

Are either of these possible without hacking apart wonderland too much?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kaplanj
Offline
Joined: 2004-07-13
Points: 0

Handling native code in modules is tricky, which is why we haven't really dealt with it. As you point out, it is difficult to load the native code itself. Another issue is that once you load native code, you can't unload or reload it, so it means you can't support different versions if you are jumping from server to server.

If you do want to load native code, there are two approaches (we use a little of both):

1. add the code to the Wonderland native jar that is downloaded by Java web start. To do this, I think you just put your native libraries in the wonderland/core/ext//native directory. These should get automatically packaged into the appropriate native jar in wonderland/web/webstart/build/webstart//wonderland_native.jar. These native libraries will be available to anyone who launches your site's Java Web Start download. The problem is that they won't be installed for clients that load a different Java Web Start and then jump to your server.

2. add the native libraries to your module .jar file, then unpack them to a temporary directory at run time. You can then use System.loadLibrary() with an absolute path to load the library. The good news is that this will work with federation (although only one version can ever be loaded, so don't plan to update too much :-) ). The bad news is you have to deal with a lot of the platform-specific issues yourself.

shavnir
Offline
Joined: 2008-05-06
Points: 0

I've already decided on option number two. Trust me once the thesis is done I'm willing to code freeze ;) Very willing.

Just as a quick refresher when you're calling getResourceAsStream to, say for example, pull a dll file out of a jar do you call it with the package path as well "/edu/bsu/...../filename.dll", do you call it with just the filename.dll and hope it searches right or do you call it with the path assuming it starts in the package that is calling it?

kaplanj
Offline
Joined: 2004-07-13
Points: 0

The second. For example, if you are in class org.jdesktop.wonderland.foo.Foo, and you call:

[code]
InputStream is = getClass().getResourceAsStream("resources/foo.properties");
[/code]
That will load the file org/jdesktop/wonderland/foo/resources/foo.properties from the jar file that Foo.class was loaded from.

shavnir
Offline
Joined: 2008-05-06
Points: 0

Okay, I've figured out an alternate method. Will the classloader that Wonderland loads plugins with let me call getResourceAsStream as normal or am I just screwing something up :\