我正在尝试从小型Java Web框架http://www.sparkjava.com/开始。指令告诉您将其添加为Maven依赖项(完成),但是当我mvn package时,我得到的spark/Route找不到类def。

我认为这是因为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)

aaa和源(来自主页的示例):
λ 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!";
        }
        });

    }

}

最佳答案

对我来说有效的方法是:

mvn package
mvn exec:java -Dexec.mainClass="com.your.class.with.main.method"

09-27 01:49