开发者

Simple Classpath question using with jars

开发者 https://www.devze.com 2023-01-16 11:11 出处:网络
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

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.


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.

Simple Classpath question using with jars


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消