-1

I have this class:

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

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");
    }
}

I get a Error: Could not find or load main class Test error.

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

Edit: 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;
    }


}

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

parsecer
  • 4,758
  • 13
  • 71
  • 140

2 Answers2

0

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

  • I have imported it with Maven. I can import the class inside the source code - Idea gives no error. – parsecer Jul 10 '19 at 20:30
  • Can you put a Java code snippet in your question in the lines where you're importing the HttpServlet API? – Cristian Ramírez Fonseca Jul 10 '19 at 21:52
  • Yes, buddy, but you gotta import it anyway. something like " import javax.servlet.http.HttpServlet;". Please tell me if it worked. I widely recommend you not to use Maven if you're new to Java programming. Just start with simple things, you'll understand it later. – Cristian Ramírez Fonseca Jul 11 '19 at 18:13
  • I already solved it. And I'm not new to Java. Nor do I think novice Java programmers should avoid Maven, it is a great help when managing dependencies. – parsecer Jul 11 '19 at 21:21
-1

I managed to solve it. I went to File - Project Structure:

enter image description here

Then to Modules:

enter image description here

There was a Provided scope near the servlet module. I changed it to Compile:

enter image description here

And it worked!

parsecer
  • 4,758
  • 13
  • 71
  • 140