0

So I'm confused - When I run my Application from the IDE (Eclipse) it works perfectly - BUT when I run its jar (java -jar myjar.jar) it throws following Exceptions: I Use Eclipselink to persist my data.

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

Here is my pom.xml file:

<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>

Does Anyone have any advice or help?

gala_m
  • 37
  • 8
  • Are you using maven? – Gnk Oct 15 '18 at 10:56
  • yes, I'm using maven – gala_m Oct 15 '18 at 11:24
  • So probably you will have a target folder where the executable jar is placed and that is where you run java -jar jarname.jar. Are all the jars that are in the `class-path` line of the MANIFEST.MF file also present in the target folder? – Gimby Oct 15 '18 at 11:47
  • Hey @gimbly - thank yo for your answer - you're right, my jar is in the target folder and i run it from there - no, there ist no jar in the target file except my own... – gala_m Oct 15 '18 at 12:04
  • Check this answer as well: https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven – user987339 Oct 15 '18 at 12:42

1 Answers1

0

Use this plugin to generate your jar with all dependencies, and run the jar-with-dependencies

And change 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>

And if you don't want all libs inside your jar one solution is to add to classpath when you make the call.

Put your jar and all dependencies jars inside \path_to_jars\

java -classpath \path_to_jars\*.jar your.package.YourMainClass
Gnk
  • 645
  • 4
  • 11
  • Hey @Gnk - it worked now, great!! but the jar file is a lot bigger now!! from 5MB to like 50MB - whats the difference (I guess its all the dependencies) - is there no other way?? – gala_m Oct 15 '18 at 12:59