看完有關maven的視頻教程後,我找到了解決方案。 首先,您需要將以下添加到xml.pom文件在你的Maven項目在Eclipse:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<!-- This tells Maven to include all dependencies -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>chatapp.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
它告訴行家在哪裏可以找到主類是很重要的。在我的例子中,我命名了我的應用程序chatapp(因爲我打算將這個簡單的'Hello World'應用程序發展爲一個聊天應用程序),包含main方法的Main類也被命名爲Main,它位於包chatapp下,因此它是chatapp.Main。您可以在<dependencies>
節點後面添加這些行。因此,POM文件應該是這樣的:
<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.orboan</groupId>
<artifactId>chatapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>new-spark-project</name>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<!-- This tells Maven to include all dependencies -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>chatapp.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
現在,重要的是不要使用Eclipse的導出工具(右鍵單擊該項目在Package Explorer,並從上下文菜單中不選擇「導出」),就像您想要將java項目導出到jar文件以進行部署時一樣。這將生成一個jar文件,但不需要所有的依賴關係。
相反,你必須選擇(從上下文菜單)Run As> Maven Install。這將生成(從pom.xml)相應的jar文件,包含所有需要的依賴關係,在我的例子中爲chatapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar
。
然後,將該jar文件上傳到CentOS服務器,並使用java -jar
chatapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar
運行它。它現在應該工作。
在服務器上運行'java -jar appname.jar'的輸出是什麼? – Casey
'在線程中發生異常「main」java.lang.BootstrapMethodError:java.lang.NoClassDefFoundError:spark/Request at chatapp.Main.main(Main.java:8) 引發:java.lang.NoClassDefFoundError:spark/Request ... 1更多 引發:java.lang.ClassNotFoundException:spark.Request at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424 ) at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more' –
因此,它看起來不是罐子包含Spark庫,如我所說。 –