0

I try to pack embedded jetty into jar file. But all the time i can't access to my "home" jar dir, where is all html file situated. Here is my launcher file, i tried to use classloader resource path but all the time it is null.

I found several solutions for get path to my jar but I need to access files in jar for example my web pages.

    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(8080);
    server.addConnector(connector);
    ClassLoader cl = Launcher.class.getClassLoader();
    System.out.println(cl);
    URL f = cl.getResource("/index.html");
    System.out.println(f);
    String webDir = f==null ? "/" : f.toExternalForm();
    System.out.println(webDir);
    WebAppContext context = new WebAppContext("/", "/");
    context.setResourceBase(webDir);

If someone know how to set correct path into my jar, please help me.

Jan
  • 1,004
  • 6
  • 23
Andrew
  • 266
  • 6
  • 18

1 Answers1

0

You might want to do it this way ...

public static void main(String[] args) throws Exception
{
    Server server = new Server();

    Connector connector = new SelectChannelConnector();
    connector.setPort(8080);
    // Use only 1 connector
    server.setConnectors(new Connector[] { connector });

    // Figure out what path to serve content from
    ClassLoader cl = Launcher.class.getClassLoader();
    URL f = cl.getResource("/index.html");
    String webDir = System.getProperty("user.dir");
    if (f != null)
    {
        webDir = f.toExternalForm();
    }

    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setResourceBase(webDir);

    server.setHandler(webapp); // add webapp to server

    server.start(); // start server on its own thread
    server.join();  // wait for server thread to stop
}
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136