Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static content with grizzly's StaticHttpHandler from within a .jar

I want Grizzly to serve static files from within a .jar, which contains a JAX-RS application, Grizzly and all other libs. I'm using org.glassfish.grizzly.http.server.StaticHttpHandler to serve the static files.

public class Main {

  // ...

  public static void main(String[] args) throws IOException, URISyntaxException {
    final HttpServer server = startServer();
    server.getServerConfiguration().addHttpHandler(
            new StaticHttpHandler(getTemplatePath()), "/static");
    System.in.read();
    server.stop();
  }

  private static String getTemplatePath() throws URISyntaxException {
    return Main.class.getClassLoader().getResource("templates/static").getPath();
  }
}

But it works only, if I start the application with my IDE. If I pack a .jar and run it, the static files are not found.

Is it possible to serve static files from within a .jar with grizzly's StaticHttpHandler? And how?

like image 326
deamon Avatar asked Jun 06 '13 12:06

deamon


1 Answers

You have to use CLStaticHttpHandler instead of StaticHttpHandler.

Please take a look at this question html server grizzly+jersey (.html from .jar archive)

like image 64
alexey Avatar answered Nov 07 '22 16:11

alexey