gpt4 book ai didi

Java错误: could not find or load main class Vehicle

转载 作者:行者123 更新时间:2023-12-01 14:48:03 25 4
gpt4 key购买 nike

我在成功编译后尝试加载 Vehicle 类,但出现此错误:错误:无法找到或加载主类车辆

我认为我的代码可能有一个错误,如下:

public class Vehicle
{
String make;
String color;
boolean engineState;

void startEngine()
{
if (engineState == true)
System.out.println("The engine is already on!");
else
{
engineState = true;
System.out.println("The engine is now on.");
}
}

void showAttributes()
{
System.out.println("This vehicle is a " + color + "
" + make);
if (engineState == true)
System.out.println("The engine is on.");
else
System.out.println("The engine is off.");
}

public static void main(String args[])
{
// Create a new vehicle and set its attributes.
Vehicle car = new Vehicle();
car.make = "Rolls Royce";
car.color = "Midnight blue";
System.out.println("Calling showAttributes ...");
car.showAttributes();

System.out.println("--------");
System.out.println("Starting the engine ...");
car.startEngine();
System.out.println("--------");
System.out.println("Calling showAttributes ...");
car.showAttributes();

// Let’s try to start the engine again.
System.out.println("--------");
System.out.println("Starting the engine ...");
car.startEngine();

}
}

最佳答案

问题不在于您的代码,而在于您如何启动它。

找到类文件的编译位置,并将此根添加到类路径中。

例如,如果您的类文件编译为:

<project root>/classes

然后您可以按如下方式运行它们:

java -cp <project root>/classes Vehicle

看看Oracle Documentation有关该主题的更多详细信息

关于Java错误: could not find or load main class Vehicle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15200571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com