1

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!

btbam91
  • 578
  • 2
  • 8
  • 20
  • 1
    Verify the classpath contains your downloaded jar. – Balwinder Singh Feb 29 '16 at 22:55
  • This looks like you are missing commons-lang3-3.4.jar is in your classpath while compiling. – Raghu K Nair Feb 29 '16 at 22:56
  • 1
    I have the .jar file right next to the .java files that I am working on. I also do this command in Mac Terminal: export CLASSPATH="directory with files here" Is this incorrect? – btbam91 Feb 29 '16 at 22:59
  • The compiler must be made aware that the library jar is being used by your project. Putting it in the same directory as your Java source file won't do it (in fact, you typically want to store your libraries in a different directory from your source files). Tell us how you're compiling your project, and we'll take it from there. – Mike Baranczak Feb 29 '16 at 23:00
  • @Mike. I essentially create a new folder for each week's class assignment. I create and work on .java files in there. I open the Mac Terminal, cd to the directory, and javac the .java file with my main method. – btbam91 Feb 29 '16 at 23:02
  • 1
    You need to include each jar individually on the classpath. – Mike Baranczak Feb 29 '16 at 23:02
  • Adding a directory to a Java classpath is for when the directory contains compiled class files. – Mike Baranczak Feb 29 '16 at 23:03
  • If you are using a generic text editor (even a syntax highlighting one) and compiling manually using `javac`, then you are crippling yourself. Use an IDE such as [Eclipse](https://eclipse.org/), [NetBeans](https://netbeans.org/), [BlueJ](http://www.bluej.org/), ... – Andreas Feb 29 '16 at 23:05
  • @MikeBaranczak Andreas Thank you for clarifying that I need to include each jar individually on the classpath. Andreas, my course does not permit us to use an advanced IDE with syntax highlighting or anything of the sort. I am using Visual Studio Code. – btbam91 Feb 29 '16 at 23:10
  • Teaching people to program in Java, but not allowing them to use an IDE, is ***insane!!!!*** It's like teaching someone to fix a car, but not allowing them to use a wrench. It can be done (maybe), but your fingers will bleed a lot, and the car may fall apart because the bolts are not tight enough. – Andreas Feb 29 '16 at 23:16

1 Answers1

1

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
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    @BoristheSpider or an IDE such as [Eclipse](https://eclipse.org/), [NetBeans](https://netbeans.org/), [BlueJ](http://www.bluej.org/), ... – Andreas Feb 29 '16 at 23:10
  • 2
    Or ... and IDE _as well as_ Maven. A truly magic combination. P.S. see [coding horror](https://blog.codinghorror.com/the-f5-key-is-not-a-build-process/). – Boris the Spider Feb 29 '16 at 23:11
  • Thank you! As I mentioned in comment above, we are not permitted in using an advanced IDE. – btbam91 Feb 29 '16 at 23:12
  • @BoristheSpider Very true. Mostly I've used Ant, but a production development project should always use IDE + build tool (Ant, Maven, Gradle, ...). – Andreas Feb 29 '16 at 23:19