How to set the classpath in Java?

纵饮孤独 提交于 2019-12-01 13:45:20

maybe you want to try to use maven for build you application? It's really easy to setup and it annihilate all problems with dependency management.

also from java 6 you can use wildcards in classpath: set CLASSPATH=my_libs\*;

Try using export CLASSPATH=... instead of set CLASSPATH=...

(I assume you're using a Unix box of some description, given the colons in the classpath.)

The most pain-free way, in my opinion, is to create batch files that contain all your project related jars... one to compile and another to run:-

compile.bat

javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1

run.bat

java -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1

With that, you can do this:-

compile.bat CollectionIndexer.java

run.bat CollectionIndexer

Even better, you can combine them together:-

compilerun.bat

Make sure you append ".java" to javac's %1

javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1.java
java -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1

With that, you can do this:-

compilerun.bat CollectionIndexer

I like this approach much better than setting classpath because I don't need to retype the classpath each time I open the terminal. :)

By the way, it is not very nice to modify the CLASSPATH environment variable specifically for compiling a project, given that after that, all other projects will inherit this change. This of course stands only if you are globally changing it. If you aren't and instead you are planning to write a little script to build you project, why don't you consider using ant? Good luck!

You have set the CLASSPATH, but you didn't put it into the environment. So it's a variable, but not quite an environmental variable.

To promote an in-script / in-session variable to an environmental variable, use the command export like so

export CLASSPATH

This promotes the variable to an environmental variable (which will be accessible to any shell that inherits the environment).

Some systems allow the combining of the set and the export. In such systems, you can combine your set command with the export command like so:

export CLASSPATH=<your value here>

The java command only reads the environmental variable CLASSPATH. It can't look into non-environmental variables as those are not inherited from process to process.

Besides exporting your UNIX environment, use absolute paths. For example, the class path entry: commons-digester-2.1/commons-digester-2.1.jar only works if you are in the parent directory of the commons-digester-2.1 installation directory.

On unix, there should be a common location into which you install your packages. Something like /usr/local, /usr/lib, or /usr/local/lib.

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