简体   繁体   English

无法找到或加载主 class 和 Main.java:5:错误:找不到符号与代码

[英]Could not find or load main class and Main.java:5: error: cannot find symbol vs code

I have already looked at questions with exact same error code is mine, but i still wasnt able to resolve the problem by looking at the solutions under them.我已经查看了与我的错误代码完全相同的问题,但我仍然无法通过查看它们下的解决方案来解决问题。 Currently i am trying to learn java through a tutorial, and by doing the exam same steps the code worked in the video but mine doesnt.目前我正在尝试通过教程学习 java,并通过考试相同的步骤,视频中的代码有效,但我的没有。

Car file ->汽车档案 ->

package com.ChiragAgg5k;

public class Car {
    private int bmw;

    public void setbmw(int bmw) {
        this.bmw = bmw;
    }

    public int getbmw() {
        return this.bmw;
    }

    public void main(String[] args) {
        System.out.println(bmw);
    }

}

Main file ->主文件->


public class Main {
    public static void main(String[] args) {
        Car carr = new Car();
        carr.setbmw(20);
        System.out.println(carr.getbmw());
    }
}

Error code(s) ->错误代码->

Error: Could not find or load main class Car
Caused by: java.lang.NoClassDefFoundError: com/ChiragAgg5k/Car (wrong name: Car)
Main.java:5: error: cannot find symbol
        Car carr = new Car();
        ^
  symbol:   class Car
  location: class Main
Main.java:5: error: cannot find symbol
        Car carr = new Car();
                       ^
  symbol:   class Car
  location: class Main

I tried writing a simple hello world program in the package and it too gave error enter image description here我尝试在 package 中编写一个简单的 hello world 程序,它也给出了错误enter image description here

Error Main.java:5: error: cannot find symbol indicates missing import from other package or missing class in the same package Error Main.java:5: error: cannot find symbol indicates missing import from other package or missing class in the same package

You have to add import statement for Car in your Main class.您必须在主 class 中添加 Car 的导入语句。 Because your Main class resides in another Package.因为您的主要 class 位于另一个 Package 中。

import com.ChiragAgg5k.Car;

public class Main {
    public static void main(String[] args) {
        Car carr = new Car();
        carr.setbmw(20);
        System.out.println(carr.getbmw());
    }
}

As Alternative you could move Main into the same Package as Car class.作为替代方案,您可以将 Main 移动到与 Car class 相同的 Package 中。

package com.ChiragAgg5k;

public class Main {
    public static void main(String[] args) {
        Car carr = new Car();
        carr.setbmw(20);
        System.out.println(carr.getbmw());
    }
}

So the solution was using Java code tester, instead of directly running the code in terminal as doing so does not change the path from which the code should actually be ran所以解决方案是使用 Java 代码测试器,而不是直接在终端中运行代码,因为这样做不会改变代码实际运行的路径

Java code tester (a vs code extension) took care of it automatically Java 代码测试器(一个 vs 代码扩展)自动处理它

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

相关问题 1个错误! “ Main.java:30:错误:找不到符号” - 1 error! “Main.java:30: error: cannot find symbol” java编译错误“无法找到或加载主类main.java” - java compiling error “could not find or load main class main.java” 如何解决 Java 中的“main.java:7: error: cannot find symbol”错误? - How to resolve "main.java:7: error: cannot find symbol" error in Java? 找不到或加载主 class - VS 代码 - Could not find or load main class - VS Code JavaFX on Eclipse "Could not find or load main class Main.java Caused by: java.lang.ClassNotFoundException: Main.java" usual solutions not working - JavaFX on Eclipse "Could not find or load main class Main.java Caused by: java.lang.ClassNotFoundException: Main.java" usual solutions not working 无法使用带参数的方法给我一个 output:Main.java:28:错误:找不到符号 - Can't use methods with parameters to give me an output: Main.java:28: error: cannot find symbol Java-错误:找不到或加载主类 - Java - Error: Could not find or load main class 错误:找不到或加载主类(在Java 8中) - Error: Could not find or load main class ( in Java 8) Java:错误:找不到或加载主类 - Java: Error: Could not find or load main class 错误:无法找到或加载主类 Java - Error: Could not find or load main class Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM