views:

36

answers:

2

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.

+2  A: 

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.

alt text

BalusC
A: 

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.

Alfred