Java - Jar无法找到包含的库和主类 [英] Java - Jar could not find included libraries and main class
问题描述
我目前正在尝试制作一个包含所有库的Jar。我做了什么,我创建了这样的文件夹:
I am currently trying to make a Jar with all my libraries included. What I have done, I have created folders like this :
eancopy (which contain all my classes)
lib (containing all my libraries : mongo, jedis...)
META-INF/MANIFEST.MF
我的主类命名为进程
(在包eancopy中)
My main class is named process
(within the package eancopy)
我的清单是这样的:
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
我用这个命令生成了JAR:
I have generated the JAR with this command :
jar cvmf META-INF/MANIFEST.MF EANcopy.jar eancopy/*.class lib/*.jar
我的问题:通过执行JAR时java -jar EANcopy.jar
,它在我生成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 :
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.
任何想法?谢谢
推荐答案
类路径属性只发现与jar共存的库而不是jar文件中包含的库。
根据主题,您必须使用特殊的类加载器或使用方法来爆炸jar中包含的库。
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.
这篇关于Java - Jar无法找到包含的库和主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!