Search code examples
javaeclipsefilenotfoundexceptionargs

Java "FileNotFoundException" when feeding arguments to Eclipse


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

//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):

enter image description here

And this is what the BMP class is doing:

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

EDIT: This is the Main (there is no args[0], they start from 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);
    }

Solution

  • Try with 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.

    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]

    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.

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