【问题标题】:Can anyone tell me why this works in eclipse but not in the command prompt? [closed]谁能告诉我为什么这在 Eclipse 中有效,但在命令提示符下无效? [关闭]
【发布时间】:2014-11-29 14:28:11
【问题描述】:

我从讲师那里得到了 3 节课作为作业,在我完全编辑它们之前,我尝试编译它们。它们不会在引用“DrawPanel panel = new DrawPanel();”时引用“cannot find symbol error”的命令提示符进行编译。然后我尝试在 Eclipse 中运行它,它运行良好,任何关于为什么甚至如何让它在我的命令提示符下工作的想法

TestDraw.java:

    package shapessimple;

    import javax.swing.JFrame;
    public class TestDraw
     {
        public static void main (String[]args)
     {
            DrawPanel panel = new DrawPanel();
            JFrame application = new JFrame();

            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            application.add(panel);
            application.setSize(300,300);
            application.setVisible(true);
        }
    }

DrawPanel.java:

package shapessimple;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {

    private Random randomNumbers = new Random();
    private MyLine[] lines;


    public DrawPanel() {
        setBackground(Color.white);

        lines = new MyLine[5 + randomNumbers.nextInt(5)];

        for (int count = 0; count < lines.length; count++) {
            int x1 = randomNumbers.nextInt(300);
            int x2 = randomNumbers.nextInt(300);
            int y1 = randomNumbers.nextInt(300);
            int y2 = randomNumbers.nextInt(300);

            Color color = new Color(randomNumbers.nextInt(256),
                    randomNumbers.nextInt(256), randomNumbers.nextInt(256));

            lines[count] = new MyLine(x1, y1, x2, y2, color);
        } 
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (MyLine line : lines) {
            line.draw(g);
        }
    }
}

MyLine.java:

package shapessimple;

import java.awt.Color;
import java.awt.Graphics;

public class MyLine {
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private Color myColor;

    public MyLine (int x1, int y1, int x2, int y2, Color color) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        myColor = color;
    }

    public void draw (Graphics g) {
        g.setColor(myColor);
        g.drawLine(x1,y1,x2,x2);
    }
}

编辑:

现在正在编译,但我收到了NoClassDefFoundError: TestDraw error message

【问题讨论】:

  • 你尝试运行它的完整命令是什么?
  • @StephenHogarty 用于编译。 glad3dr 询问您用于运行的命令。
  • 为什么不javac *.java
  • 它不会在命令提示符下编译。在eclipse中我按下了运行
  • @StephenHogarty 您必须在命令中包含所有文件才能编译它:javac TestDraw.java DrawPanel.java MyLine.java

标签: java eclipse command prompt symbols


【解决方案1】:

必须先编译所有文件,然后才能从命令行运行程序。要编译它,您必须包含所有文件:

javac TestDraw.java DrawPanel.java MyLine.java

或者你可以这样做

javac *.java

然后您应该能够通过运行包含main() 的文件并指定类路径以便类加载器知道在哪里查找来运行程序:

java -cp . TestDraw

如果你不设置类路径,那么类加载器将只使用 CLASSPATH 环境变量的值,它可能不包含包含你的程序的目录。

请注意,在运行 .class 文件时,您不要包含 .class 扩展名。

【讨论】:

  • 谢谢它现在编译得很好,但它不会运行。现在我得到一个 NoClassDefFoundError
  • 您能否发布对包含完整错误的问题的编辑?
  • @StephenHogarty 尝试使用java shapesimple.TestDraw 从父目录运行它,就像@MikeLaren 建议的那样。
  • 当我这样做时它说 错误:无法找到或加载主类 shapesimple.TestDraw
  • @StephenHogarty 我已经在我的答案中编辑了关于运行程序的部分。试试看。
【解决方案2】:

为了运行应用程序,您需要编译所有使用的 .java 文件,而不是只编译“TestDraw.java”。确保每个源 .java 文件都有一个 .class 文件,并且它们都在 Sharpesimple 文件夹中。

然后运行应用程序只需执行java shapesimple.TestDraw,它应该可以正常工作。

【讨论】:

    【解决方案3】:

    如果您在 Windows 机器上运行,请转到 shapessimple 的父文件夹并尝试以下命令:

    javac -cp ./:%CLASSPATH% shapessimple.TestDraw.java
    

    或在 Linux 机器上:

    javac -cp ./;$CLASSPATH shapessimple.TestDraw.java 
    

    【讨论】:

    • OP 需要在运行之前编译他的所有类。您正在编译 1 个类文件。它不会找到其余的类文件,因为它们没有被编译。
    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多