I have a java web app, which I am using cloud9 for, and I have set up Apache freemarker as a templating engine. To import the package I used gradle, but when I run gradle build
, I get exceptions due to references to the freemarker package in my code. When I comment out all uses of freemarker aside from the import statement, my gradle will build my code, but it will throw errors when I run it (freemarker package does not exist). How can I fix this?
App.java
import freemarker.template.*;
import com.sun.net.httpserver.HttpServer;
import java.net.*;
import java.util.*;
public class App{
public static Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);
int port;
public static void main(String[] args){
App app = new App(8081);
try{
app.init();
app.templateConfig();
}catch(Exception e){
System.out.println(e);
}
}
public App(int port){
this.port = port;
}
public void init() throws Exception{
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
System.out.println("server started at " + port);
server.createContext("/", new HomeController.Index());
server.createContext("/echoHeader", new HomeController.EchoHeaderHandler());
server.createContext("/echoGet", new HomeController.EchoGetHandler());
server.createContext("/echoPost", new HomeController.EchoPostHandler());
server.setExecutor(null);
server.start();
}
public void templateConfig() throws Exception{
cfg.setDirectoryForTemplateLoading(new File("../templates"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHanlder.HTML_DEBUG_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
}
}
HomeController.java
import freemarker.template.*;
import com.sun.net.httpserver.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class HomeController{
public static class Index implements HttpHandler{
public void handle(HttpExchange exchange) throws IOException{
//String response = "<h1>Server start success if you see this message</h1><h1>Port: " + 8081 + "</h1>";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
//os.write(response.getBytes());
Map<String, Object> root = new HashMap<>();
root.put("port", 3000);
Template home = App.cfg.getTemplate("home.ftlh");
home.process(root, os);
os.close();
}
}
public static class EchoHeaderHandler implements HttpHandler{
public void handle(HttpExchange exchange){
}
}
public static class EchoGetHandler implements HttpHandler{
public void handle(HttpExchange exchange){
}
}
public static class EchoPostHandler implements HttpHandler{
public void handle(HttpExchange exchange){
}
}
}
build.gradle
repositories {
mavenCentral()
}
apply plugin: "java"
dependencies {
compile "org.freemarker:freemarker:2.3.28"
}
sourceSets {
main.java.srcDir "src/main"
}
jar {
from configurations.compile.collect { zipTree it }
manifest.attributes "Main-Class":"com.isaackrementsov.app.App"
}
My directory structure
workspace
build
classes
java
main
//Source code compiles here
libs
tmp
compileJava
jar
src
main
com
isaackrementsov
app
//Source code here
My whole project is here: Cloud9 Project