通过IntelliJ IDEA 13部署的Tomcat war返回错误

时间:2014-09-04 20:41:09

标签: java tomcat intellij-idea

我在IntelliJ中部署第一个WAR文件时出现以下错误。据推测,这是一个本地问题,因为这只是我们为测试设置而部署的WAR文件。

所有80个Maven测试都在通过,但当我浏览到Web应用程序时,我收到此错误:

java.lang.IllegalArgumentException: javax.naming.NameNotFoundException: Name [jdbc/ipam] is not bound in this Context. Unable to find [jdbc].
    com.myapp1.ipam.rest.JaxRsApplication.<init>(JaxRsApplication.java:26)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1104)
    org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:902)
    org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:872)
    org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:964)
    org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:956)
    org.glassfish.jersey.server.ApplicationHandler.createApplication(ApplicationHandler.java:336)
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:315)
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:285)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:310)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

这似乎是问题所在:

    public JaxRsApplication() {
    DataSource datasource;
    try {
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        datasource = (DataSource) envCtx.lookup("jdbc/ipam");
    } catch (NamingException e) {
        throw new IllegalArgumentException(e);
    }
    this.init(new SystemConfiguration(datasource));
}

这是我第一次使用IntelliJ,而且我是Java的新手,所以我不知道该怎么做。

这是 context-fragment.xml

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <Resource name="jdbc/ipam"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              auth="Container"
              type="javax.sql.DataSource"
              initialSize="2"
              maxActive="5"
              maxIdle="5"
              minIdle="1"
              maxWait="10000"
              removeAbandoned="true"
              removeAbandonedTimeout="60"
              logAbandoned="true"
              defaultReadOnly="false"
              username="**********"
              password="**********"
              driverClassName="org.postgresql.Driver"
              validationQuery="select 1"
              validationQueryTimeout="5"
              logValidationErrors="true"
              testOnBorrow="true"
              testWhileIdle="true"
              url="jdbc:postgresql://db1host/ipam_dev_table_here"/>
</Context>

1 个答案:

答案 0 :(得分:22)

确保在启动tomcat时设置IntelliJ以部署context.xml文件。你可以通过

来做到这一点
  1. 选择&#34;项目结构&#34;从文件菜单

  2. 从那里,选择&#34; Facets&#34;选项。确保已配置Web构面。如果没有,请添加一个。

  3. 添加Web构面后,选择&#34;添加Application Server特定描述符...&#34;

  4. 从选项中选择Tomcat Context Descriptor,然后单击OK。

  5. 默认情况下,IntelliJ将指向META-INF / context.xml。如果这不是上下文文件的位置,则需要编辑文件的路径。