Take the 2-minute tour ×

I've made a program which uses the Apache Commons io and lang3 libraries.

It runs fine in eclipse but I can't get it to run from cmd and it comes up with the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/la ng3/StringUtils 
    at mainActivity.main(mainActivity.java:37) 
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 1 more

How do I get it to run from cmd (so that it works in a batch file)

** I'm relatively new and am on Win 8 (I am not using Maven) **

share|improve this question
Welcome to StackOverflow! Here, we expect questions to be supported by code, original research, and examples of the exact problem you're trying to solve. When asking a question about running from the command line, you should povide details of your folder/project structure, the exact command you're running, and your operating system information. –  Brian Jul 8 '13 at 14:55
Are you using Maven >? –  Makky Jul 8 '13 at 15:05
I'm not using maven (don't actually know what it is) –  darkace Jul 8 '13 at 15:31

7 Answers 7

up vote 1 down vote accepted

When you run the program from eclipse, the jar files are added by Eclipse in the classpath. But when you run the same form command prompt, the jars files are needed to be in classpath explicitly.

There are two ways

  • Run your program with classpath as follows

java -classpath ".;c:\yourLib*" YourApp

where yourLib is the folder containing the apache-commons jars.

  • set a CLASSPATH environment variable with value to absolute paths of jars seperated by ;

set CLASSPATH=D:\yourLib\

then run your program without classpath option. Runtime will pick the required classpath from environment variable defined earlier.

java YourApp

Note: I am assuming windows platform.

share|improve this answer
Thank you, that was nice and clear. The key phrase I was previously missing is "absolute paths of jars" –  darkace Jul 8 '13 at 15:41

Add the necessary jars to your classpath. Windows:

> java -classpath yourjar.jar;lib\*.jar com.example.Main


$ java -classpath yourjar.jar:lib/*.jar com.example.Main

The only differences are the directory separator (//\) and path separator (:/;). This assumes your Apache jar(s) is in a lib directory in your project.

share|improve this answer
So com.example.Main is the directory to the main .java file? And yourjar.jar is the directory to the library .jar files? –  darkace Jul 8 '13 at 15:18
@darkace It's the full qualified class name (pacakge + '.' + class) of the class that declares public static void main(String[] args) that you want to run. Take a look at the java command-line documentation. –  Brian Jul 8 '13 at 16:20

You have to make your JAVA aware of libraries you are using. Java doesn't know where to look for those libraries.

share|improve this answer

Update the classpath with apache-coomons libraries.

share|improve this answer
I think that's the bit I'm doing wrong... The commons folders are in the eclipse folder. Do I specify both commons folders in separate paths in the Environment Variable path? Do I specify the individual jar files? Do I need to add anything to my Java installation folders? (I'm on Windows 8, Java and Eclipse are installed on the D drive; The .class I'm running is on the C drive) –  darkace Jul 8 '13 at 15:13
No path variable is different from classpath. Path variable is for system to know about the executables, say javac. classpath on the other hand is used for specifying all the places where the classes used in your program may be present( like a .jar file). –  zerocool Jul 8 '13 at 15:23
You must be having apache-common-lang jars some where in your local machine. Just use the below command from cmd:java -classpath DIRECTORY_WHERE_JAR_IS_PRESENT(copy paste that location)*.jar;ANOTHERLOCATION package.CLASSNAME –  zerocool Jul 8 '13 at 15:27
Thank you very much for the clarification, I must have skimmed over the fact that classpath and path weren't referring to the same thing! –  darkace Jul 8 '13 at 15:42

Your classpath has problems. Eclipse is able to use the project settings for resolving runtime library dependencies in the classpath. Add commons-lang*.jar and commons-io*.jar files in the classpath of java. You can use -cp or -classpath option to set classpath on command line.

share|improve this answer

You'll need to manually add each jar file in the CMD window. You can do this using -cp

For example

java -cp /path/to/file;/path/to/anotherfile ...

Also, please remember to use the right path seperator - ; for windows, and : for linux

share|improve this answer
This is fine for windows. On Linux, path separator between jar files should be : instead of ; –  Bimalesh Jha Jul 8 '13 at 15:01

If you are using Maven then

share|improve this answer

Your Answer


