I built a project in eclipse that uses a txt file. the file is located in the main folder project.

I get the file name as argument via command line, and I get FileNotFoundException. I try to use Scanner Object and get the file name as input from eclipse.. and it's worked. (I insert only the file name : file.txt . not the full path) so why via the eclipse it's work and with command line not?

thank you! this is the exception:

  java.io.FileNotFoundException: bigMaze.txt (The system cannot find the file spec
    ified)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:146)
            at java.io.FileReader.<init>(FileReader.java:72)
            at BFS.BFS.readFile(BFS.java:43)
            at BFS.BFS.InsertMaze(BFS.java:57)
            at BFS.BFS.StartMain(BFS.java:16)
            at search.main(search.java:20)
    Exception in thread "main" java.lang.NullPointerException
            at BFS.BFS.InsertMaze(BFS.java:62)
            at BFS.BFS.StartMain(BFS.java:16)
            at search.main(search.java:20)
有帮助吗?

解决方案

If you're running from the command line, try placing the file in the same directory as the .class file

ProjectRoot
       bin
          file.txt
          program.class
       src

If the program is running from eclispe, then the file should go where you originally had it. directly under the project root.

This is all considering your running the program with String filename = "file.txt";

其他提示

I think the problem is regarding pathname of your txt file. In case of command prompt you have to provide full path like: "MyComputer://D/yourFile.txt" but using eclipse you can give just "D://yourFile.txt". It will work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top