当我将Java
项目转换为Jar文件时,在此部分代码的第3行中出现ArrayIndexOutOfBounds
例外情况-2:
for (int i = 0; i < copy.get(copy.size() - 2).size(); i++) {
if (!copy.get(copy.size() - 2).get(i).toString().equals(" ")) {
startLocations[index] = Integer.parseInt(copy.get(copy.size() - 2).get(i).toString());
index++;
}
}
我发现这很奇怪,因为程序在Eclipse中运行得非常好,copy
的大小,暂时保持迷宫网格的2D ArrayList
为9.我转换时代码失败到一个Jar,我从cmd运行它。以下是完整的相关代码。所有文件都在正确的位置。
String fileName = "maze.txt";
String line = null;
ArrayList < ArrayList < Square >> grid = new ArrayList < ArrayList < Square >> ();
try {
// Setup FileReader, BufferedReader, and LineReader
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int row = 0;
// Get all the lines inside the maze file
while ((line = bufferedReader.readLine()) != null) {
ArrayList < Square > lineList = new ArrayList < Square > ();
for (int i = 0; i < line.length(); i++) {
String letter = Character.toString(line.charAt(i));
if (letter.equals("L")) {
lineList.add(new LockedSquare(row, i, letter));
} else {
lineList.add(new Square(row, i, letter));
}
row++;
}
grid.add(lineList);
}
// Cut down grid to only the maze
ArrayList < ArrayList < Square >> copy = grid;
int length = grid.size();
grid = new ArrayList < ArrayList < Square >> ();
for (int i = 0; i < length - 2; i++) {
grid.add(copy.get(i));
}
// Start position
int[] startLocations = new int[2];
int index = 0;
for (int i = 0; i < copy.get(copy.size() - 2).size(); i++) {
if (!copy.get(copy.size() - 2).get(i).toString().equals(" ")) {
startLocations[index] = Integer.parseInt(copy.get(copy.size() - 2).get(i).toString());
index++;
}
}
int playerX = startLocations[0];
int playerY = startLocations[1];
}
// Exceptions
catch (FileNotFoundException e) {
System.out.println("Error: maze.txt not found");
} catch (IOException ex) {
System.out.println("Error reading maze.txt");
}
答案 0 :(得分:1)
这种情况正在发生,因为您的jar文件无法找到maze.txt
。
更改String fileName = "maze.txt";
至
String fileName = <absolute-path-for-maze.txt> ;
它应该通过jar
答案 1 :(得分:0)
Woops。我是如此愚蠢。与我的JAR位于同一文件夹中的maze.txt文件中没有内容。