首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java错误:无法找到或加载主类Vehicle

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

Stack Overflow用户
提问于 2013-03-04 19:33:18
回答 2查看 916关注 0票数 0

在成功编译Vehicle类之后,我试图加载它,但出现了这个错误:Error: could not find or load main class Vehicle

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

代码语言:javascript
复制
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();

    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-03-04 19:39:27

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

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

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

代码语言:javascript
复制
<project root>/classes

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

代码语言:javascript
复制
java -cp <project root>/classes Vehicle

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

票数 1
EN

Stack Overflow用户

发布于 2013-03-04 19:39:43

在我的eclipse中运行得很好,结果如下:

代码语言:javascript
复制
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!

代码可以正常工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15200571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档