尽管NetBeans从IDE运行,但由NetBeans生成的jar未运行

时间:2016-12-02 22:15:01

标签: java spring maven netbeans

这是我的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>
<groupId>com.mycompany</groupId>
<artifactId>TimeTrackerWithMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <java.version>1.8</java.version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.mycompany.timetrackerwithmaven.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.4.201502262128</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.0.31-beta</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0-rc2</version>
    </dependency>
    <dependency>
        <groupId>com.google.collections</groupId>
        <artifactId>google-collections</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>platform</artifactId>
        <version>3.5.2</version>
    </dependency>
</dependencies>
<name>TimeTrackerWithMavenDevelopment</name>

这是我生成的jar中的MANIFEST.MF。

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Blackstorm
Class-Path: mockito-core-2.0.31-beta.jar byte-buddy-0.6.14.jar objenes
 is-2.1.jar commons-io-1.3.2.jar junit-4.12.jar hamcrest-core-1.3.jar 
 spring-core-4.2.4.RELEASE.jar commons-logging-1.2.jar spring-test-4.2
 .4.RELEASE.jar spring-beans-4.2.4.RELEASE.jar spring-context-4.2.4.RE
 LEASE.jar spring-aop-4.2.4.RELEASE.jar aopalliance-1.0.jar spring-exp
 ression-4.2.4.RELEASE.jar gson-2.4.jar guava-19.0-rc2.jar google-coll
 ections-1.0.jar jna-4.1.0.jar platform-3.5.2.jar
Created-By: Apache Maven 3.0.5
Build-Jdk: 1.8.0_101
Main-Class: com.mycompany.timetrackerwithmaven.Main

当我使用&#34; java -jar TimeTrackerWithMaven-1.0-SNAPSHOT.jar&#34;运行我的代码时我收到以下错误。

  

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)           在java.lang.Class.getMethod0(未知来源)           在java.lang.Class.getMethod(未知来源)           at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)           在sun.launcher.LauncherHelper.checkAndLoadMain(未知来源)   引起: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个   错误:发生JNI错误,请检查您的安装并重试   线程&#34; main&#34;

中的例外情况

1 个答案:

答案 0 :(得分:1)

您的输出jar没有包含必要的库,并且在类路径上找不到依赖项。要解决这个问题,要么将它们包含在类路径中(在您的情况下,将它们放在与输出jar相同的文件夹中,可能会有一些插件来帮助自动化,虽然这看起来不像是一个想法,你的文件夹将是受到大量依赖性jar的污染,使其不受组织限制)或使用maven-shade-plugin将这些依赖项合并到输出jar中。