Skip to main content

Classpath to dlls

8 replies [Last post]
tdanecito
Offline
Joined: 2005-10-10

Hi All,

I was wondering if there is a way to get the classpath to the dlls in the jars of a webstart application. I have some dlls that I have in a jar and even though I use loadLibaray() to load the dlls the dlls themselves need to have the class path to them passed as an option when creating an instance.
I currently work around this by reading the jar with the dlls and saving the dlls to a known location then passing that path to the java code to create an instance.
I would like to save disk space by pointing to the dlls unjared by webstart.

Any way to do that?

Thanks,
-Tony

Message was edited by: tdanecito

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tdanecito
Offline
Joined: 2005-10-10

Sorry I was up till 3:00AM doing some coding and forgot about my promise.
Below is the code that worked for me.

Regards,
-Tony

public String caculateNativeLibPathBySunJWS(String libName) throws IOException,IllegalStateException {
String libFolder = null;
ClassLoader cl = this.getClass().getClassLoader();
if (cl instanceof JNLPClassLoader) {
JNLPClassLoader jnlpCl = (JNLPClassLoader) cl;
String jdicLibURL = jnlpCl.findLibrary(libName);//get lib path by classloder
libFolder = (new File(jdicLibURL)).getParentFile().getCanonicalPath();
} else {
throw new IllegalStateException("unable to webstart native library path");
}
return libFolder;
}

tdanecito
Offline
Joined: 2005-10-10

Thanks for the help. I had defined the nativelib and it still did not work. I ended up finding some code several years old I had from another project that did work.

Regards,
-Tony

twalljava
Offline
Joined: 2004-07-26

Care to share? I'm always looking for improvements.

tdanecito
Offline
Joined: 2005-10-10

Sure. I will send later tonight after I get home. I found it in some old jdic source code here on java.net.

Regards,
-Tony

tdanecito
Offline
Joined: 2005-10-10

Hi,

I tried the code snippet but it throws and exception about findLibrary not being a method for ClassLoader. It is a protected method so not sure it is supposed to work.

Regards,
-Tony

twalljava
Offline
Joined: 2004-07-26

Yes, it's protected, which is why the "setAccessible" call is required. The code won't work unless you have sufficient privileges to call "setAccessible", but you need those privileges anyway in order to use the tag in web start in the first place.

Sign your code and re-test. Note that you also must actually have a defined, or the class loader won't have anything to look up.

twalljava
Offline
Joined: 2004-07-26

Here's a little snippet JNA uses (original idea submitted by robert engels, adapted from the JNLPAppletLauncher):

public String getWebStartLibraryPath(String libName) {
if (System.getProperty("javawebstart.version") == null)
return null;
try {
ClassLoader cl = getClass().getClassLoader();
Method m = ClassLoader.class.getMethod("findLibrary", new Class[] { String.class });
m.setAccessible(true);
String libpath = (String)m.invoke(cl, new Object[] { libName });
if (libpath != null) {
return new File(libpath).getParent();
}
String msg =
"Library '" + libName + "' was not found by class loader " + cl;
throw new IllegalArgumentException(msg);
}
catch (Exception e) {
return null;
}
}

twalljava
Offline
Joined: 2004-07-26

There's a typo in this code; it should call "getDeclaredMethod" instead of "getMethod". This is probably why it didn't work for you.