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.

4

1 回答 1

0

你可能想这样做......

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
}
于 2013-08-12T17:28:53.647 回答