设置JAR的类路径 [英] Setting classpath for a JAR

查看:111
本文介绍了设置JAR的类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独立的java项目。我已经完成了maven clean安装。我现在通过命令提示符进入target\classes文件夹,并在classpath中设置所有必需的文件。现在我执行主类。结果显示出来。

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.

现在我通过命令提示符移回目标文件夹并尝试执行jar文件(jar文件有一个清单文件,其中定义了主类)。

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

现在我得到以下异常。我还从上面的语句中删除了classpath属性,但仍然得到了相同的异常。在classpath变量中设置了所需的类,这就是我能够在不使用-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.

线程主线程中的异常java.lang.NoClassDefFoundError:org / apache / co
mons / logging / LogFactory

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

推荐答案

当您使用 -jar 时,这是设置程序类路径的唯一机制。

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

您必须在清单中的Class-Path行中列出您需要的所有jar(这意味着它们需要在本地文件系统中找到)或者创建一个能够定位的自定义类加载器你需要的罐子。

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.

这篇关于设置JAR的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆