如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:55:54  来源:igfitidea点击:

How to set the classpath in java in ubuntu and how to work with jar?

javaubuntujarclasspath

提问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 PATHas 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.jarb.jar 中

回答by Paulo Mattos

You should add your jar filesto your CLASSPATHinstead:

您应该将您的jar 文件添加到您的CLASSPATH

export CLASSPATH=/home/somebody/lib/java/a.jar:/home/somebody/lib/java/b.jar

The classpath supports directories (with *.classfiles) orindividual jar files. You can also add wildcards(or use java -classpathoption) 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 PATHenvironment variable at all. The CLASSPATHis more than enough.

顺便说一句,您根本不需要更改PATH环境变量。该CLASSPATH是绰绰有余。

回答by Lew Bloch

As per the documentation:

根据文档

It's better to use the classpath option (-classpathor -cp) to a command than the global CLASSPATHenvironment variable.

对命令使用类路径选项 (-classpath-cp) 比全局CLASSPATH环境变量更好。