【问题标题】:error when compiling java program 'Cannot find symbol'编译java程序时出错'找不到符号'
【发布时间】:2013-09-11 13:24:33
【问题描述】:

尝试在终端中使用 javac -g 命令编译“PongMain.java”时出现这些错误:

错误:

    tests-iMac:~ finnfallowfield$ javac -g /Users/finnfallowfield/Desktop/Developer/Java\:Javascript/Game\ Development/Java\ Pong/src/main/pong/PongMain.java 
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:9: error: cannot find symbol
import main.pong.Ball;
                ^
  symbol:   class Ball
  location: package main.pong
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:10: error: cannot find symbol
import main.pong.PaddleLeft;
                ^
  symbol:   class PaddleLeft
  location: package main.pong
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:11: error: cannot find symbol
import main.pong.PaddleRight;
                ^
  symbol:   class PaddleRight
  location: package main.pong
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:15: error: cannot find symbol
    Ball ball;
    ^
  symbol:   class Ball
  location: class PongMain
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:16: error: cannot find symbol
    PaddleLeft pLeft;
    ^
  symbol:   class PaddleLeft
  location: class PongMain
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:17: error: cannot find symbol
    PaddleRight pRight;
    ^
  symbol:   class PaddleRight
  location: class PongMain
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:29: error: cannot find symbol
        ball = new Ball();
                   ^
  symbol:   class Ball
  location: class PongMain
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:30: error: cannot find symbol
        pLeft = new PaddleLeft();
                    ^
  symbol:   class PaddleLeft
  location: class PongMain
/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/main/pong/PongMain.java:31: error: cannot find symbol
        pRight = new PaddleRight(ball.getY() - 35);
                     ^
  symbol:   class PaddleRight
  location: class PongMain
9 errors

我正在尝试编译一个乒乓球游戏,所有其他 java 文件都编译得很好,但这个没有。 这是我要编译的文件的代码:

文件源代码:

package main.pong.main;

import java.applet.*;
import java.awt.event.*;
import java.awt.*;

import javax.swing.Timer;

import main.pong.Ball;
import main.pong.PaddleLeft;
import main.pong.PaddleRight;

public class PongMain extends Applet implements MouseMotionListener, ActionListener
{
        Ball ball;
        PaddleLeft pLeft;
        PaddleRight pRight;
        Font newFont = new Font("sansserif", Font.BOLD, 20);
        Graphics bufferGraphics;
        Image offscreen;
        final int WIDTH = 500, HEIGHT = 300;
        long currentTime;

        public void init()
        {
                //Sets the applet to be 500 * 300
                setSize(500, 300);
                //Initiate ball and two paddles
                ball = new Ball();
                pLeft = new PaddleLeft();
                pRight = new PaddleRight(ball.getY() - 35);

                //Add mousMotionListener
                addMouseMotionListener(this);
                setBackground(Color.blue);
                offscreen = createImage(WIDTH, HEIGHT);
                bufferGraphics = offscreen.getGraphics();
        }

        public void start(){
                currentTime = System.currentTimeMillis();
                //Set up frame-rate
                Timer time = new Timer(15, this);
                time.start();
                while(pRight.getScore() < 10){
                }
                time.stop();
                currentTime = System.currentTimeMillis() - currentTime;
                repaint();
        }

        public void stop(){

        }

        public void paint(Graphics g)
        {
                bufferGraphics.clearRect(0,0,WIDTH,HEIGHT);
                bufferGraphics.setColor(Color.green);
                //Left side
                bufferGraphics.fillRect(pLeft.XPOS,pLeft.getPos(),10,70);
                //Right side
                bufferGraphics.fillRect(pRight.XPOS, pRight.getPos(), 10, 70);

                //White lines
                bufferGraphics.setColor(Color.white);
                bufferGraphics.setFont(newFont);
                bufferGraphics.drawString("Futile", 150, 15);
                bufferGraphics.drawString(""+ pRight.getScore(),300,15);
                bufferGraphics.fillRect(240,0,20,300);

                if(pRight.getScore() == 10){
                        //Display for how long game lasted
                        bufferGraphics.drawString("You Lasted: " + (currentTime/ 1000) + "sec.", 40, 150);
                }

                //We draw the ball
                bufferGraphics.setColor(Color.red);
                bufferGraphics.fillRect(ball.getX(), ball.getY(),10, 10);

                g.drawImage(offscreen,0,0,this);
                Toolkit.getDefaultToolkit().sync();
    }


    // STUFF
        public void update(Graphics g)
        {
                paint(g);
        }

        public void mouseMoved(MouseEvent evt)
        {
                pLeft.setPos(evt.getY()- 35);
        }

        public void mouseDragged(MouseEvent evt)
        {
        }

        public void checkCollision(){
                if(ball.getY() == 0 || ball.getY() == 290){
                        ball.dy = (ball.dy * -1);
                }

                if((ball.getX() == 40) && hitPaddle()){
                        ball.dx = (ball.dx * -1);
                }

                if(ball.getX() == 460){
                        ball.dx = (ball.dx * -1);
                }

                if(ball.getX() == 0){
                         pRight.setScore(pRight.getScore() + 1);
                         ball.reset();
                }
        }

        public boolean hitPaddle(){
                boolean didHit = false;

                if((pLeft.getPos() - 10) <= ball.getY() && (pLeft.getPos() + 70) > ball.getY()){
                        didHit = true;
                }
                return didHit;
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
                ball.move();
                pRight.setPos(ball.getY() - 35);
                checkCollision();
                repaint();
        }
}

感谢您的阅读,请回复并注意我是一个完整的java初学者和 堆栈交换,所以我需要很多帮助来解决这个问题!

【问题讨论】:

  • 不要贴pastebin链接,直接贴相关代码和错误。
  • 并显示您用于编译的确切命令。
  • 我觉得直接粘贴代码太长了?
  • @SharkyDoesCode:如果当前形式太长,请减少它,直到您有一个简短但完整的程序来证明问题。这是诊断过程的一部分。
  • @JohnSkeet 我改变了 pastebin 的东西,但错误遍布整个程序,所以缩短它是不可能的......

标签: java eclipse macos compilation terminal


【解决方案1】:

要正确编译,需要满足一些外部条件:

  1. 必须有一个名为main.pong.Ball 的类
  2. Ball.javaBall.class 必须可用
  3. 如果是源文件,则它必须位于编译时类路径上编译器可用的目录main/pong 中,或者必须在javac 命令行上命名
  4. 如果是类文件,那么它需要在编译时类路径上的目录main/pong 中。

其中一个条件没有得到满足;遇到所有问题,这些问题应该会消失(当然,可能会用新问题代替。)一般来说,鉴于您似乎拥有的设置,实现这一目标的最简单方法是使用“cd”命令更改为目录/Users/finnfallowfield/Desktop/Developer/Java:Javascript/Game Development/Java Pong/src/,然后运行javac main/pong/PongMain.java

【讨论】:

  • 谢谢,认为解决了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多