我試圖用http://www.sparkjava.com/這個小型的Java Web框架啓動。這些指令告訴你將它作爲一個Maven依賴項添加(完成),但當我mvn package
時,我得到一個沒有爲spark/Route找到的類def。Spark Java和類路徑
我認爲這是來自Spark類不在我的類路徑。我如何添加它?它會在pom.xml
?
編輯:對不起,這是我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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bernsteinbear.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
編輯:跟蹤
λ chaos myapp → java -cp target/myapp-1.0-SNAPSHOT.jar com.bernsteinbear.myapp.App
Exception in thread "main" java.lang.NoClassDefFoundError: spark/Route
Caused by: java.lang.ClassNotFoundException: spark.Route
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
aaaand的來源(例如,從主頁):
λ chaos myapp → cat src/main/java/com/bernsteinbear/myapp/App.java
/**
* Hello world!
*
*/
package com.bernsteinbear.myapp;
import spark.*;
import static spark.Spark.*;
public class App {
public static void main(String[] args) {
get(new Route("/hello") {
@Override
public Object handle(Request request, Response response) {
return "Hello World!";
}
});
}
}
請分享加入你的pom.xml – nkukhar
@NKukhar! – tekknolagi
pom.xml從Maven的角度看起來很正確(假設Spark本身沒有錯誤)。你能分享堆棧跟蹤和你的一些源代碼嗎?你有沒有嘗試過來源的例子? – Will