Java "FileNotFoundException" 向 Eclipse 提供参数时

Java "FileNotFoundException" when feeding arguments to Eclipse

我正在尝试通过将 args 提供给 Eclipse 来 运行 一个 Java 程序。这是引发错误的代码部分:

//Store the occupancy matrix in a bitmap image
    new BMP().saveBMP(args[2],occupancy);

这是我得到的错误:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\Gian\AIprojects\Homework1\Map.bmp (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Main.main(Main.java:31)

我通过 Run -> Run Configurations.. 提供参数,然后在 Arguments 选项卡上提供参数。这是选项卡的屏幕截图(第二个参数是引发错误的参数):

这就是 BMP class 正在做的事情:

public void saveBMP(String filename, int [][] rgbValues){
        try {
            FileOutputStream fos = new FileOutputStream(new File(filename));

            bytes = new byte[54 + 3*rgbValues.length*rgbValues[0].length + getPadding(rgbValues[0].length)*rgbValues.length];

            saveFileHeader();
            saveInfoHeader(rgbValues.length, rgbValues[0].length);
            saveRgbQuad();
            saveBitmapData(rgbValues);

            fos.write(bytes);

            fos.close();

        } catch (FileNotFoundException e)
}

我还应该提到,在“参数”选项卡中,我尝试不提供路径,而只提供文件名 "Map.bmp",但它引发了同样的错误。有人知道问题出在哪里吗?谢谢

编辑:这是主要的(没有 args[0],它们从 args1 开始):

public class Main {
    public static void main(String[] args) throws FileNotFoundException, IOException, Exception{

        //Input file must be specified in args[1]
        BufferedReader reader = new BufferedReader(new FileReader(new File(args[1])));

        int v = Integer.parseInt(args[3]);
        // int v = 0;
        int m = (int) pow(2,v);
        System.out.println("M: " + m);

        //Read from the file and set the grid dimensions
        StringTokenizer tokens = new StringTokenizer(reader.readLine());
        int maxX = Integer.parseInt(tokens.nextToken())*m;
        int maxY = Integer.parseInt(tokens.nextToken())*m;
        System.out.println("maxX: " + maxX + " maxY: " + maxY);

        //Store the matrix of integers that will be written in the .bmp file
        //and initialize it with white cells
        int[][] occupancy = new int[maxX][maxY];
        for(int i=0; i<maxX; i++)
            for(int j=0; j<maxY; j++)
                occupancy[i][j] = 16777215;

        //Read from the file the start and the goal position
        //and store them in the occupancy matrix
        tokens = new StringTokenizer(reader.readLine());
        XYLocation robot = new XYLocation(Integer.parseInt(tokens.nextToken())*m,Integer.parseInt(tokens.nextToken())*m);
        System.out.println("Robot");
        System.out.println("X: " + robot.getXCoOrdinate() + " Y: " + robot.getYCoOrdinate());
        occupancy[robot.getYCoOrdinate()][robot.getXCoOrdinate()] = 255;
        tokens = new StringTokenizer(reader.readLine());
        XYLocation finish = new XYLocation(Integer.parseInt(tokens.nextToken())*m,Integer.parseInt(tokens.nextToken())*m);
        System.out.println("Goal");
        System.out.println("X: " + finish.getXCoOrdinate() + " Y: " + finish.getYCoOrdinate());
        occupancy[finish.getYCoOrdinate()][finish.getXCoOrdinate()] = 65280;

        //Build the environment
        Environment init = new Environment(robot);
        Environment.setFinish(finish);
        int numWalls = Integer.parseInt(reader.readLine());
        for(int i=0; i < numWalls; i++) {
            tokens = new StringTokenizer(reader.readLine());
            XYLocation temp = new XYLocation(Integer.parseInt(tokens.nextToken())*m,Integer.parseInt(tokens.nextToken())*m);
            for(int x=0; x < m; x++)
                for(int y=0; y < m; y++) {
                    int newX = temp.getXCoOrdinate()+x;
                    int newY = temp.getYCoOrdinate()+y;
                    Environment.addWall(new XYLocation(newX,newY));
                    occupancy[newY][newX] = 0x000000;
            }
        }

    //Store the occupancy matrix in a bitmap image
        new BMP().saveBMP(args[2],occupancy);
        // new BMP().saveBMP("Map.bmp",occupancy);

      //Use AIMA framework to solve the problem
        Problem problem = new Problem(init, RobotFunctionFactory.getActionsFunction(),
                RobotFunctionFactory.getResultFunction(), new RobotGoalTest());

        RobotWithAStarSearch(problem, robot, occupancy);
        RobotWithDepthFirstSearch(problem, robot, occupancy);
    }

尝试使用 C:/Users/Gian/AIprojects/Homework1/Map.bmp

如果你想使用“\”,你必须写两次,因为java将“\”视为字符串中的转义字符。

所以要么使用

C:/Users/Gian/AIprojects/Homework1/Map.bmp

C:\Users\Gian\AIprojects\Homework1\Map.bmp

编辑

此外,您调用 BMP 时参数 args[2] 为 0。也许您应该使用 args[1]

编辑2

我看到你把主要的。 一开始它从 args[1] 读取,这是你不存在的文件,为此它创建一个异常,然后它写入 args[2] 并使用 args[3] 用于其他。

那么为什么不在 eclipse 配置的开头添加一个额外的参数呢?