java.lang.NoClassDefFoundError:javax / persistence / NoResultException

时间:2018-10-15 10:50:38

标签: java jar eclipselink

所以我很困惑-当我从IDE(Eclipse)运行我的应用程序时,它工作得很好-但是,当我运行其jarjava -jar myjar.jar)时,它会引发以下异常: 我使用Eclipselink保留数据。

Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/NoResultException
        at ch.myapp.application.service.ServiceHelper.<init>(ServiceHelper.java:7)
        at ch.myapp.application.service.AdresseService.<init>(AdresseService.java:15)
        at ch.myapp.application.demo.TestHash.main(TestHash.java:51)
Caused by: java.lang.ClassNotFoundException: javax.persistence.NoResultException
        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)
        ... 3 more

这是我的pom.xml文件:

<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>ch.mygroupid</groupId>
  <artifactId>myartifactid</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <repositories>
    <repository>
        <id>oss.sonatype.org</id>
        <name>OSS Sonatype Staging</name>
        <url>https://oss.sonatype.org/content/groups/staging</url>
    </repository>
    <repository>
       <id>EclipseLink</id>
       <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
    </repository>
  </repositories>
  <build>
    <finalName>myapp</finalName>
    <!-- <sourceDirectory>src</sourceDirectory> -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>                
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>ch.myapp.application.demo.TestHash</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
      <plugin>
        <groupId>com.sebastian-daschner</groupId>
        <artifactId>jaxrs-analyzer-maven-plugin</artifactId>
        <version>0.16</version>
        <executions>
            <execution>
                <goals>
                    <goal>analyze-jaxrs</goal>
                </goals>
                <configuration>
                    <backend>swagger</backend>
                    <deployedDomain>localhost:8080</deployedDomain>
                    <resourcesDir>swagger</resourcesDir>
                    <renderSwaggerTags>true</renderSwaggerTags>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        com.sebastian-daschner
                                    </groupId>
                                    <artifactId>
                                        jaxrs-analyzer-maven-plugin
                                    </artifactId>
                                    <versionRange>[0.14,)</versionRange>
                                    <goals>
                                        <goal>analyze-jaxrs</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
        <!-- scope: test nur fürs testing! sonst gibts beim compilieren (mvn install) einen fehler -->
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
        <!-- 
        den scope nur für Testing benutzen
        <scope>test</scope>
        -->
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>commonj.sdo</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-http-server</artifactId>
        <version>2.3.30</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-http</artifactId>
    <version>2.25.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.20</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>2.44.0</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>
  </dependencies>   
  <properties>
     <project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
  </properties> 
</project>

有人有任何建议或帮助吗?

1 个答案:

答案 0 :(得分:0)

使用此插件生成具有所有依赖项的jar,然后运行jar-with-dependencies

并更改your.main.Class

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            your.main.Class
                        </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

如果您不希望jar中包含所有库,一种解决方法是在进行调用时将其添加到classpath中。

将您的jar和所有依赖项jar放入\ path_to_jars \

java -classpath \path_to_jars\*.jar your.package.YourMainClass