Java Maven生成清单不包含类路径。

Java Maven生成清单不包含类路径。,java,maven,classpath,manifest,runnable,Java,Maven,Classpath,Manifest,Runnable,我正试图构建一个包含我的类和依赖项的可运行jar,在我成功创建文件后,我陷入了困境。当我执行jar时: java -jar Amp60-distribution.jar 我收到以下错误消息: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger at java.lang.Class.getDeclaredMethods0(Native Method) at java.

我正试图构建一个包含我的类和依赖项的可运行jar,在我成功创建文件后,我陷入了困境。当我执行jar时:

java -jar Amp60-distribution.jar
我收到以下错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
    at java.lang.Class.getMethod0(Class.java:2764)
    at java.lang.Class.getMethod(Class.java:1653)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    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
我验证了MANIFEST.MF,但它不包含类路径:

Manifest-Version: 1.0
Built-By: jtormin
Build-Jdk: 1.7.0_45
Created-By: Apache Maven 3.0.4
Main-Class: gui/MainFrame
Archiver-Version: Plexus Archiver
My pom.xml:

    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <compress>true</compress>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>gui/MainFrame</mainClass>
                            <classpathLayoutType>custom</classpathLayoutType>
                            <customClasspathLayout>
                                $${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension}
                            </customClasspathLayout>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <finalName>Amp60</finalName>
                    <outputDirectory>target</outputDirectory>
                    <descriptor>assembly/assembly.xml</descriptor>
                    <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>gui/MainFrame</mainClass>
                    </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.15</version>
        </dependency>
        <dependency>
            <groupId>jakarta-regexp</groupId>
            <artifactId>jakarta-regexp</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
    <organization>
        <name>ISP</name>
    </organization>
</project>

src
资源
maven jar插件
2.4
假的
真的
真的
图形用户界面/大型机
习俗
$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension}
maven编译器插件
2.4
1.7
1.7
org.apache.maven.plugins
maven汇编插件
2.4
Amp60
目标
assembly/assembly.xml
真的
图形用户界面/大型机
包裹
单一的
net.sf.opencsv
opencsv
2.3
log4j
log4j
1.2.17
罐子
乔达时间
乔达时间
2.1
公地io
公地io
2.4
org.apache.commons
commons-math3
3.2
org.jfree
柱状图
1.0.15
雅加达正则表达式
雅加达正则表达式
1.4
网络服务提供商
assembly.xml文件:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4">
    <id>distribution</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>target/classes/</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>resources</directory>
            <outputDirectory>resources</outputDirectory>
            <includes>
                <include>**</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <useTransitiveDependencies>true</useTransitiveDependencies>
        </dependencySet>
    </dependencySets>
</assembly>

分布
罐子
假的
目标/类别/
/
**
资源
资源
**
/
假的
真的

我认为有两种更优雅、更简单的方法可以实现您的目标——构建一个包含所有依赖项的JAR。至少有一个名为
assembly.xml
的额外文件让我感到困惑。尝试删除
assembly.xml
maven jar插件
,并使用以下方法:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>gui.MainFrame</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>
此外,您还需要明确指定
一个jar插件的存储库

<pluginRepositories>
    <pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
    </pluginRepository>
</pluginRepositories>

onejar-maven-plugin.googlecode.com
http://onejar-maven-plugin.googlecode.com/svn/mavenrepo

接受的答案是否有帮助?谢谢,但不起作用。我是马文的新手,但我认为这两种情况都有点不同。在本例中,Maven没有生成类路径。尽管如此,我还是尝试了解决方案,eclipse报告了以下问题:
在org.apache.maven.archiver.ManifestConfiguration中找不到“manifestEntries”的setter、adder或field
谢谢,WelcomeTo。我使用了第二种解决方案(onejar),它成功了。唯一的问题是它无法加载资源并在线程“main”java.lang.reflect.InvocationTargetException中启动异常:
异常,原因是:gui.MainFrame.main(MainFrame.java:145)处的java.lang.NullPointerException
此异常是由以下代码行引起的:
InputStream logFileConfiguration=ClassLoader.getSystemResourceAsStream(“conf/log4j.properties”)
conf目录和log4j文件在jar中,但我仍然不明白它为什么找不到它们。@user1637370尝试用以下代码替换您的代码:
Thread.currentThread().getContextClassLoader().getResourceAsStream(“log4j.xml”)
,或者如果它不起作用,请尝试以下命令:
SomeYourClass.class.getClassLoader().getResourceAsStream(“log4j.xml”)
它不起作用,但我找到了解决方案:InputStream logFileConfiguration=MainFrame.class.getResourceAsStream(“/conf/log4j.properties”)`
<pluginRepositories>
    <pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
    </pluginRepository>
</pluginRepositories>