无法找到或加载主类cmd

发布于 2025-01-04 07:06:30 字数 1462 浏览 0 评论 0原文

我用 javac 编译了一些示例代码,当我尝试运行它时,我得到的结果是:

C:\Users\Lenis>cd C:\Java\2D

C:\Java\2D>java donut.board
Error: Could not find or load main class donut.board

我尝试编写“java -cp . donut.board”,但仍然得到相同的结果。

我的类路径是:“C:\Program Files\Java\jdk1.7.0_02\lib\tools.jar;”。

如何修复它?

这是代码:

package donut;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;

public class Board extends JPanel{

public void paint(Graphics g)
{
  super.paint(g);

  Graphics2D g2 = (Graphics2D) g;

  RenderingHints rh =
        new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                           RenderingHints.VALUE_ANTIALIAS_ON);

  rh.put(RenderingHints.KEY_RENDERING,
         RenderingHints.VALUE_RENDER_QUALITY);

  g2.setRenderingHints(rh);

  Dimension size = getSize();
  double w = size.getWidth();
  double h = size.getHeight();

  Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
  g2.setStroke(new BasicStroke(1));
  g2.setColor(Color.gray);


  for (double deg = 0; deg < 360; deg += 5) {
      AffineTransform at =
          AffineTransform.getTranslateInstance(w / 2, h / 2);
      at.rotate(Math.toRadians(deg));
      g2.draw(at.createTransformedShape(e));
    }
}
}

I compiled some sample code with javac and when i tried running it here's what i get:

C:\Users\Lenis>cd C:\Java\2D

C:\Java\2D>java donut.board
Error: Could not find or load main class donut.board

I tried writing "java -cp . donut.board" and still get the same.

My classpath is : "C:\Program Files\Java\jdk1.7.0_02\lib\tools.jar;."

How to fix it?

And here is the code:

package donut;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;

public class Board extends JPanel{

public void paint(Graphics g)
{
  super.paint(g);

  Graphics2D g2 = (Graphics2D) g;

  RenderingHints rh =
        new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                           RenderingHints.VALUE_ANTIALIAS_ON);

  rh.put(RenderingHints.KEY_RENDERING,
         RenderingHints.VALUE_RENDER_QUALITY);

  g2.setRenderingHints(rh);

  Dimension size = getSize();
  double w = size.getWidth();
  double h = size.getHeight();

  Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
  g2.setStroke(new BasicStroke(1));
  g2.setColor(Color.gray);


  for (double deg = 0; deg < 360; deg += 5) {
      AffineTransform at =
          AffineTransform.getTranslateInstance(w / 2, h / 2);
      at.rotate(Math.toRadians(deg));
      g2.draw(at.createTransformedShape(e));
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千鲤 2025-01-11 07:06:30

你在类中没有 main 方法,并且在调用时你必须给出 donut.Board (大写 B)它区分大小写。添加主要方法。

You don't have main method in the class and also when invoking you have to give donut.Board (capital B) It is case sensitive. Add the main method.

清引 2025-01-11 07:06:30

您的类名 Board 以大写字母开头。

Your class name Board starts with capital.

兲鉂ぱ嘚淚 2025-01-11 07:06:30

在 Board 类中包含您的 main 方法,您可以在此处查看一个简单的示例: http://docs.oracle.com/javase/tutorial/java/concepts/class.html

还要更改 java -cp 。 donut.boardjava -cp 。 donut.Board

正如您在下面的代码中看到的,这是一个本身不能做任何事情的类,这只是说明您可以使用自行车做什么,但您没有使用所有这些行为

 class Bicycle {
    int cadence = 0;
    int speed = 0;
    void changeCadence(int newValue) {
         cadence = newValue;
    }
    void speedUp(int increment) {
         speed = speed + increment;   
    }
    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }
    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed );
    }
} 

所以如果如果你想使用这个类,你可以创建另一个包含 main 方法的类,现在你实际上正在使用你在上一个类中为 Bicycle 提供的所有行为

class BicycleDemo {
    public static void main(String[] args) {
       Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.printStates();
    }
}

Include your main method in your Board class, you can see a simple example here: http://docs.oracle.com/javase/tutorial/java/concepts/class.html

Also change java -cp . donut.board to java -cp . donut.Board

As you can see in the following code, this is a class that by itself can´t do anything, this is saying just what you can do with a Bicycle but you are not using all that behavior

 class Bicycle {
    int cadence = 0;
    int speed = 0;
    void changeCadence(int newValue) {
         cadence = newValue;
    }
    void speedUp(int increment) {
         speed = speed + increment;   
    }
    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }
    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed );
    }
} 

So if you want to use this class you can create another class where you include the main method, now you are really using all the behavior that you gave to a Bicycle in the previous class

class BicycleDemo {
    public static void main(String[] args) {
       Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.printStates();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文