【问题标题】:How to put all required jars into a one final jar?如何将所有需要的罐子放入一个最终罐子中?
【发布时间】:2016-06-23 09:43:42
【问题描述】:

我在 lib 文件夹中创建了一个包含依赖 jar 文件的 jar 文件。但是当我尝试将创建的 jar 用作库时,它会显示为“线程中的异常”main“java.lang.NoClassDefFoundError”。

谁能帮我创建一个带有依赖 jar 的 jar?

提前致谢。

这是我的 pom.xml: `

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <properties>
      <jclouds.version>1.9.2</jclouds.version>
   </properties>
   <groupId>org.apache.jclouds.examples</groupId>
   <artifactId>openstack-examples</artifactId>
   <version>1.0</version>
   <build>
      <plugins>
         <!-- any other plugins -->
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.1</version>
            <configuration>
               <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id>
                  <!-- this is used for inheritance merges -->
                  <phase>package</phase>
                  <!-- append to the packaging phase. -->
                  <goals>
                     <goal>attached</goal>
                     <!-- goals == mojos -->
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
</project>

这是我的 jar 文件中 MANIFEST 的内容:

Manifest-Version: 1.0 
Archiver-Version: Plexus Archiver
Built-By: root
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_25 

Here I add the image for structure of my jar file.

【问题讨论】:

  • 你使用maven吗?使用 maven 很容易
  • 你需要把你的lib jars 放在类路径中
  • 你可以看到它。 youtube.com/watch?v=BsndjGOc1w4
  • 谢谢。是的,我已经尝试过使用 maven。但是使用 maven,最终的 jar 包括所有依赖的 jar,而没有与主 jar 相关的类文件。
  • 您的清单中没有任何主类,因此您无法启动您的 jar

标签: java maven jar stack-trace


【解决方案1】:

您可以使用maven shade jar 创建一个超级jar。

或者,您可以指定包含 JARS 的文件夹的路径,而不是创建 1 个 jar。参考:-Run a JAR file from the command line and specify classpath

java -cp "jar_name.jar;libs/*" com.test.App 之类的东西应该可以工作。

【讨论】:

  • 我必须创建一个库 jar 而不是可运行的 jar。我的问题是我有四个类和 20 个依赖 jar 文件用于这 4 个类文件,我必须创建一个包含我的 jar 文件4个类和依赖的jar。然后我的jar文件将用作库jar而不是可运行的jar。谢谢你
猜你喜欢
  • 2012-12-28
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
  • 2016-01-19
  • 2011-05-14
相关资源
最近更新 更多