向Eclipse提供参数时的Java“FileNotFoundException”

时间:2021-07-10 21:26:33

I am trying to run a Java program by feeding argsto Eclipse. This is the part of the code that raises the error:

我试图通过提供argsto Eclipse来运行Java程序。这是引发错误的代码的一部分:

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

and this is the error I get:

这是我得到的错误:

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)

I am feeding the arguments through Run -> Run Configurations.. and then on the Arguments tab. This is a screenshot of the tab (second argument is the one which is raising the error):

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

向Eclipse提供参数时的Java“FileNotFoundException”

And this is what the BMP class is doing:

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

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)
}

I should also mention that in the Arguments tab I tried not to give the path, but just the filename "Map.bmp", but it is raising the same error. Does anybody know where's the problem? Thanks

我还应该提一下,在Arguments选项卡中,我尝试不给出路径,只是文件名“Map.bmp”,但它引发了同样的错误。有谁知道问题出在哪里?谢谢

EDIT: This is the Main (there is no args[0], they start from args1):

编辑:这是主(没有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);
    }

1 个解决方案

#1


1  

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

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

If you want to use "\" you have to write it twice because java treats "\" as escape characters in strings.

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

So either use

所以要么使用

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

or

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

EDIT

Moreover you call BMP with the param args[2] which is 0. Maybe you sould use args[1]

此外,你用param args [2]调用BMP,这是0.也许你应该使用args [1]

EDIT2

I see you have put the main. At the beginning it reads from args[1] which is your file that does not exists, for this it creates an exception, then it writes to args[2] and uses args[3] for other.

我看到你把主力。开头它从args [1]读取,这是你不存在的文件,因为它创建了一个异常,然后写入args [2]并使用args [3]进行其他操作。

So why dont you add an additional argument at the beginning in you eclipse configuration ?

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

#1


1  

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

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

If you want to use "\" you have to write it twice because java treats "\" as escape characters in strings.

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

So either use

所以要么使用

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

or

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

EDIT

Moreover you call BMP with the param args[2] which is 0. Maybe you sould use args[1]

此外,你用param args [2]调用BMP,这是0.也许你应该使用args [1]

EDIT2

I see you have put the main. At the beginning it reads from args[1] which is your file that does not exists, for this it creates an exception, then it writes to args[2] and uses args[3] for other.

我看到你把主力。开头它从args [1]读取,这是你不存在的文件,因为它创建了一个异常,然后写入args [2]并使用args [3]进行其他操作。

So why dont you add an additional argument at the beginning in you eclipse configuration ?

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