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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11459664/
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-08-06 13:47:59  来源:igfitidea点击:

How to add multiple jar files in classpath in linux

javalinuxjarclasspath

提问by exit_1

Okay, I'm very new to linux and command line, and fairly new to java. I got an internship building a java program. I finally got it done on my machine (windows) and now I have to migrate it to a linux machine to test and then have it run as an executable. I have done much reading and researching on linux and understanding classpaths but it is still all very hard to fully comprehend. It's just not clicking for me yet. Can anyone explain the purpose of classpath in a simplified way using examples? One of the most confusing aspects to me is actually defining the physical path to the jar. Do I start all the way from usr or do I only need to begin from the jvm folder? If it matters, my java program is not located in the jvm folder. Can anyone shed some light for me?

好的,我对 linux 和命令行很陌生,对 java 也很陌生。我得到了一份构建 Java 程序的实习机会。我终于在我的机器(windows)上完成了它,现在我必须将它迁移到一台 linux 机器上进行测试,然后让它作为可执行文件运行。我对 linux 和理解类路径做了很多阅读和研究,但仍然很难完全理解。它只是还没有为我点击。任何人都可以使用示例以简化的方式解释类路径的目的吗?对我来说最令人困惑的方面之一实际上是定义 jar 的物理路径。我是从 usr 一直开始还是只需要从 jvm 文件夹开始?如果重要,我的 java 程序不在 jvm 文件夹中。任何人都可以为我提供一些帮助吗?

EDIT: thank you guys very much for your help, I can't say that I'm fully in the clear but my understanding of my situation is a lot better.

编辑:非常感谢你们的帮助,我不能说我完全清楚,但我对我的情况的理解要好得多。

采纳答案by Petriborg

You use the -classpathargument. You can use either a relative or absolute path. What that means is you can use a path relative to your current directory, OR you can use an absolute path that starts at the root /.

你使用这个-classpath论点。您可以使用相对或绝对路径。这意味着您可以使用相对于当前目录的路径,或者您可以使用从 root 开始的绝对路径/

Example:

例子:

bash$ java -classpath path/to/jar/file MyMainClass

In this example the mainfunction is located in MyMainClassand would be included somewhere in the jar file.

在此示例中,该main函数位于MyMainClassjar 文件中并将包含在该文件中的某处。

For compiling you need to use javac

对于编译,您需要使用 javac

Example:

例子:

bash$ javac -classpath path/to/jar/file MyMainClass.java

You can also specify the classpath via the environment variable, follow this example:

您还可以通过环境变量指定类路径,请参考以下示例

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

For any normally complex java project you should look for the ant script named build.xml

对于任何通常复杂的 java 项目,您应该查找名为的 ant 脚本 build.xml

回答by Razvan

The classpath is the place(s) where the java compiler (command: javac) and the JVM (command:java) look in order to find classes which your application reference. What does it mean for an application to reference another class ? In simple words it means to use that class somewhere in its code:

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

Example:

例子:

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

When you try to compile this (javac) the compiler will need the AnotherClass class. The same when you try to run your application: the JVM will need the AnotherClass class. In order to to find this class the javac and the JVM look in a particular (set of) place(s). Those places are specified by the classpath which on linux is a colon separated list of directories (directories where the javac/JVM should look in order to locate the AnotherClass when they need it).

当您尝试编译此 (javac) 时,编译器将需要 AnotherClass 类。当您尝试运行应用程序时也是如此:JVM 将需要 AnotherClass 类。为了找到这个类,javac 和 JVM 会在特定的(一组)地方查找。这些位置由类路径指定,在 linux 上是一个以冒号分隔的目录列表(javac/JVM 应该查看的目录,以便在需要时找到 AnotherClass)。

So in order to compile your class and then to run it, you should make sure that the classpath contains the directory containing the AnotherClass class. Then you invoke it like this:

因此,为了编译您的类然后运行它,您应该确保类路径包含包含 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

Usually classes come in the form of "bundles" called jar files/libraries. In this case you have to make sure that the jar containing the AnotherClass class is on your classpaht:

通常类以称为 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

In the examples above you can see how to compile a class (MyClass.java) located in the working directory and then run the compiled class (Note the "." at the begining of the classpath which stands for current directory). This directory has to be added to the classpath too. Otherwise, the JVM won't be able to find it.

在上面的示例中,您可以看到如何编译位于工作目录中的类 (MyClass.java),然后运行编译后的类(注意类路径开头的“.”代表当前目录)。这个目录也必须添加到类路径中。否则,JVM 将无法找到它。

If you have your class in a jar file, as you specified in the question, then you have to make sure that jar is in the classpath too , together with the rest of the needed directories.

如果您将类放在 jar 文件中,正如您在问题中所指定的那样,那么您必须确保 jar 以及其余所需的目录也在类路径中。

Example:

例子:

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

or more general (assuming some package hierarchy):

或更一般的(假设一些包层次结构):

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

In order to avoid setting the classpath everytime you want to run an application you can define an environment variable called CLASSPATH.

为了避免每次要运行应用程序时都设置类路径,您可以定义一个名为 CLASSPATH 的环境变量。

In linux, in command prompt:

在 linux 中,在命令提示符下:

export CLASSPATH="dir1;dir2;path/to/jar/containing/AnotherClass;...;dirN" 

or edit the ~/.bashrc and add this line somewhere at the end;

或编辑 ~/.bashrc 并在最后某处添加此行;

However, the class path is subject to frequent changes so, you might want to have the classpath set to a core set of dirs, which you need frequently and then extends the classpath each time you need for that session only. Like this:

但是,类路径会经常更改,因此,您可能希望将类路径设置为一组核心目录,您经常需要这些目录,然后仅在每次需要该会话时扩展类路径。像这样:

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

回答by noPE

Say you have multiple jar files a.jar,b.jar and c.jar. To add them to classpath while compiling you need to do

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

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

To run do

运行做

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

回答by user9218212

Step 1.

第1步。

vi ~/.bashrc

Step 2. Append this line on the last:

步骤 2. 在最后添加这一行:

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

Step 3.

第 3 步。

source ~/.bashrc

After these steps direct complile and run your programs(e.g. javac xyz.java)

这些步骤后直接编译并运行你的程序(例如javac xyz.java)

回答by Adam Winter

For linux users, you should know the following:

对于 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. $CLASSPATH 特别是 Java 用来查看多个目录以查找脚本所需的所有不同类的工具(除非您使用 -cp 覆盖明确告诉它)。使用 -cp (--classpath) 要求您手动跟踪所有目录并在每次运行程序时复制粘贴该行(IMO 不推荐)。

  2. 冒号 (":") 字符分隔不同的目录。只有一个 $CLASSPATH 并且其中包含所有目录。因此,当您运行“export CLASSPATH=....”时,您希望包含当前值“$CLASSPATH”以便附加到它。例如:

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

    在上面的第一行中,您只用一个简单的“点”开始 CLASSPATH,它是您当前工作目录的路径。这样,无论何时运行 java,它都会在当前工作目录(您所在的目录)中查找类。在上面的第二行中,$CLASSPATH 获取您之前输入的值 (.) 并将路径附加到 mysql 驱动程序。现在,java 将查找驱动程序和您的类。

  3. echo $CLASSPATH
    

    非常方便,它返回的内容应该像冒号分隔的所有目录列表,其中包含您希望 java 查找运行脚本所需的所有目录。

  4. Tomcat 不使用 CLASSPATH。在此处阅读如何处理:https: //tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html