0

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) **

3
  • 2
    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. Commented Jul 8, 2013 at 14:55
  • Are you using Maven >? Commented Jul 8, 2013 at 15:05
  • I'm not using maven (don't actually know what it is) Commented Jul 8, 2013 at 15:31

7 Answers 7

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was nice and clear. The key phrase I was previously missing is "absolute paths of jars"
1

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.

2 Comments

So com.example.Main is the directory to the main .java file? And yourjar.jar is the directory to the library .jar files?
@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.
0

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

Comments

0

Update the classpath with apache-coomons libraries.

4 Comments

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)
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).
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
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!
0

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.

Comments

0

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

1 Comment

This is fine for windows. On Linux, path separator between jar files should be : instead of ;
-2

If you are using Maven then

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.3</version>
</dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.