我想创建一个完整的单个Jar球,其中包含所有依赖项,因此可以将其发送给从命令行使用它的任何人。
我的罐子的结构是:
├── META-INF
│ ├── MANIFEST.MF
│ └── maven
│ └── com.cn.xing.jar
│ └── CnJar
│ ├── pom.properties
│ └── pom.xml
├── com
│ └── cn
│ └── xing
│ └── jar
│ ├── HelloJava.class
│ └── InjectEntry.class
├── lib
│ ├── aopalliance-1.0.jar
│ ├── commons-codec-1.6.jar
│ ├── commons-logging-1.1.3.jar
│ ├── gson-2.2.4.jar
│ ├── httpclient-4.3.5.jar
│ ├── httpcore-4.3.2.jar
│ ├── spring-aop-3.2.2.RELEASE.jar
│ ├── spring-beans-3.2.2.RELEASE.jar
│ ├── spring-context-3.2.2.RELEASE.jar
│ ├── spring-core-3.2.2.RELEASE.jar
│ └── spring-expression-3.2.2.RELEASE.jar
└── spring
└── appCtx.xml
,清单文件内容为:
Manifest-Version: 1.0
Built-By: ChinaXing
Build-Jdk: 1.7.0_65
Class-Path: lib/spring-context-3.2.2.RELEASE.jar lib/spring-aop-3.2.2.
RELEASE.jar lib/aopalliance-1.0.jar lib/spring-beans-3.2.2.RELEASE.ja
r lib/spring-core-3.2.2.RELEASE.jar lib/spring-expression-3.2.2.RELEA
SE.jar lib/httpclient-4.3.5.jar lib/httpcore-4.3.2.jar lib/commons-lo
gging-1.1.3.jar lib/commons-codec-1.6.jar lib/gson-2.2.4.jar
Created-By: Apache Maven 3.2.2
Main-Class: com.cn.xing.jar.InjectEntry
Archiver-Version: Plexus Archiver
但是当我在命令行中执行它时如下:
java -jar ../CnJar-1.0-SNAPSHOT.jar
它显示了例外情况:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getMethod0(Class.java:2937)
at java.lang.Class.getMethod(Class.java:1771)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
这意味着无法找到Spring jar。
所以我想知道如何让Jvm classLoader在我的jar lib
dir中找到jar?
答案 0 :(得分:1)
默认类加载器不会处理这种情况。你可以: