简体   繁体   English

错误:无法找到或加载主类 Java

[英]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.我在 StackOverflow 上阅读了各种内容并尝试复制解决方案,但没有任何效果,它仍然给出相同的错误。 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.本质上,我有 3 个文件, f1.java, f2.javaf3.java ,我使用命令行javac f1.java f2.java f3.java编译我的程序,没有问题。 However, when I try to run the program with the line java f1 f2 f3 or java -cp . f1 f2 f3但是,当我尝试使用java f1 f2 f3java -cp . f1 f2 f3行运行程序时java -cp . f1 f2 f3 java -cp . f1 f2 f3 , I get the following error: java -cp . f1 f2 f3 ,我收到以下错误:

  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.我所有的 java 文件和类文件都在同一个文件夹中,src。 I've tried running the command from within src and outside.我试过从 src 内部和外部运行命令。 My f1 class is also in the f1 java file.我的 f1 类也在 f1 java 文件中。 I am using vscode, Java SE-11.我正在使用 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.我厌倦了在同一个文件夹中创建三个 java 类,它对我有用。 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:打开JDK版本:

$ 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 java -cp . abcf1

Are you following a tutorial?你在学习教程吗? If not, you should and the tutorial would explain about packages and classpaths.如果没有,你应该和教程将解释包和类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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