2016-11-02 23 views
-1

Eclipseにargsを供給してJavaプログラムを実行しようとしています。これは、エラーが発生したコードの一部です:Eclipseに引数を渡すときのJava "FileNotFoundException"

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

、これは私が取得エラーです:私はRun -> Run Configurations..通過し、その後Argumentsタブの引数を供給しています

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) 

。これは(2番目の引数がエラーを出されたものである)タブのスクリーンショットです:

enter image description here

そして、これは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) 
} 

私も言及する必要があります引数タブでパスを渡そうとせず、ファイル名 "Map.bmp"だけを入力しようとしましたが、同じエラーが発生しています。誰が問題がどこにあるか知っていますか?おかげ

EDIT:これがメインです(何も引数が存在しない[0]、彼らは引数1から開始):

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

Cで試してみてください。/Users/Gian/AIprojects/Homework1/Map.bmp

"\"を使用する場合は、javaが文字列のエスケープ文字として "\"を扱うため、2回書く必要があります。

だから、どちらかが[1] [2]たぶん、あなたは、引数を使用sould 0である。またあなたはのparam引数でBMPを呼び出すEDIT

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

または

C:\\Users\\Gian\\AIprojects\\Homework1\\Map.bmp 
を使用

EDIT2

私はあなたがメインを入れているのを見ます。 最初にargs [1]が存在しないファイルを読み込みます。これは例外を作成するため、args [2]に書き込み、args [3]をotherに使用します。

なぜ、あなたはEclipseの設定で最初に追加の引数を追加しませんか?

+0

"/"と "\\"の両方を試しましたが、エラーが発生しています。 –

+0

私が理解していないことは、BMPが新しいFileOutputStreamファイルを作成する必要があることです。なぜそれ自体を作成するつもりなら、FileNotFoundExceptionを与えるべきですか? –

+1

pars args [2]を使用してBMP.saveBMPを呼び出します。これはargs listの時点で0です。多分あなたはargs [1]を使用する必要がありました – Massimo

関連する問題