javaapache-stringutils

Java: Importing StringUtils


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:

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;

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

Thank you!


Solution

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

    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.jar to the javac and java commands, or tell your IDE to add it to the classpath.

    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=~/assignment/week3:~/assignment/week3/commons-lang3-3.4.jar