I first met this problem in my Ideas, I wrote a Class that extends javax.servlet.http.HttpServlet, My Ideas throws an error message reads Error: Could not find or load main class com.bjpowernode.OneServlet, here is the image: enter image description here
It can be seen that Idea didn't show the red wavy line, that shows my codes are fine. I found a solution to this problem from enter link description here, I changed the scope from provided to compile: enter image description here But I actually want to know why and how it works? I compared the difference of Idea compilation instructions under different scope setting,I found that when Idea uses java command to run the .Class, the parameter -classpath of the command of the compile scope has two more paths: D:\apache-tomcat-8.0.50\lib\jsp-api.jar;D:\apache-tomcat-8.0.50\lib\servlet-api.jar That's to say, Idea didn't consider external library paths when run .class under the provided scope, and the super Class HttpServlet is from servlet-api.jar package. Why? To simplify the problem, I created two different classes under two different paths and packages: Class Base and Class Sub, and Sub extends Base. The codes of the Base is here:
package base;
public class Base{
public static void main(String[] args) {
}
}
The codes of the Sub is here:
package sub;
import base.Base;
public class Sub extends Base{
public static void main(String[] args) {
System.out.println("In Sub");
}
}
The path of Base is ./path1/base/Base.java and the path of Sub is ./path2/sub/Sub.java. I compiled them using these two commands:
javac ./path1/base/Base.java -d ./path1
javac ./path2/sub/Sub.java -d ./path2 -cp "./path1;./path2"
And compiled successfully. But when I run sub.Sub using the command below:
java sub.Sub -cp "./path1;./path2"
And I got the same error:
Error: Could not find or load main class sub.Sub
Ive tried multiple variations of this, but none of them seem to work. Any ideas? Although I solved the problem of idea reporting errors, I still could not understand the principle behind? I hope this question can help me to figure it out. My jdk version is 1.8. Thanks in advance.
main()
method. Otherwise the java command could not distinguish between the options for the JVM and the arguments for yourmain()
method. - Thomas Kläger-J
, all other arguments are for the compiler. - Thomas Kläger