编译错误:包 javax.servlet 不存在 [英] Compile error: package javax.servlet does not exist

查看:41
本文介绍了编译错误:包 javax.servlet 不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包,我在其中导入 javax.servlet.* 和 javax.servlet.http.*当我尝试在命令提示符下编译它时出现错误

I have a package in which I import javax.servlet.* and javax.servlet.http.* When I try to compile it in command prompt I get the error

包 javax.servlet 不存在

package javax.servlet does not exist

我使用 JDK 1.7.0 和 Tomcat 6.0.

I use JDK 1.7.0 and Tomcat 6.0.

推荐答案

需要将Tomcat的/lib/servlet-api.jar文件的路径添加到编译时的classpath中.

You need to add the path to Tomcat's /lib/servlet-api.jar file to the compile time classpath.

javac -cp .;/path/to/Tomcat/lib/servlet-api.jar com/example/MyServletClass.java

Java 需要在类路径中查找导入的依赖项.否则,它将默认为在上面的示例中作为 . 包含的当前文件夹.; 是 Windows 的路径分隔符;如果您使用的是基于 Unix 的操作系统,那么您需要使用 : 代替.

The classpath is where Java needs to look for imported dependencies. It will otherwise default to the current folder which is included as . in the above example. The ; is the path separator for Windows; if you're using an Unix based OS, then you need to use : instead.

如果您仍然面临相同的编译错误,并且您实际上使用的是 Tomcat 10 或更高版本,那么您应该从 javax.* 迁移源代码中的导入jakarta.*.

If you're still facing the same complation error, and you're actually using Tomcat 10 or newer, then you should be migrating the imports in your source code from javax.* to jakarta.*.

import jakarta.servlet.*;
import jakarta.servlet.http.*;

如果您出于任何原因想继续使用 javax.*,那么您应该降级到 Tomcat 9 或更旧版本,因为这是仍在使用旧的 javax.* 的最新版本 命名空间.

In case you want to keep using javax.* for whatever reason, then you should be downgrading to Tomcat 9 or older as that was the latest version still using the old javax.* namespace.

这篇关于编译错误:包 javax.servlet 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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