如果从jar运行,则带有嵌入式码头的Spring应用程序找不到webdefault.xml. [英] Spring application with embedded jetty can't find webdefault.xml if running from jar

查看:90
本文介绍了如果从jar运行,则带有嵌入式码头的Spring应用程序找不到webdefault.xml.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用嵌入式Jetty实例的spring应用程序.

I have spring application which uses embedded Jetty instance.

project
   | src
      | controller
      | webapps
          | jsp
          | WEB-INF
              | web.xml
              | applicationContext.xml
              | spring-servlet.xml

我的jar具有相同的树形结构,但我不断得到

my jar has the same tree structure but I keep getting

    d:\test>java -jar springtest.jar
2011-11-22 15:37:02.576:INFO::jetty-7.x.y-SNAPSHOT
2011-11-22 15:37:02.686:WARN::Failed startup of context o.e.j.w.WebAppContext{/,[file:/C:/Users/me/AppData/Local/Temp/jetty-0.0.0.0-8080-webapps-_-any-/webinf
/, jar:file:/d:/test/springtest.jar!/org/jcvi/webapps/]}
java.io.FileNotFoundException: d:\test\org\eclipse\jetty\webapp\webdefault.xml (The system cannot find
the path specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
        at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
        at org.eclipse.jetty.xml.XmlParser.parse(XmlParser.java:188)
        at org.eclipse.jetty.xml.XmlParser.parse(XmlParser.java:204)
        at org.eclipse.jetty.webapp.Descriptor.parse(Descriptor.java:60)
        at org.eclipse.jetty.webapp.WebDescriptor.parse(WebDescriptor.java:140)
        at org.eclipse.jetty.webapp.MetaData.setDefaults(MetaData.java:141)
        at org.eclipse.jetty.webapp.WebXmlConfiguration.preConfigure(WebXmlConfiguration.java:46)
        at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:412)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
        at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
        at org.eclipse.jetty.server.Server.doStart(Server.java:258)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
        at org.jcvi.ServerRunner.startServer(ServerRunner.java:83)
        at org.jcvi.MainServer.main(MainServer.java:18)
2011-11-22 15:37:02.748:INFO::Started SelectChannelConnector@0.0.0.0:8080 STARTING

我有以下运行码头服务器实例的java类

I have following java class which runs jetty server instance

String webDir = this.getClass().getClassLoader().getResource("webapps").toExternalForm();
Server server = new Server(8080);

WebAppContext context = new WebAppContext();
context.setContextPath("/");
context.setResourceBase(webDir);
context.setParentLoaderPriority(true);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
server.setHandler(context);
server.start();

我的web.xml看起来像

my web.xml looks like

<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>spring</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

如果我在IDE中运行,此应用程序可以正常运行,但JAR失败. 如何解决此问题,以便我可以拥有其中包含Web应用程序的单个jar文件?

this application runs fine if I run inside IDE, but it fails with JAR. How can I resolve this issue so that I can have single jar file which has the web application in it?

推荐答案

我遇到了类似的问题,并通过以下主类实现对其进行了解决:

I had a similar problem and I solve it with this main class implementation:

private static final int PORT = 8080;
private static final String WAR_LOCATION = "src/webapps"; //in your case I guess
private static final String CONTEXT_PATH = "/movence"; //change it if you want

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

    setupConnector(connector);
    setupContext(server, context);
    setupServer(server, context, connector);
    startServer(server);
}

private static void startServer(Server server) throws Exception, InterruptedException {
    server.start();
    server.join();
}

private static void setupServer(Server server, WebAppContext context, SocketConnector connector) {
    server.setConnectors(new Connector[] { connector });
    server.addHandler(context);
}

private static void setupConnector(SocketConnector connector) {
    connector.setPort(PORT);
}

private static void setupContext(Server server, WebAppContext context) {
    context.setServer(server);
    context.setContextPath(CONTEXT_PATH);
    context.setWar(WAR_LOCATION);
}

这篇关于如果从jar运行,则带有嵌入式码头的Spring应用程序找不到webdefault.xml.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆