Java 如何在 Linux 上设置类路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set the class path on Linux?
提问by user48094
I need an answer for the question in the title.
我需要标题中问题的答案。
Thanks.
谢谢。
回答by Andrew Hare
Here are two good tutorials I found via Google:
这是我通过谷歌找到的两个很好的教程:
http://www.linuxheadquarters.com/howto/basic/classpath.shtml
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html
http://www.linuxheadquarters.com/howto/basic/classpath.shtml
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html
回答by vartec
export CLASSPATH=/your/stuff/
or preserving system wide settings:
或保留系统范围的设置:
export CLASSPATH=$CLASSPATH:/your/addition/
回答by oxbow_lakes
If you mean the Java classpath(from your tag), then this is only different from Windows in terms of path-separators (: instead of ;). For example
如果您指的是Java 类路径(来自您的标签),那么这仅在路径分隔符(:而不是 ;)方面与 Windows 不同。例如
java -classpath /mydir/mylib.jar:/otherdir/otherlib.jar com.MyProgram -Xmx64m
回答by duffymo
I don't think you should have a system classpath environment variable on Linux or any other operating system.
我认为您不应该在 Linux 或任何其他操作系统上拥有系统类路径环境变量。
Each and every project should have its own classpath settings. They're usually set by scripts or convention, so there's no need for a system environment variable.
每个项目都应该有自己的类路径设置。它们通常由脚本或约定设置,因此不需要系统环境变量。
Besides, what would you do if two projects had conflicting JARs required?
此外,如果两个项目需要冲突的 JAR,你会怎么做?
Will that environment classpath include every JAR needed by every project on your machine? That's not practical.
该环境类路径是否包含机器上每个项目所需的每个 JAR?那不实用。
A classpath environment variable might have been the standard with Java 1.0, but I don't think it should be now.
类路径环境变量可能是 Java 1.0 的标准,但我认为现在不应该是。
回答by Aaron Digulla
Create a small shell script which sets the classpath:
创建一个设置类路径的小shell脚本:
#!/bin/bash
export JAVA_HOME=...
cp=$(find lib -name "*.jar" -exec printf :{} ';')
if [[ -n "$CLASSPATH" ]]; then
cp="$cp;CLASSPATH"
fi
"$JAVA_HOME/bin/java" -classpath "$cp" ...