我正在一个项目中,该项目看起来将tomcat嵌入到Java Web应用程序中。 我希望将此应用程序转换为Java Web Start,以便可以对其进行部署,并且下载该应用程序的任何人都可以在嵌入式服务器上使用该应用程序,而不必下载tomcat。 我面临的问题是,当我在eclipse上运行代码时,代码运行完美,但是当我将其转换为可下载文件时,该文件在启动时显示了我在标题中提到的错误。 我以前有一个错误,上面说... java.lang.NoClassDefFoundError:org / apache / catalina / Context 我通过使用Catalina jar文件作为jnlp文件中的资源href之一来解决此问题。 虽然据我所知,此LifecycleException是Catalina.jar的一部分。 请帮助解决我的错误,或者至少指出正确的方向来解决它。 我正在Eclipse IDE上使用Maven项目运行代码。 谢谢!
package com.N.VJ;
import java.io.File;
import javax.servlet.ServletException;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
public class App {
public static void main(String[] args) throws LifecycleException,
InterruptedException, ServletException {
String docBase = "src/main/webapp/";
Tomcat tomcat = new Tomcat();
String webPort = System.getenv("PORT");
if(webPort == null || webPort.isEmpty()) {
webPort = "8088";
}
tomcat.setPort(Integer.valueOf(webPort));
tomcat.addWebapp("/", new File(docBase).getAbsolutePath());
System.out.println("configuring app with basedir: " + new File("./" +
docBase).getAbsolutePath());
tomcat.start();
tomcat.getServer().await();
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tom1</groupId>
<artifactId>cat1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cat1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
</dependencies>
<build>
<finalName>cat1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.tom1.cat1.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<port>${tomcat.port}</port>
<path>/</path>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/</path>
<port>${tomcat.port}</port>
<attachArtifactClassifierType>war</attachArtifactClassifierType>
<mainClass>org.apache.tomcat.maven.runner.Tomcat8RunnerCli</mainClass>
<extraDependencies>
<extraDependency>
<groupId>com.nitorcreations</groupId>
<artifactId>tomcat8-war-runner</artifactId>
<version>1.0</version>
</extraDependency>
</extraDependencies>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-util</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-coyote</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-el-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-tribes</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina-ha</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId>
<version>${tomcat.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<warRunDependencies>
<warRunDependency>
<dependency>
<groupId>com.tom1</groupId>
<artifactId>cat1</artifactId>
<version>4</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.3-SNAPSHOT</version>
<type>maven-plugin</type>
</dependency>
<contextPath>/</contextPath>
</warRunDependency>
</warRunDependencies>
<enableNaming>true</enableNaming>
<extraDependencies>
<extraDependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.1.3.1</version>
</extraDependency>
<extraDependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</extraDependency>
</extraDependencies>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<tomcat.version>8.0.48</tomcat.version>
</properties>
</project>
JNLP文件
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+"
codebase="http://localhost:8080/TCApp"
href="http://localhost:8080/TCApp/SO.jnlp">
<information>
<title>Hello World</title>
<vendor>Noel Joseph</vendor>
<homepage href="http://localhost:8080/TCApp/display.html" />
<description> Hello World</description>
</information>
<security>
<all-permissions />
</security>
<resources>
<j2se version="1.4+" href="http://java.sun.com/products/autodl/j2se"/>
<jar href="SO.jar" />
<jar href="catalina-6.0.29.jar" />
<jar href="catalina-ha-6.0.29.jar" />
</resources>
<application-desc main-class="com.N.VJ.App"/>
</jnlp>
答案 0 :(得分:1)
嘿,大家解决了这个错误。 受到新错误的欢迎,但已将其修复。 这很简单,不知道为什么我以前没有考虑过。 因此,我要做的就是添加jar href =“必要的jar”。 因此,根据我的理解,当我们在eclipse中打包文件时,打包实际上并没有打包所有必需的jar,或者也许打包,但是它们在运行时没有用,因为当主类调用所需的jar时他们不见了。 通过在href中添加这些必需的jar,可以在运行时和TADA中调用它们,从而解决了问题。
我的JNLP文件最后看起来像这样。
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+"
codebase="http://localhost:8080/TCApp"
href="http://localhost:8080/TCApp/SO.jnlp">
<information>
<title>Hello World</title>
<vendor>Noel Joseph</vendor>
<homepage href="http://localhost:8080/TCApp/display.html" />
<description> Hello World</description>
</information>
<security>
<all-permissions />
</security>
<resources>
<j2se version="1.4+" href="http://java.sun.com/products/autodl/j2se"/>
<jar href="New3.jar" main="true"/>
<jar href="tomcat-catalina-9.0.10.jar" />
<jar href="javax.servlet.jar" />
<jar href="javax.servlet-api-3.0.1.jar" />
<jar href="tomcat-util-9.0.10.jar" />
<jar href="tomcat-util-scan-9.0.10.jar" />
<jar href="tomcat-juli-9.0.10.jar" />
<jar href="tomcat-coyote-9.0.10.jar" />
<jar href="tomcat-api-9.0.10.jar" />
<jar href="tomcat-jni-9.0.10.jar" />
<jar href="tomcat-jsp-api-9.0.10.jar" />
<jar href="commons-logging-1.2.jar" />
<property name="sun.java2d.noddraw"
value="true"/>
</resources>
<application-desc main-class="com.NO.JO.App"/>
</jnlp>