我有一个带有spark的java项目,我希望它部署在Tomcat服务器上。我的问题是,在eclipse的Run on server期间,我收到了错误:
SEVERE: Exception starting filter SparkFilter
javax.servlet.ServletException: java.lang.ClassNotFoundException: com.myprojects.usermobility.main.UserMobilityMain
at spark.servlet.SparkFilter.getApplication(SparkFilter.java:106)
at spark.servlet.SparkFilter.getApplications(SparkFilter.java:131)
at spark.servlet.SparkFilter.init(SparkFilter.java:66)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4700)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5340)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.myprojects.usermobility.main.UserMobilityMain
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at spark.servlet.SparkFilter.getApplication(SparkFilter.java:103)
... 14 more
我已经在其他服务器的spark-java教程中指定了一个web.xml文件。 web.xml中:
<filter>
<filter-name>SparkFilter</filter-name>
<filter-class>spark.servlet.SparkFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>com.myprojects.usermobility.main.UserMobilityMain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SparkFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我还添加了java类中需要的东西。 UserMobilityMain.java:
package com.myprojects.usermobility.main;
import spark.Request;
import spark.Spark;
import spark.servlet.SparkApplication;
public class UserMobilityMain implements SparkApplication {
@Override
public void init() { ... } // there are specified mappings
}
我试图在web.xml中只使用java类名,但它也没有用。我检查了war文件,在WEB-INF / class文件夹中有适当的.class文件。我正在使用Tomcat 8.0和Java 1.8。我还包括maven配置文件。 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>UserMobilityWeb</groupId>
<artifactId>UserMobilityWeb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<build>
<finalName>UserMobilityWeb</finalName>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
</dependencies>
</project>
此外,我尝试通过maven构建并将war文件移动到Tomcat服务器中的webapps文件夹,但它仍然在Tomcat日志文件中出现相同的错误。
编辑:我之前还有这个项目的war文件正在运行,但我需要对代码进行一些更改并构建新的。