我的pom文件:
<?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>
当我尝试通过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
我有什么要添加到pom文件以使该项目可执行?当我在netbeans中编译并运行它时,它工作,但当我尝试使用jar文件时,它不起作用。
@edit 仍然没有解决方案
@edit 我开始赏金..
答案 0 :(得分:2)
您的问题是类路径。您在.pom文件中指定的内容只是构建项目所需的依赖项。然而,实际运行生成的.jar是另一回事,.pom没有权力。
它适用于NetBeans IDE,因为它可以跟踪您添加到项目中的依赖关系,并在尝试运行时将它们添加到类路径中。您的cmd命令不会复制该命令。
这个问题有两种解决方案:
-cp
参数第二个解决方案在理论上听起来好得多,并且可以在Maven中实现(例如)OneJar或Maven Shade Plugin
从我的一个项目配置Maven Shade Plugin
:
<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>
答案 1 :(得分:0)
将其添加到POM中的依赖项列表中:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
似乎你的NetBeans IDE在其类路径中已经有了这个JAR,这可能意味着你已经在你的计算机上的某个地方安装了这个JAR。