错误:当类扩展HttpServlet时找不到或加载主类 [英] Error: Could not find or load main class when class extends HttpServlet

查看:99
本文介绍了错误:当类扩展HttpServlet时找不到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上了这个课:

public class Test  {
    public static void main(String[] args) {
        System.out.println("hey");
    }
}

它工作正常.但是如果我将其扩展为 HttpServlet :

it works fine. But if I make it extend HttpServlet:

public class Test extends HttpServlet {
    public static void main(String[] args) {
        System.out.println("hey");
    }
}

我得到一个错误:找不到或加载主类Test 错误.

我也使用Maven,但是在这种情况下,请尝试在Idea Intellij中手动编译和运行该类.

I also use Maven, but in this case try to compile&run the class by hand in Idea Intellij.

我这样导入servlet:

I import servlet like this:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

我还尝试测试其他依赖项:

I also tried to test other dependency:

<dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.11.3</version>
</dependency>

public class TestServlet {
    public static void main(String[] args) {
        System.out.println("hi");
        Jsoup soup;
    }


}

正常工作.添加扩展HttpServlet 的那一刻,我再次收到错误消息.

works fine. The moment I add extends HttpServlet, I get the error again.

推荐答案

将Servlets API文件添加到您的Classpath中.我想知道您是否也没有导入它,如果没有,请导入它.

Add the Servlets API file to your Classpath. I wonder you didn't imported it too, if not, import it.

这篇关于错误:当类扩展HttpServlet时找不到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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