如何在 ubuntu 中设置 java 中的类路径以及如何使用 jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44087261/
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 to set the classpath in java in ubuntu and how to work with jar?
提问by Tomonso Ejang
I'm trying to import the class from jar files. I want to place all of the jar files in the directory:
我正在尝试从 jar 文件中导入类。我想将所有 jar 文件放在目录中:
/home/somebody/lib/java/
Compiling with the command given below says:
使用下面给出的命令编译说:
package com.a.A does not exits**. java C.java
包 com.aA 不退出**。java C.java
I set the following in profile.
我在配置文件中设置了以下内容。
CLASSPATH=/home/somebody/lib/java
then exported it. Added to PATH
as well
然后导出。加入PATH
以及
C.java
C.java
import com.a.A;
import com.a.AA;
import com.b.B;
import com.b.BB;
class B {
public static void main() {
new A();
new AA();
new B();
new BB();
}
}
the classfiles are in the following jar_file a.jarand b.jar.
类文件位于以下 jar_file a.jar和b.jar 中。
回答by Paulo Mattos
You should add your jar filesto your CLASSPATH
instead:
您应该将您的jar 文件添加到您的CLASSPATH
:
export CLASSPATH=/home/somebody/lib/java/a.jar:/home/somebody/lib/java/b.jar
The classpath supports directories (with *.class
files) orindividual jar files. You can also add wildcards(or use java -classpath
option) but I will leave that off for now to don't further complicate things ;)
类路径支持目录(带有*.class
文件)或单独的 jar 文件。您还可以添加通配符(或使用java -classpath
选项),但我暂时将其搁置,以免使事情进一步复杂化;)
By the way, you don't need to change your PATH
environment variable at all. The CLASSPATH
is more than enough.
顺便说一句,您根本不需要更改PATH
环境变量。该CLASSPATH
是绰绰有余。
回答by Lew Bloch
As per the documentation:
根据文档:
It's better to use the classpath option (-classpath
or -cp
) to a command than the global CLASSPATH
environment variable.
对命令使用类路径选项 (-classpath
或-cp
) 比全局CLASSPATH
环境变量更好。