如何在Linux中将多个jar文件添加到类路径中

23

好的,我对Linux和命令行非常陌生,对Java也相当陌生。我得到了一个建立Java程序的实习机会。我在我的电脑上(Windows)终于完成了它,现在我必须将它迁移到Linux机器上进行测试,然后作为可执行文件运行。我阅读并研究了很多关于Linux和理解类路径的资料,但仍然很难完全理解。它还没有给我留下深刻印象。有人能用例子简单地解释一下类路径的目的吗?对我最困惑的其中一个方面实际上是定义到Jar包的物理路径。我需要从usr开始还是只需要从jvm文件夹开始?如果有影响的话,我的Java程序不在jvm文件夹中。有人能帮我理清思路吗?

编辑:非常感谢大家的帮助,虽然我还不能说我完全明白了,但我对我的情况的理解更加清晰了。


可能是将目录中的所有JAR包包含在Java类路径中的重复问题。 - Stewart
5个回答

24

假设你有多个jar文件a.jar,b.jar和c.jar。在编译时将它们添加到类路径中,你需要执行以下操作:

$javac -cp .:a.jar:b.jar:c.jar HelloWorld.java

运行时执行

$java -cp .:a.jar:b.jar:c.jar HelloWorld

15
你可以使用-classpath参数。你可以使用相对路径或绝对路径。这意味着你可以使用相对于当前目录的路径,或者你可以使用以根目录/开头的绝对路径。
例如:
bash$ java -classpath path/to/jar/file MyMainClass
在这个例子中,main函数位于MyMainClass中,并且应该被包含在jar文件中的某个地方。
编译时需要使用javac命令。
例如:
bash$ javac -classpath path/to/jar/file MyMainClass.java

您还可以通过环境变量指定classpath,参考此示例

bash$ export CLASSPATH="path/to/jar/file:path/tojar/file2"
bash$ javac MyMainClass.java

对于任何一般的 Java 项目,您应该寻找名为 build.xml 的 Ant 脚本。


1
那么,如果我要编译的Java程序名为SnortMonitor,你的示例是否会更改为:bash$ java -classpath path/to/jar/file SnortMonitor - exit_1
1
啊,编译时你需要调用javac而不是java,但仍需使用-classpath和源目录。 - Petriborg
谢谢您的帮助,但是您能否再具体一些呢?也许是因为我理解不够。当我输入“javac SnortMonitor.java”时,会出现一系列错误,我知道这是因为我需要在一个名为jsendnsca的目录中包含2个jar文件。我需要每次编译和运行程序时都将这些jar文件包含在类路径中吗? - exit_1
2
没错,Pat - 只需说“javac -classpath jsendnsca.jar:otherjar.jar SnortMonitor.java”。 - Petriborg
谢谢,有没有办法让我不必每次编译和运行时都定义每个 jar? - exit_1
1
我相信如果你指定了环境变量CLASSPATH,它会使用它 - 我会编辑我的答案,以使如何做更清晰明了。 - Petriborg

5

类路径是Java编译器(命令:javac)和JVM(命令:java)查找应用程序引用的类的地方。应用程序引用另一个类是什么意思?简单来说,就是在其代码中使用该类:

例如:

public class MyClass{
    private AnotherClass referenceToAnotherClass;
    .....
}

当您尝试编译此代码(javac)时,编译器将需要AnotherClass类。同样地,当您尝试运行应用程序时,JVM也会需要AnotherClass类。为了找到该类,javac和JVM会在特定的位置查找,这些位置由classpath指定。在Linux上,classpath是一个由冒号分隔的目录列表,表示javac/JVM应该查找以定位需要的AnotherClass类的目录。因此,为了编译并运行您的类,您应确保classpath包含包含AnotherClass类的目录。然后,您可以像这样调用它:
javac -classpath "dir1;dir2;path/to/AnotherClass;...;dirN" MyClass.java //to compile it
java -classpath "dir1;dir2;path/to/AnotherClass;...;dirN" MyClass //to run it

通常类是以名为jar文件/库的“包”形式提供的。在这种情况下,您必须确保包含AnotherClass类的jar文件在您的类路径上:

javac -classpath "dir1;dir2;path/to/jar/containing/AnotherClass;...;dirN" MyClass.java //to compile it
java -classpath ".;dir1;dir2;path/to/jar/containing/AnotherClass;...;dirN" MyClass //to run it

在上面的示例中,您可以看到如何编译位于工作目录中的类(MyClass.java),然后运行已编译的类(注意类路径开头的“。”表示当前目录)。此目录也必须添加到类路径中。否则,JVM 将无法找到它。
如果您的类在 jar 文件中,正如您在问题中指定的那样,则必须确保该 jar 文件与其余所需目录一起位于类路径中。
例如:
java -classpath ".;dir1;dir2;path/to/jar/containing/AnotherClass;path/to/MyClass/jar...;dirN" MyClass //to run it

或者更一般地(假设有一些包层次结构):
java -classpath ".;dir1;dir2;path/to/jar/containing/AnotherClass;path/to/MyClass/jar...;dirN" package.subpackage.MyClass //to run it

为了避免每次运行应用程序都需要设置类路径,您可以定义一个名为CLASSPATH的环境变量。
在Linux中,在命令提示符下:
export CLASSPATH="dir1;dir2;path/to/jar/containing/AnotherClass;...;dirN" 

或者编辑 ~/.bashrc,将以下这行代码添加到文件末尾;

然而,类路径经常会发生变化,所以您可能希望将classpath设置为一组核心目录,您需要频繁使用,然后每次只需要为该会话扩展classpath。像这样:

export CLASSPATH=$CLASSPATH:"new directories according to your current needs" 

我每次想要编译和运行时都需要定义这些类路径吗? - exit_1

1
对于Linux用户,您应了解以下内容:
  1. $CLASSPATH is specifically what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override). Using -cp (--classpath) requires that you keep track of all the directories manually and copy-paste that line every time you run the program (not preferable IMO).

  2. The colon (":") character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run "export CLASSPATH=...." you want to include the current value "$CLASSPATH" in order to append to it. For example:

    export CLASSPATH=.
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
    

    In the first line above, you start CLASSPATH out with just a simple 'dot' which is the path to your current working directory. With that, whenever you run java it will look in the current working directory (the one you're in) for classes. In the second line above, $CLASSPATH grabs the value that you previously entered (.) and appends the path to a mysql dirver. Now, java will look for the driver AND for your classes.

  3. echo $CLASSPATH
    

    is super handy, and what it returns should read like a colon-separated list of all the directories you want java looking in for what it needs to run your script.

  4. Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html


1

步骤1。

vi ~/.bashrc

步骤2. 在最后添加此行:

export CLASSPATH=$CLASSPATH:/home/abc/lib/*;  (Assuming the jars are stored in /home/abc/lib) 

第三步。

source ~/.bashrc

完成这些步骤后,直接编译并运行您的程序(例如:javac xyz.java)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接