Java:导入 StringUtils

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

Java: Importing StringUtils

javaapache-stringutils

提问by btbam91

I am trying to import StringUtils. My first step was downloading 'commons-lang3-3.4.jar' which I included in the same directory as my PersonTester.java file that I am working on. In my PersonTester.java in which I intend to use StringUtils, I include:

我正在尝试导入 StringUtils。我的第一步是下载“commons-lang3-3.4.jar”,我将其包含在与我正在处理的 PersonTester.java 文件相同的目录中。在我打算使用 StringUtils 的 PersonTester.java 中,我包括:

import org.apache.commons.lang3.StringUtils;

When I try and compile I get the following error:

当我尝试编译时,出现以下错误:

PersonTester.java:6: error: package org.apache.commons.lang3 does not exist

import org.apache.commons.lang3.StringUtils;

导入 org.apache.commons.lang3.StringUtils;

When I comment out the import statement and remove any statements that intend to utilize StringUtils, it compiles and runs just fine.

当我注释掉 import 语句并删除任何打算使用 StringUtils 的语句时,它编译并运行得很好。

Thank you!

谢谢!

采纳答案by Andreas

Putting commons-lang3-3.4.jarnext to you Java source file does not automatically add it to the classpath.

放在commons-lang3-3.4.jar您旁边的 Java 源文件不会自动将其添加到类路径中。

You have to explicitly add it to classpath, and you would usually not put it next to your source file.

您必须将其显式添加到类路径中,并且通常不会将其放在源文件旁边。

Depending on your environment, you need to add -cp commons-lang3-3.4.jarto the javacand javacommands, or tell your IDE to add it to the classpath.

根据您的环境,您需要添加-cp commons-lang3-3.4.jarjavacjava命令,或告诉您的 IDE 将其添加到类路径中。

If you do export CLASSPATH="directory with files here"as mentioned in a comment to question, you need to change it to include both the directory of your .class files and explicitly list the .jar file, e.g.

如果您export CLASSPATH="directory with files here"按照问题的评论中所述进行操作,则需要将其更改为包含 .class 文件的目录并明确列出 .jar 文件,例如

export CLASSPATH=~/assignment/week3:~/assignment/week3/commons-lang3-3.4.jar