Search code examples
javafilenotfoundexceptionargs

How do I make the args function equal to 2? As it is just throwing File Not found exception


/**
  *   Compilation  : javac Traversal.java
  *   Execution    : java Traversal args0 ----- GraphicsMode
  *                : java Traversal args0 args1 ----- TextMode
  *
  *   Arguments    : args0 - boardfilename.txt
  *                     args1 - movesfilename.txt
  *
  *   Controls     : h - left movement
  *                  l - right movement
  *                  j - down movement
  *                  k - up movement
  **/   
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
        if (args.length == 1) {
            GameBoard board = Middleware.parseArgs(args[0]);
            Middleware.validateBoard(board);
            GraphicsMode.play(board);
        }else if (args.length == 2) {
            GameBoard board = Middleware.parseArgs(args[0]);
            Middleware.validateBoard(board);
            String moves = Middleware.readMoves(new Scanner(new File(args[1])));
            TextMode.play(board, moves);
        }
        exit();
    }

    /**
    * Sleep for 4000 microseconds to allow
    * the final event sound to finish
    * playing and then exits.
    */
    public static void exit() throws InterruptedException {
        Thread.sleep(4000);
        System.exit(0);
    }

Solution

  • As already mentioned in your JavaDoc, you need to run this program from the command prompt (cmd window) as follows if you want two arguments to be passed to the program:

    java Traversal args0 args1
    

    where

    args0 - boardfilename.txt                  
    args1 - movesfilename.txt
    

    In other words, run it as java Traversal boardfilename.txt movesfilename.txt from the command prompt. If boardfilename.txt and movesfilename.txt are not in the same location where Traversal.class is, mention their complete paths i.e.

    java Traversal path-to-boardfilename.txt path-to-movesfilename.txt
    

    If you are running it in eclipse, right-click Traversal.java > Click Run Configurations... > Put the values in the dialog box as shown in the screenshot > Click Run

    enter image description here

    In this case (i.e. when you are trying to run it from eclipse), make sure boardfilename.txt and movesfilename.txt are there in the parent folder of src.