且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编译错误:包 javax.servlet 不存在

更新时间:2022-05-04 02:52:18

需要将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.