Java - Jar无法找到包含的库和主类

时间:2023-01-13 12:50:12

I am currently trying to make a Jar with all my libraries included. What I have done, I have created folders like this :

我目前正在尝试制作一个包含所有库的Jar。我做了什么,我创建了这样的文件夹:

eancopy (which contain all my classes)
lib (containing all my libraries : mongo, jedis...)
META-INF/MANIFEST.MF

My main class is named process (within the package eancopy)

我的主要类是命名过程(在包eancopy中)

My manifest is like this:

我的清单是这样的:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Class-Path: lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar
Created-By: 1.6.0_24-b07 (Sun Microsystems Inc.)
Main-Class: eancopy.process

I have generated the JAR with this command :

我用这个命令生成了JAR:

jar cvmf META-INF/MANIFEST.MF EANcopy.jar eancopy/*.class lib/*.jar

My problem : When executing the JAR by java -jar EANcopy.jar, it works when it is executed in the place where I have generated the JAR but in another place I have the message :

我的问题:当通过java -jar EANcopy.jar执行JAR时,它在我生成JAR的地方执行时有效,但在另一个地方我有消息:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject
Caused by: java.lang.ClassNotFoundException: com.mongodb.DBObject
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: eancopy.process. Program will exit.

Any ideas ? Thanks

有任何想法吗 ?谢谢

2 个解决方案

#1


0  

This exception shows you the cause of the problem:

此异常显示问题的原因:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject

线程“main”中的异常java.lang.NoClassDefFoundError:com / mongodb / DBObject

You haven't specified these libs lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar in your CLASSPATH environment variable, hence why it fails when run in other places.

您还没有在CLASSPATH环境变量中指定这些库lib \ commons-pool-1.5.6.jar lib \ jedis-2.0.0.jar lib \ mongo-2.6.3.jar,因此在其他运行时它失败的原因地方。

#2


0  

Class-path attribute only finds the libraries co-located with your jar not those contained in your jar file. According to this thread, you either have to use a special class loader or use a method to explode the included libraries inside your jar.

类路径属性仅查找与jar共存的库,而不是jar文件中包含的库。根据这个线程,您要么必须使用特殊的类加载器,要么使用方法来爆炸jar中包含的库。

#1


0  

This exception shows you the cause of the problem:

此异常显示问题的原因:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject

线程“main”中的异常java.lang.NoClassDefFoundError:com / mongodb / DBObject

You haven't specified these libs lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar in your CLASSPATH environment variable, hence why it fails when run in other places.

您还没有在CLASSPATH环境变量中指定这些库lib \ commons-pool-1.5.6.jar lib \ jedis-2.0.0.jar lib \ mongo-2.6.3.jar,因此在其他运行时它失败的原因地方。

#2


0  

Class-path attribute only finds the libraries co-located with your jar not those contained in your jar file. According to this thread, you either have to use a special class loader or use a method to explode the included libraries inside your jar.

类路径属性仅查找与jar共存的库,而不是jar文件中包含的库。根据这个线程,您要么必须使用特殊的类加载器,要么使用方法来爆炸jar中包含的库。