perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.65k stars 1.56k forks source link

Newb question - not a good start #919

Closed ORESoftware closed 7 years ago

ORESoftware commented 7 years ago

I run the follow commands

mvn package # works fine, build successful
java -cp target/cdt-hive-1.0-SNAPSHOT.jar cdt.App  # causes error below
$ java -cp target/cdt-hive-1.0-SNAPSHOT.jar cdt.App
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: spark/Request
        at cdt.App.main(App.java:8)
Caused by: java.lang.NoClassDefFoundError: spark/Request
        ... 1 more
Caused by: 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

here is the code I used:

package cdt;

import static spark.Spark.*;

public class App {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");
    }
}

and my pom.xml file:

<project>

    <modelVersion>4.0.0</modelVersion>
    <groupId>cdt</groupId>
    <artifactId>cdt-hive</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>cdt-hive</name>
    <url>http://maven.apache.org</url>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

</project>
jakaarl commented 7 years ago

You're not packaging dependencies in the JAR.

I suggest using Maven assembly plugin's jar-with-dependencies, see: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html Also, on the same page, see "Creating an Executable JAR". It's handy.

ORESoftware commented 7 years ago

ok thanks appreciated