2

I have a standalone java project. I have performed a maven clean install. I now go into the target\classes folder via command prompt and set all the required files in classpath. Now I execute the main class. The result is being displayed.

Now I move back to target folder via command prompt and try to execute the jar file (jar file has a manifest file where the main class is defined).

Java -cp %CLASSPATH% -jar Destination-01.19-SNAPSHOT.jar

Now I get the below exception. I have also removed the classpath attribute from the statement above but still get the same exception.The required class is set in classpath variable which is the reason why I was able to run the main class without using the -jar attribute.

Exception in thread "Main Thread" java.lang.NoClassDefFoundError: org/apache/co mons/logging/LogFactory

Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • 1
    I think you need to put the classpath in you MANIFEST file – Jakob Weisblat Jan 09 '12 at 14:12
  • Check [this too](http://stackoverflow.com/questions/1510071/maven-how-can-i-add-an-arbitrary-classpath-entry-to-a-jar) – ring bearer Jan 09 '12 at 14:15
  • No is not. Try printing `echo %CLASSPATH%` and see if the **jar** file containing the LogFactory class is there. – OscarRyz Jan 09 '12 at 14:15
  • Thanks. It is in the classpath - did echo %CLASSPATH%:.m2\repository\commons-logging\commons-logging \1.1\commons-logging-1.1.jar;.m2\repository\log4j\log4j\1.2.14\l og4j-1.2.14.jar; If it is not in the classpath , then I guess the stand alone main class should also not get executed. – Punter Vicky Jan 09 '12 at 14:18
  • Thanks Jake and Ring Bearer. I will try it out - I am using Maven. Wouldn't the jar take anything that is set outside? – Punter Vicky Jan 09 '12 at 14:20

2 Answers2

4

It seems -cp and -jar are incompatible. You need to put the classpath in the manifest

See http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

If you want maven to automatically update the classpath, see: -

http://maven.apache.org/shared/maven-archiver/examples/classpath.html

Peter Jamieson
  • 745
  • 5
  • 10
4

When you use -jar this is the sole mechanism setting the classpath for the program.

You must either have all the jars you need listed in the Class-Path line in the manifest (which implies they need to be findable in the local file system) or create a custom class loader capable of locating the jar you need.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347