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.63k stars 1.56k forks source link

Importing not working #1003

Closed Horkyze closed 5 years ago

Horkyze commented 6 years ago

I've imported dependency into pom.xml:

<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>2.7.2</version>
</dependency>

Ran mvn clean && mvn compile then ant to build. However it says the dependency cannot by found:

> ant        
Buildfile: /home/me/floodlight/build.xml

init:
    [mkdir] Created dir: /home/me/floodlight/target/bin
    [mkdir] Created dir: /home/me/floodlight/target/bin-test
    [mkdir] Created dir: /home/me/floodlight/target/lib
    [mkdir] Created dir: /home/me/floodlight/target/test

compile:
    [javac] Compiling 527 source files to /home/me/floodlight/target/bin
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.8
    [javac] /home/me/floodlight/src/main/java/net/floodlightcontroller/core/Main.java:31: error: package spark does not exist
    [javac] import static spark.Spark.*;
    [javac]                    ^
    [javac] /home/me/floodlight/src/main/java/net/floodlightcontroller/core/Main.java:45: error: cannot find symbol
    [javac]         get("/hello", (req, res) -> "Hello World");
    [javac]         ^
    [javac]   symbol:   method get(String,(req,res)-[...]orld")
    [javac]   location: class Main
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 2 errors
    [javac] 1 warning

BUILD FAILED
/home/me/floodlight/build.xml:144: Compile failed; see the compiler error output for details.

Total time: 6 seconds

Any idea what might be wrong?

jakaarl commented 6 years ago

Uh, what exactly are you using Maven and Ant for? My recommendation is to use one, not both (preferably Maven).

Horkyze commented 6 years ago

Ok, thanks. I managed to add spark by adding the jar file into build.xml: <include name="spark-core-2.7.2.jar"/> now the ant compile with ant works file.

However when I tried to run hello world example byt adding this:

import static spark.Spark.*;
...
get("/hello", (req, res) -> "Hello World");

An exeption is raised. Looks like there is a problem with running the embeded jetty server. Is there any other dependency I need to import?

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: javax/servlet/Filter
    at spark.embeddedserver.EmbeddedServers.initialize(EmbeddedServers.java:40)
    at spark.Service.lambda$init$2(Service.java:536)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more
jakaarl commented 6 years ago

I suggest you try using only Maven, see: http://sparkjava.com/tutorials/maven-setup Couldn't spot a clear "deploying Spark guide", but the deployment instructions for Heroku have a nice example of using maven-assembly-plugin to assemble a JAR including dependencies: http://sparkjava.com/tutorials/heroku

Horkyze commented 6 years ago

Thanks, I managed to make to work now :) If you are interested https://github.com/MWGA/floodlight/commit/a601d1897fc39263dbbf7461d08b537539a8f2a9 is the current state that works.

I compile with mvn install