0
votes

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.

Idea didn't consider external library paths when run .class under the provided scope because the "provided" scope means that this library is provided by the runtime environment and IDEA should not place it into the classpath itself. - Thomas Kläger
Thank you for answering my questions. I understande Idea 's setting now. And now I figure it out that why the command java sub.Sub -cp "./path1;../path2; didn't work. The key is the parameter sequence, if -cp parameter is set before sub.Sub just like java -cp "./path1;./path2" sub.Sub it works!!!! I thought the order of the parameters of the command is free, but obviously the java command is not. To my surprise, the -cp of the javac command can be placed anywhere. That means both of the following commands about javac work: - kuze mhw
javac -cp "./path1;.;" ./path2/sub/Sub.java javac ./path2/sub/Sub.java -cp "./path1;.;" - kuze mhw
Yes, for the java command the order matters: the JVM options first (including the classpath), then the class name (sub.Sub), then the arguments for your main() method. Otherwise the java command could not distinguish between the options for the JVM and the arguments for your main() method. - Thomas Kläger
For the javac things are a little different: the options for the JVM are prefixed with -J, all other arguments are for the compiler. - Thomas Kläger