简体   繁体   English

如何在Linux上设置类路径?

[英]How do I set the class path on Linux?

I need an answer for the question in the title. 我需要在标题中回答问题。

Thanks. 谢谢。

export CLASSPATH=/your/stuff/

或保留系统范围的设置:

export CLASSPATH=$CLASSPATH:/your/addition/

If you mean the Java classpath (from your tag), then this is only different from Windows in terms of path-separators (: instead of ;). 如果您指的是Java类路径 (来自您的标记),那么就路径分隔符(:而不是;)而言,这仅与Windows不同。 For example 例如

java -classpath /mydir/mylib.jar:/otherdir/otherlib.jar com.MyProgram -Xmx64m

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? 该环境类路径是否包含计算机上每个项目所需的每个JAR? That's not practical. 那不切实际。

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的标准,但我认为它不应该是现在。

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" ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM