Jar 的数组超出弹跳异常,但 Eclipse 没有
Array Out of Bounce Exception for Jar, but not Eclipse
当我将我的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");
}
发生这种情况是因为您的 jar 文件无法找到 maze.txt
。
改变String fileName = "maze.txt";
到
String fileName = <absolute-path-for-maze.txt> ;
它应该在 jar
中完成
糟糕。我太傻了。我的 maze.txt 文件在与我的 JAR 相同的文件夹中没有内容。
当我将我的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");
}
发生这种情况是因为您的 jar 文件无法找到 maze.txt
。
改变String fileName = "maze.txt";
到
String fileName = <absolute-path-for-maze.txt> ;
它应该在 jar
糟糕。我太傻了。我的 maze.txt 文件在与我的 JAR 相同的文件夹中没有内容。