无法找到或加载主类cmd
我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在类中没有 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.
您的类名
Board
以大写字母开头。Your class name
Board
starts with capital.在 Board 类中包含您的 main 方法,您可以在此处查看一个简单的示例: http://docs.oracle.com/javase/tutorial/java/concepts/class.html
还要更改
java -cp 。 donut.board
到java -cp 。 donut.Board
正如您在下面的代码中看到的,这是一个本身不能做任何事情的类,这只是说明您可以使用自行车做什么,但您没有使用所有这些行为
所以如果如果你想使用这个类,你可以创建另一个包含 main 方法的类,现在你实际上正在使用你在上一个类中为 Bicycle 提供的所有行为
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
tojava -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
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