简体   繁体   English

Java:导入StringUtils

[英]Java: Importing StringUtils

I am trying to import StringUtils. 我正在尝试导入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. 我的第一步是下载“ commons-lang3-3.4.jar”,它与我正在处理的PersonTester.java文件包含在同一目录中。 In my PersonTester.java in which I intend to use StringUtils, I include: 我打算在其中使用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! 谢谢!

Putting commons-lang3-3.4.jar next 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. 您必须将其显式添加到classpath中,并且通常不会将其放在源文件旁边。

Depending on your environment, you need to add -cp commons-lang3-3.4.jar to the javac and java commands, or tell your IDE to add it to the classpath. 根据您的环境,您需要将-cp commons-lang3-3.4.jar添加到javacjava命令中,或者告诉您的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, eg 如果确实要在要提问的注释中提到export CLASSPATH="directory with files here" ,则需要对其进行更改,以包括.class文件的目录和显式列出.jar文件,例如

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

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

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