System.loadLibrary problem

M

Matthew Zimmer

Hey all,
I have a strange issue I'm trying to figure out and could really use
some help. I've tried everything I can think of and am not having much
luck. Here's the scenario: I've created a dll called Win32Registry.dll
to allow me to add keys to the windows registry. I have a class called
Win32RegKey in package win32regkey. I've created a jar called
Win32Registry.jar with my Win32RegKey class in it. Win32RegKey has the
call System.loadLibrary("Win32Registry") in it.

Now, when I run a test program from test.Test that creates a key
everything works fine. However, the dll and the jar file are in the
same directory that I'm running from and the classpath includes "." in it.

Now, when I take the same dll and jar file and drop them into a lib
directory in my main project I start having some problems. Oh, I'm
using JBuilder8 by the way. I include Win32Registry.jar in my project
path, but can't include Win32Registry.dll in it since JB8 doesn't let
you do that. Anyway, when I try to access the same code that I did
through test.Test I get the following exception:
java.lang.UnsatisfiedLinkError: no Win32Registry in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1403)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:832)
at win32regkey.Win32RegKey.<clinit>(Win32RegKey.java:82)
at com.mypackage.MyClass.doStuff(MyClass.java:26)
...

Now, if I put the dll file in my working directory things are okay. But
here's the part that really bothers me about that. Previously I was
using jst.jar and jst.dll which contain registry code that unfortunately
does not allow you to add a new key (which is why I had to write my
own). I'm assuming that somewhere in jst.jar the code must call
System.loadLibrary("jst"). Now, I have the exact same setup with jst in
that the jst.jar file is in my project path, and the jst.dll is not but
they are both in my lib directory. Everything works fine with jst.

My question is this: am I doing something wrong with either the
creation of my dll or my jar file (I'm using the standard jar -cvf ...
stuff for the jar file and gcc for the dll) or is the System.loadLibrary
call wrong. Can you pass a relative path to that...maybe something like
"System.loadLibrary("./Win32Registry")"? I've tried that particular
example but maybe there is some other way to do it that I just don't know.

Anyway, that's the problem that I'm facing right now. I'd sure
appreciate any help that somebody out there might be able to give me.
This is really starting to frustrate me. Thanks for any help.

Matthew
 
G

Gordon Beaton

[ short story made long ]

Put dll files in your PATH if you want the JVM to find them, or change
your PATH so that it includes the dll directory.

The argument to System.loadLibrary() is strictly speaking not even a
filename, so it isn't meaningful to talk about using any kind of path
in the argument, relative or otherwise.

If you want your dll in a non-PATH location, use System.load()
instead, and specify an absolute path and complete dll filename.

/gordon
 
T

Thomas Fritsch

Hi again!

Matthew said:
...
My question is this: am I doing something wrong with either the
creation of my dll or my jar file (I'm using the standard jar -cvf ...
stuff for the jar file and gcc for the dll) or is the
System.loadLibrary call wrong.

No, it is correct.
Can you pass a relative path to that...maybe something like
"System.loadLibrary("./Win32Registry")"?

I think this is not allowed. And if it works in spite of this, I would
consider it a bug of the Java VM ;-)

You can find out where your Java VM (or to be more precise: its
System.loadLibrary method) actually searches for DLLs by
System.out.println(System.getProperty("java.library.path"));
somewhere in your Java code.
You will get a long list containing: the Windows system directory, the
Windows directory, the directory of your executable (JAVA.EXE), ".", and
all directories given by the PATH variable.
Choose one of these directories where to put your DLL.

Greetings
Thomas
 
M

Matthew Zimmer

If you want your dll in a non-PATH location, use System.load()
instead, and specify an absolute path and complete dll filename.

/gordon

Thanks...that did the trick. I was able to walk my classpath searching
for the file and then used System.load to actually bring it in. Not the
prettiest thing in the world, but it works! :)

Matthew
 
M

Matthew Zimmer

Thomas said:
Hi again!




No, it is correct.



I think this is not allowed. And if it works in spite of this, I would
consider it a bug of the Java VM ;-)



You can find out where your Java VM (or to be more precise: its
System.loadLibrary method) actually searches for DLLs by
System.out.println(System.getProperty("java.library.path"));
somewhere in your Java code.
You will get a long list containing: the Windows system directory, the
Windows directory, the directory of your executable (JAVA.EXE), ".", and
all directories given by the PATH variable.
Choose one of these directories where to put your DLL.

Greetings
Thomas

Thanks for the idea. I used this to find the classpath stuff and then
just walked that path seeing if the dll was anywhere in there. If it
was, I used System.load() to get it. If not, I used
System.loadLibrary() and hope. :) It wasn't pretty, but it worked and
since I'll have a little control over where the dll goes on the end
machines (ie it'll always be in the classpath) I should be good to go.

Matthew
 
D

DreamCoder

How do you do your dll?
I have this question because in other post , they told me that i only
can load a dll using JNI?
 
M

Matthew Zimmer

DreamCoder said:
How do you do your dll?
I have this question because in other post , they told me that i only
can load a dll using JNI?

I use JNI. :) It's a pain to work with (especially for someone like me
who hasn't done a lot of c/c++ coding) but it's really the only way to
access window's functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,520
Members
44,996
Latest member
rainocode

Latest Threads

Top