无法在fat / uber jar中找到或加载主类

时间:2017-11-29 13:34:08

标签: java ant executable-jar

我有java项目,它有spring依赖项。我正在通过ANT构建一个jar文件,如下所示

<jar jarfile="Test.jar">
    <manifest>
        <attribute name="Main-Class" value="com.karthik.EntryPoint"/>
    </manifest> 
    <fileset dir="${build.classes.dir}" includes="**" />
</jar>

当我运行命令 java -jar Test.jar 时。我收到错误,如下所示

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

为了解决这个问题,我创建了一个胖/超级jar,它具有所有Spring依赖关系,如下所示:

<jar jarfile="Test.jar">    
     <zipgroupfileset dir="${build.dir}/lib" includes="spring*.jar" />  

     <manifest>
         <attribute name="Main-Class" value="com.karthik.EntryPoint"/>
     </manifest>    

     <fileset dir="${build.classes.dir}" includes="**" />
     <fileset dir="${src.java.dir}" includes="**/*.java" />
</jar>

我现在可以看到Test.jar中捆绑的Spring类。
但是当我运行命令 java -jar Test.jar 时,我收到一个错误,如下所示

Error: Could not find or load main class com.karthik.EntryPoint

为什么我只在uber / fat jar中收到此错误,我们如何解决?

0 个答案:

没有答案