2013-08-10 5 views
0

임베디드 부두를 jar 파일로 압축하려고합니다. 하지만 항상 내 "집"jar dir에 액세스 할 수 없습니다. html 파일은 어디에 위치해 있습니다. 여기에 내 런처 파일이 있는데, 클래스 로더 리소스 경로를 사용하려고했지만 null이 항상 있습니다.임베디드 부두가있는 컨텍스트 경로를 설정할 수 없습니다.

내 항아리에 경로를 가져 오는 몇 가지 해결책이 있지만 웹 페이지의 jar 파일에 액세스해야합니다.

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); 

누군가 내 항아리에 올바른 경로를 설정하는 방법을 알고 있다면 도와주세요.

답변

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 
}