Java ClassDefFoundError& NoClassFoundException

时间:2015-04-21 19:28:09

标签: java

我目前正在使用Notepad ++和Java JDK 7开发一款小型2D游戏。我不想使用像Eclipse这样的IDE,所以我的java文件和类都在我的 C:\中用户\ Jeremy \ Documents \ ZombieSurvival 目录。要编译和运行我的代码,我使用notepad ++中的npexec插件并执行命令:

C:\ Program Files \ Java \ jdk1.7.0_79 \ bin \ javac Game.java (编译)

C:\ Program Files \ Java \ jdk1.7.0_79 \ bin \ java游戏(正在运行)

一切一直在工作,直到我开始编写我的TileMap类。如果我使用这个类,会弹出ClassNotFoundError和NoClassDefFoundException。这很奇怪,因为在我的另一个游戏中,java在我的Map类中显示相同的错误,所以我的TileMap类错误的原因是什么?就像我说的不使用这个类,程序运行完美!有什么想法吗?

import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;


public class TileMap {

private int[][] map;
private int mapWidth;
private int mapHeight;
private int tileSize;

private BufferedImage image;
private Graphics2D g;

public TileMap(String file, int tileSize) {
    this.tileSize = tileSize;
    loadMap(new File(file));
}

public void update() {

}

public void render(Graphics2D g) {
    g.drawImage(image, 0, 0, null);
}

private void loadMap(File file) {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    mapWidth = Integer.parseInt(reader.readLine());
    mapHeight = Integer.parseInt(reader.readLine());
    map = new int[mapHeight][mapWidth];

    image = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_INT_RGB);
    g = (Graphics2D) image.getGraphics();

    for(int col = 0; col < mapHeight; col ++) {
        String line = reader.readLine();
        String[] tokens = line.split(" ");

        for(int row = 0; row < mapWidth; row++) {
            map[col][row] = Integer.parseInt(tokens[row]);
        }
    }
}

private void drawMap() {
    for(int col = 0; col < mapHeight; col ++) {
        for(int row = 0; row < mapWidth; row++) {
            int block = map[col][row];
            if(block == 1) {
                g.setColor(Color.BLACK);
            } else {
                g.setColor(Color.WHITE);
            }

            g.fillRect(row * tileSize, col * tileSize, tileSize, tileSize);
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果要从类中访问其他(非JRE)类,则需要使用classpath选项明确告知JVM类文件所在的位置。使用它来运行您的应用程序(假设工作目录为C:\Users\Jeremy\Documents\ZombieSurvival):

C:\Program Files\Java\jdk1.7.0_79\bin\java -classpath "." Game