Setting classpath for a JAR

扶醉桌前 提交于 2019-12-18 09:13:28

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8789683/setting-classpath-for-a-jar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!