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

5
  • 1
    I think you need to put the classpath in you MANIFEST file Commented Jan 9, 2012 at 14:12
  • Check this too Commented Jan 9, 2012 at 14:15
  • No is not. Try printing echo %CLASSPATH% and see if the jar file containing the LogFactory class is there. Commented Jan 9, 2012 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. Commented Jan 9, 2012 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? Commented Jan 9, 2012 at 14:20

2 Answers 2

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.

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

Comments

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

1 Comment

Thanks Peter! I have set one dependency in <systemPath> - but this not getting added to classpath in manifest.

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.