简体   繁体   English

如何在类路径中添加外部库?

[英]How to add external library in classpath?

I have a folder that contains these java files: Main, TableManager, CustomFileWriter, CustomFileReader plus the external library commons-lang3-3.0.jar.I'm trying to compile the Main.java with this command我有一个包含这些 java 文件的文件夹:Main、TableManager、CustomFileWriter、CustomFileReader 加上外部库 commons-lang3-3.0.jar。我正在尝试使用此命令编译 Main.java

javac -cp commons-lang3-3.0.jar Main.java

but it says cannot find symbol但它说找不到符号

TableManager table = new TableManager()

I create an instance of TableManager in Main class.我在 Main 类中创建了一个 TableManager 实例。 Without the external library and compiling with just javac Main.java works fine.没有外部库,只用 javac Main.java 编译工作正常。 How can I fix this.我怎样才能解决这个问题。 I need the external library for the StringUtils.我需要 StringUtils 的外部库。 I'm not using frameworks.我没有使用框架。 Just text editor and running to terminal.只是文本编辑器并运行到终端。

To compile a Java file and include a Jar file, enter the following command line:要编译 Java 文件并包含Jar文件,请输入以下命令行:

  javac -cp jar-file Main.java

For multiple JAR files, separate the jar-files with semicolons ;对于多个JAR文件,用分号分隔jar-files ; , with the following command line: , 使用以下命令行:

  javac -cp jar-file1;jar-file2;jar-file3 Main.java

You need the path, not just the jar name, like您需要路径,而不仅仅是 jar 名称,例如

javac -cp c:\\home\\ann\\public_html\\classes\\compute.jar engine\\ComputeEngine.java

You can check it in the documentation .您可以在文档中查看它。

To compile a class (on windows) with a jar in the same direcory use:要使用同一目录中的 jar 编译类(在 Windows 上),请使用:

javac -cp .;myjar.jar MyClass.java

To then run the class you can use:然后运行该类,您可以使用:

java -cp .;myjar.jar MyClass

NOTE: on linux you will need to replace ;注意:在 linux 上,您需要替换; with ::

The "-cp" Option overwrites your classpath. “-cp”选项会覆盖您的类路径。 So in order to successfully compile and run your java-app, you have to add the path of your Main.class file and the external library as arguments.因此,为了成功编译和运行您的 java-app,您必须添加 Main.class 文件和外部库的路径作为参数。 Here the "."这里的“。” is the relative path to your Main.class file and commons-lang3-3.0.jar the relative path to the external library.是 Main.class 文件的相对路径,commons-lang3-3.0.jar 是外部库的相对路径。 Under Windows it is sometimes necessarry to use quotes.在 Windows 下,有时需要使用引号。

To compile:编译:

javac -cp ".;commons-lang3-3.0.jar" Main.java

To run:跑步:

java -cp ".;commons-lang3-3.0.jar" Main

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

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