Reputation: 878
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) **
Upvotes: 0
Views: 4473
Reputation: 1750
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
Upvotes: 0
Reputation: 17461
If you are using Maven then
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
Upvotes: -2
Reputation: 17903
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
java -classpath ".;c:\yourLib*" YourApp
where yourLib
is the folder containing the apache-commons jars.
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.
Upvotes: 1
Reputation: 17309
Add the necessary jars to your classpath. Windows:
> java -classpath yourjar.jar;lib\*.jar com.example.Main
Unix:
$ 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.
Upvotes: 1
Reputation: 1504
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.
Upvotes: 0
Reputation: 10332
You have to make your JAVA aware of libraries you are using. Java doesn't know where to look for those libraries.
Upvotes: 0