Reputation: 5609
I have a toy program that is called Test.class. It accesses a class in a jar called myjar.jar. That jar is in my CLASSPATH variable set as part of my Windows environment. When I type echo %CLASSPATH%, I see C:\myclasses\myjar.jar. When I execute my program java Test
it runs fine.
But if I package the code as a jar and try running
java -jar Test.jar
It ca It can't find my classpath. I know this has a simple solution.
Can you please help me.
Upvotes: 2
Views: 212
Reputation: 61793
First off I would not bother with this stuff myself anymore(I used too) and let my IDE(Netbeans/Eclipse) figure this stuff for you out. BTW I already hope you are using an IDE because it makes programming in Java that much more fun.
Next I would advice to learn a build tool like maven.
Upvotes: 0
Reputation: 1109570
When -jar
(or -cp
or -classpath
) argument is been used, then the %CLASSPATH%
will be ignored. Instead, the Class-Path
entry in JAR's /META-INF/MANIFEST.MF
file will be used. You'd like to put the JAR-relative path to the other JAR in there. E.g.
Class-Path: myjar.jar
The above example expects the myjar.jar
to be in same folder as the JAR file you'd like to execute.
An alternative is to package the 3rd party JAR inside your JAR file. In for example Eclipse you can do this.
Upvotes: 3