Java错误:找不到或无法加载主类车辆
我想以后我成功编译它的载重车辆类,但这个错误occures: Error: could not find or load main class Vehicle
Java错误:找不到或无法加载主类车辆
我认为有可能是在我的代码错误,其中是以下内容:
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更多细节
我正在使用我命令提示符来测试它。有什么地方可以运行吗?我也会检查你所说的位置 – 2013-03-04 12:50:59
我试过你的解决方案,但同样的错误发生。如果javac Vehicle.java已经运行了一个名为Vehicle.class的文件应该已经创建好了吗?似乎没有创建这样的文件 – 2013-03-04 13:26:25
如果javac Vehicle.java报告成功,那么将生成一个类文件 - 我们只需要找出在哪里:-)您是通过IDE还是通过命令创建它提示?如果从提示符中可以试试这个:javac -d。 Vehicle.java - 这应该在你所在的同一目录中生成类文件 - 你可以试试这个,让我知道它是否有效? – 2013-03-04 13:31:43
在我的日食工作正常,这是结果:
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!
代码工程
Vehicle.class不会创建时,我执行以下操作:javac Vehicle.java – 2013-03-04 13:37:40
错误不在您的代码中,而是在您用来启动它的命令中。 – 2013-03-04 11:34:44
你是什么意思? – 2013-03-04 14:10:49
我的意思是你发布了所有不相关的信息,而且没有任何相关的信息。就目前而言,你无法帮助解决这个问题。 – 2013-03-04 14:27:29