Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Intro to Java Web Development with Spark Bells and Whistles Request Handled

Edvards Valbahs
PLUS
Edvards Valbahs
Courses Plus Student 23,881 Points

How to deploy/host java spark app for example on AWS?

it is very interesting web framework, but I would like to see some step by step guide: How to host some java spark app on server side :). Thanks

1 Answer

Rob Mount
Rob Mount
8,658 Points

Edvards.

This may be coming very late, but here it goes. I can't speak towards AWS, but Spark has an embedded web-server called Jetty in order to host. Your application will simply have to include all of your dependencies (the .jar files that Gradle pulled in for you) in the class path. Once you launch your packaged .jar (using command: java -jar app.jar), the embedded Jetty server just starts up. Alternatively, there's a concept called "super jar" or "fat jar" where you can write a task for Gradle to incorporate all of your dependencies into a single .jar file so that you don't ever have to worry about class path and so you can just build and run the .jar file wherever you want your web-server to be without having to worry about an actual web-server with appropriate extensions installed locally (again, thanks to embedded Jetty).

here is a sample Gradle task i found for a fat jar you can add to your project's build.gradle file:

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
                'Implementation-Version': version,
                'Main-Class': 'com.teamtreehouse.courses.Main'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

If you don't want to use Jetty, Spark can also be deployed into a Tomcat environment. I don't know if TreeHouse will allow a reference to a third-party site, but here's a tutorial that can help you. About half-way down the page there is information on deploying Spark to Tomcat. link

Good luck. Spark is Awesome!!!