Manifest in pom file - NoClassDefFoundError

My pom file:

<?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>pl.xx</groupId>
    <artifactId>kwestionariusz</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc41</version>
        </dependency>
        <dependency>
            <groupId>unknown.binary</groupId>
            <artifactId>postgresql-9.2-1002.jdbc4</artifactId>
            <version>SNAPSHOT</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <build>
       <plugins>
        <plugin>
    <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
        <mainClass>pl.xx.kwestionariusz.gui.Kwestionariusz</mainClass>
        </manifest>
      </archive>
    </configuration>
    </plugin>
    </plugins>  
</build>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
</project>

      

the error I get when I try to run it via cmd:

\NetBeansProjects\kwestionariusz\target>java -jar kwestionariusz-1.0.jar



Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/hiber
nate/service/ServiceRegistry
        at pl.xx.kwestionariusz.dao.ListaZainteresowanDao.wyszukajZaintereso
wania(ListaZainteresowanDao.java:31)
        at pl.xx.kwestionariusz.gui.Kwestionariusz$2.<init>(Kwestionariusz.j
ava:110)
        at pl.xx.kwestionariusz.gui.Kwestionariusz.initComponents(Kwestionar
iusz.java:106)
        at pl.xx.kwestionariusz.gui.Kwestionariusz.<init>(Kwestionariusz.jav
a:33)
        at pl.xx.kwestionariusz.gui.Kwestionariusz$5.run(Kwestionariusz.java
:275)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegist
ry
        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)
        ... 19 more

      

enter image description here

What do I need to add to the pom file to make this project executable? It works when I compile and run it in netbeans, but doesn't work when I try to use the jar file.

@edit no solution yet

@edit im starting reward.

+3


source to share


2 answers


Your problem is the classpath. What you have specified in the .pom file is just the dependencies needed to create the project. However, actually running the resulting .jar is a different matter and pp doesn't have any cardinality there.

It works in the NetBeans IDE because, like any good IDE, it keeps track of the dependencies you added to the project and adds them to the classpath when you try to run. Your cmd command doesn't replicate this.

There are two solutions to this problem:

  • Adding whatever is required for the classpath. This is probably due to the creation of a .bat file containing the path to each required library using the argument-cp

  • Create your .jar file to contain all the required dependencies.


Solution number two sounds much better in theory and can be achieved in Maven with (for example) OneJar or the Maven Shade Plugin

Configuration Maven Shade Plugin

from one of my projects:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <manifestEntries>
                        <Main-Class>my.package.tree.Main</Main-Class>
                    </manifestEntries>
                </transformer>
            </transformers>
            <filters>
                <filter>
                    <!-- Exclude files that sign a jar (one or multiple of the dependencies). 
                        One may not repack a signed jar without this, or you will get a SecurityException 
                        at program start. -->
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.RSA</exclude>
                        <exclude>META-INF/*.INF</exclude> <!-- This one may not be required -->
                    </excludes>
                </filter>
            </filters>
        </configuration>
    </execution>
</executions>
</plugin>

      

+2


source


Add this to the list of dependencies in the POM:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.0.1.Final</version>
</dependency>

      



It seems that your NetBeans IDE already has this JAR in its classpath, which probably means that you already have this JAR somewhere on your machine.

0


source







All Articles