java classpath in unix

雨燕双飞 提交于 2019-12-06 16:10:46

问题


I can run java in cygwin+windows using the following settings (the sw/jar directory has several jar files, and I pick the relevant one from the java command line):

CLASSPATH=.;C:\sw\java_6u35\lib\\*;C:\sw\jar\\*
java org.antlr.Tool Calc.g

But I am having the following problems when running in linux:

(1) I can't set a directory name in a classpath, the following line reports an error:

setenv CLASSPATH .:/sw/jdk1.6.0_35/lib/\*:/sw/jar/*

(2) when I run explictly with -jar option, I still get an error:

java -jar /sw/jar/antlr-3.4.jar org.antlr.Tool Calc.g
error(7):  cannot find or open file: org.antlr.Tool

However, the class does exist. When I do jar tf /sw/jar/antlr-3.4.jar, I get:

...
org/antlr/Tool.class

So my question is: (a) how do I specify in unix that my jar-directory is xxx that contains several jar files, and (2) how do I pick the relevant jar from this dir at runtime?


回答1:


To specify multiple jars in a directory, directly in the java command, use this

java -cp "/sw/jar/*" org.antlr.Tool Calc.g

This will include all the jars

If you want to set the classpath in Unix/Linux systems, use this

export CLASSPATH=/sw/jar/a.jar:/sw/jar/b.jar




回答2:


in unix use this to set the classpath:

export CLASSPATH=myClassPath

about not finding your jar, you're using a leading slash (/), that means that you path is absolute (not relative to your home folder) is this what you want?

if you want the path to be relative to your folder try:

java -jar ~/mypathToMyJar


来源:https://stackoverflow.com/questions/17549100/java-classpath-in-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!