简体   繁体   中英

Error: Could not find or load main class Java

I've read various things on StackOverflow and have tried to replicate the solutions but nothing worked, it stills gives the same error. Essentially, I have 3 files, f1.java, f2.java , and f3.java , I compile my program with the command line, javac f1.java f2.java f3.java , with no issue. However, when I try to run the program with the line java f1 f2 f3 or java -cp . f1 f2 f3 java -cp . f1 f2 f3 , I get the following error:

  Error: Could not find or load main class f1
    Caused by: java.lang.ClassNotFoundException: f1

All my java files and class files are in the same folder, src. I've tried running the command from within src and outside. My f1 class is also in the f1 java file. I am using vscode, Java SE-11. I've been stuck on this issue for a while, how do I fix this? Thanks for the help!

I tired creating three java classes in the same folder and it works for me. I would suggest you refer to solution on this link: What does "Could not find or load main class" mean?

public class f1 {
  public static void main(String[] a){
    System.out.println("f1 main");
  }
}
public class f2 {
  public static void main(String[] a){
    System.out.println("f2 main");
  }
}
public class f3 {
  public static void main(String[] a){
    System.out.println("f3 main");
  }
}

Compilation:

$ javac f1.java f2.java f3.java

Running:

$ java f1 f2 f3                
f1 main

Open JDK version:

$ java -version      
 openjdk version "11.0.2" 2019-01-15
 OpenJDK Runtime Environment 18.9 (build 11.0.2+9)

Do you use package names in your classes?

If so, you need to specify the fully qualified names for your classes.

For example, java -cp . abcf1 java -cp . abcf1

Are you following a tutorial? If not, you should and the tutorial would explain about packages and classpaths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM