Linking JARs and DLL

N

Neo

I have been trying to link my JAR file with a third party JAR that
depends on a DLL for the native method calls. The program compiles but
when I execute it, I get a ClassNotFoundException

- Libraries:

c:\programs\lib.jar
c:\programs\lib.dll

- Application
c:\mydir\application.jar

application.jar uses lib.jar which itself uses lib.dll

-- application.java ---

//application.class is inside application.jar
//Class hello is inside the test package which is inside the lib.jar.
import test.hello;

public class application
{
public void run()
{
hello test = new hello();
}
}

When I run the program (application.jar), I use: java -classpath "c:
\programs\lib.jar" -Djava.library.path="c:\programs" -jar c:\mydir
\application.jar

However, I still get ClassNotFoundException
 
M

Mark Space

Neo said:
When I run the program (application.jar), I use: java -classpath "c:
\programs\lib.jar" -Djava.library.path="c:\programs" -jar c:\mydir
\application.jar

However, I still get ClassNotFoundException

The exception is says it can't find a class inside lib.jar, right? I'm
guessing because you didn't show us the exception, which might help a lot.

But anyway, -classpath and -jar don't mix. The -jar option will ignore
all classpath settings, including on the command line, so you can't use
-classpath with -jar.

You have to modify the manifest file in the application.jar to point to
the lib.jar.
 
R

Roedy Green

When I run the program (application.jar), I use: java -classpath "c:
\programs\lib.jar" -Djava.library.path="c:\programs" -jar c:\mydir
\application.jar

When you use a jar the command line the classpath comes from within
the jar.

The easiest way to handle it is to use Java Web Start. then the run
time will look after getting your dlls on the path.

see http://mindprod.com/jgloss/javawebstart.html
 
N

Neo

Mark,

See my comments below:

The exception is says it can't find a class inside lib.jar, right?  I'm
guessing because you didn't show us the exception, which might help a lot..

It cannot find test.hello.class (the class from the third party
lib.jar) at run time. It compiles fine. Something like: "Exception in
thread "main" java.lang.NoClassDefFoundError: test/hello at...Caused
by: java.lang.ClassNotFoundException: test.hello at
java.netURLClassLoader... said:
But anyway, -classpath and -jar don't mix.  The -jar option will ignore
all classpath settings, including on the command line, so you can't use
-classpath with -jar.

So, how can my jar file (application.jar) use lib.jar (which itself
depends on lib.dll)? It needs to use lib.jar because it contains the
class test.hello.class which it uses.
You have to modify the manifest file in the application.jar to point to
the lib.jar.

But the problem is that it doesn't know where each user has those
libraries (lib.jar, lib.dll) until it runs. How can I load dynamically
those libraries and use the classes from the libraries at run time?
Any help is appreciated...I'm completely lost here.
 
N

Neo

Roedy,

The problem is that my jar is going to be included with other jars
that run in windows XP. Those users don't want to use web start. (I
have no control over that.) Is there another way to do this?
 
M

Mark Space

Neo said:
But the problem is that it doesn't know where each user has those
libraries (lib.jar, lib.dll) until it runs. How can I load dynamically
those libraries and use the classes from the libraries at run time?
Any help is appreciated...I'm completely lost here.

It's a common problem. Personally, I think Sun should have provided a
solution.

In my opinion (and this is a guess) the default classloader works the
way it does because Sun wanted to be as conservative as possible.
There's no chance that you could accidentally use the wrong lib.jar with
the way the classloader works now.

One solution would be to modify the manifest file during installation.

Another solution would be to make your own classloader that does snarf
the value of the CLASSPATH environment variable and use that to search
for classes. Shouldn't be too hard, but I really am surprised Sun
doesn't provide a ready made one.

Here's an example:

<http://java.sun.com/docs/books/tutorial/deployment/jar/jarclassloader.html>


Other ideas are welcome, I'm no expert here.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top