Java错误:无法找到或加载主类车辆
我正在尝试在成功编译车辆类后加载该类,但出现以下错误:Java错误:无法找到或加载主类车辆,java,Java,我正在尝试在成功编译车辆类后加载该类,但出现以下错误: 错误:找不到或无法加载主类车辆 我认为我的代码中可能有一个错误,如下所示: public class Vehicle { String make; String color; boolean engineState; void startEngine() { if (engineState == true) System.out.println("The engin
错误:找不到或无法加载主类车辆
我认为我的代码中可能有一个错误,如下所示:
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
/classes
然后可以按如下方式运行它们:
java -cp <project root>/classes Vehicle
java-cp/classes车辆
查看有关该主题的详细信息在我的eclipse works fine中,结果如下:
Calling showAttributes ...
This vehicle is a Midnight blueRolls Royce
The engine is off.
--------
Starting the engine ...
The engine is now on.
--------
Calling showAttributes ...
This vehicle is a Midnight blueRolls Royce
The engine is on.
--------
Starting the engine ...
The engine is already on!
代码有效错误不在代码中,而在启动代码的命令中。我的意思是你发布了所有不相关的信息,而没有相关的信息。目前的问题无法帮助您解决。我正在使用me命令提示符对其进行测试。还有别的地方我可以运行它吗?我还将检查您所说的位置。我尝试了您的解决方案,但出现了相同的错误。如果javac Vehicle.java已经运行,那么应该已经创建了一个名为Vehicle.class的文件,对吗??如果javac Vehicle.java报告成功,那么将生成一个类文件-我们只需要找出:-)您是通过IDE还是通过命令提示符构建它?如果在提示符下,您可以尝试以下方法:javac-d。Vehicle.java-这应该会在您所在的同一目录中生成类文件-您能试试这个并告诉我它是否有效吗?好的,我键入了这个。让我告诉您javac Vehicle.java没有报告任何内容。它刚刚运行,命令提示符再次显示c:\labs\CourseTwo目录。它应该说些什么吗?我之前创建了另一个.java,在修复路径目录后,它工作得很好。因此,似乎根本没有创建该类,这就是为什么我说我的代码可能有问题的原因:(当我执行以下操作时,不会创建Vehicle.class:javac Vehicle.java