可以运行jar文件。 Maven构建后出现JNI错误。类路径可以被破坏

时间:2018-08-26 20:21:00

标签: java maven intellij-idea classpath

我正在努力运行用maven创建的.jar文件。 当我在InteliJ IDE中运行项目时,它可以很好地运行,但是当我使用maven进行构建时,则无法运行它。

用于运行运行的命令:

  

java -cp target \ xmpp-server.jar com.wedevol.xmpp.server.EntryPoint

运行输出:

c:\Repos\remindanyoneserverapp>java -cp target\xmpp-server.jar com.wedevol.xmpp.server.EntryPoint
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/jivesoftware/smack/XMPPException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.jivesoftware.smack.XMPPException
        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 more

Maven comand“ mvn package”输出:

c:\Repos\remindanyoneserverapp>mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.wedevol.xmpp.server:xmppserver2 >-----------------
[INFO] Building FCM XMPP Server 0.0.4
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xmppserver2 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory c:\Repos\remindanyoneserverapp\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ xmppserver2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xmppserver2 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory c:\Repos\remindanyoneserverapp\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ xmppserver2 ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmppserver2 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ xmppserver2 ---
[INFO] Building jar: c:\Repos\remindanyoneserverapp\target\xmpp-server.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.312 s
[INFO] Finished at: 2018-08-26T21:32:37+02:00
[INFO] ------------------------------------------------------------------------

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>com.wedevol.xmpp.server</groupId>
	<artifactId>xmppserver2</artifactId>
	<version>0.0.4</version>
	<packaging>jar</packaging>
	<name>FCM XMPP Server</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<smack.version>4.2.3</smack.version>
		<json-simple.version>1.1.1</json-simple.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.igniterealtime.smack</groupId>
			<artifactId>smack-java7</artifactId>
			<version>${smack.version}</version>
		</dependency>
		<dependency>
			<groupId>org.igniterealtime.smack</groupId>
			<artifactId>smack-tcp</artifactId>
			<version>${smack.version}</version>
		</dependency>
		<dependency>
			<groupId>org.igniterealtime.smack</groupId>
			<artifactId>smack-extensions</artifactId>
			<version>${smack.version}</version>
		</dependency>
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
			<version>${json-simple.version}</version>
		</dependency>
		<dependency>
  			<groupId>com.google.firebase</groupId>
  			<artifactId>firebase-admin</artifactId>
  			<version>5.9.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>
	</dependencies>

	<build>
		<finalName>xmpp-server</finalName>
		<plugins>
			<!-- Set a JDK compiler level -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>

		</plugins>
	</build>
</project>

我想这是类路径的问题。我试图获取用IDE生成的类路径,但是我找不到它。 在这种情况下,请提供任何建议以如何从类路径开始。 也许可以更好地配置Maven以将所有依赖项构建到jar中。

1 个答案:

答案 0 :(得分:0)

pom.xml必须更新以下内容:

a
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>