• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JNI and Jar

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

These days my project need to use JNI to call MS sound dll to make some words sound. The project works well until I put them all in a jar file. My question is does there exist anyway to put a .dll file into a jar, and let the System.load(String) or System.loadLibrary(String) methods to find it??

I've tried lots of ways tonight, and nearly to be crazy now. There're some methods I've used:
1. Use class.getResource to get the URL and from the URL's toString method get the "file:/Dir" string. Pass this string to loadLibrary method.
Failed from the outside, not to talk about jar.
URL s= PlayerHelper.class.getResource("com_c3_gisk_PlayerHelper.dll");
String str = s.toString();
System.load(str);

2. Delete the "str" in the method one, replace the "file:/" string to an empty string "". Still use load(str). This time it works well in the outside. But when put it in the jar file, failed.

3. Use System.loadLibrary("com_c3_gisk_PlayerHelper") to use the relative location. Success in the outside, but failed in the Jar file.

The main reason is if we use the getResource() in the jar file, the URL's toString method will return the string like the following:
"jar:file:/C:/test.jar!/com_c3_gisk_PlayerHelper.dll".

Help me!
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading the API doc:
Loads a code file with the specified filename from the local file system

A file containing native code is loaded from the local file system from a place where library files are conventionally obtained.
 
Lawrance Miao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Norm Radder :
When the dll file is in the Jar file, what is the dll file's conventional path of the local operation system? My platform is WinXP
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A dll has to be in the one of the following places a) folders java.library.path, b) the folder the application is running from, c) current folder.

If you are running an applet under IE,
a = folders listed in PATH environment variable
b = the folder where IEExplore.exe is running
c = I don't know

If you want your applet to access a dll, you have to either put the dll in Windows-system, OR install the dll somewhere on the local system and add the path to the dll in the environment variable

If you are running an application
a = folders listed in -Djava.library.path command line option. I think this defaults to the PATH environment variable
b = jre-bin folder
c = folder where the user starts the application from

So, for your application to access a dll, you have to either put the dll in Windows-system, OR install the dll somewhere on the local system and add the path to the dll in the environment variable, OR put the dll in jre-bin OR always start your application from a specific folder, and place the dll in that folder

AFAIK, there is no way you can load a dll from the jar.

I don't know if you can rig up your own classloader that loads the dll from inside the jar, and places it in jre-bin or something. I have never tried it myself. You might want to look into extending the ClassLoader if you are really interested

Here's a thread in Java forums that you might find interesting
 
Lawrance Miao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try a whole afternoon, but fail.
reply
    Bookmark Topic Watch Topic
  • New Topic