Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Teedo

Thread: Compiles in NetBeans but cannot find txt file after I have created a .jar

  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Compiles in NetBeans but cannot find txt file after I have created a .jar

    Hey guys, like the title says....I can compile in netbeans and it runs perfectly, but after I clean and build and try to run from cmd prompt, it comes up with this error...
    It can't find the level1.txt file, though if i set the FileReader to something like "C://level1.txt" and place the file there, then it would run.... or if I set it as "level1.txt" and place the level1.txt file in the same folder as the .jar, then it would run also...Do I HAVE to do that or is there a way to make it run from the .jar?
    Thanks :D

    Java Code:
    C:\Users\Teedo\Documents\NetBeansProjects\The Tower\dist>java -jar "The_Tower.ja
    r"
    Loading Level...
    Jun 30, 2013 7:07:29 PM thetower.Level loadLevel
    SEVERE: null
    java.io.FileNotFoundException: src\thetower\levels\level1.txt (The system cannot
     find the path specified)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(Unknown Source)
            at java.io.FileInputStream.<init>(Unknown Source)
            at java.io.FileReader.<init>(Unknown Source)
            at thetower.Level.loadLevel(Level.java:41)
            at thetower.TheTower.<init>(TheTower.java:30)
            at thetower.TheTower.main(TheTower.java:51)
    
    Exception in thread "main" java.lang.NullPointerException: source
            at java.util.Objects.requireNonNull(Unknown Source)
            at java.util.Scanner.<init>(Unknown Source)
            at thetower.Level.loadLevel(Level.java:45)
            at thetower.TheTower.<init>(TheTower.java:30)
            at thetower.TheTower.main(TheTower.java:51)
    Here is the faulting code....

    Java Code:
        public void loadLevel() {
            System.out.println("Loading Level...");
            tiles = new Tile[sizeX][sizeY];
            
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader("src/thetower/levels/Level1.txt"));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Level.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            lines = new ArrayList<String>();
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    lines.add(line);
                }
            } catch (IOException ex) {
                Logger.getLogger(Level.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,232
    Blog Entries
    7
    Rep Power
    24

    Default Re: Compiles in NetBeans but cannot find txt file after I have created a .jar

    For testing purposes create a File object for your file and print the absolute path or canonical path of the file; it isn't where you supposed it should be looking for it.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Compiles in NetBeans but cannot find txt file after I have created a .jar

    Quote Originally Posted by JosAH View Post
    For testing purposes create a File object for your file and print the absolute path or canonical path of the file; it isn't where you supposed it should be looking for it.

    kind regards,

    Jos
    I have now tested this and it is looking for the file 'level1.txt' in the same directory as the_tower.jar, for some reason it's not looking INSIDE the jar?
    This is the error now after trying to run the jar from c:\
    It's fine if I place the 'level1.txt' in c:\ as well, or create a folder 'levels' and change the path to 'levels/level1.txt'...but there must be a way to have it all inside the jar file?

    Java Code:
    C:\>java -jar "The_Tower.jar"
    Loading Level...
    Path : C:\\level1.txt
    Jul 01, 2013 3:37:39 AM thetower.Level loadLevel
    SEVERE: null
    java.io.FileNotFoundException: level1.txt (The system cannot find the file speci
    fied)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(Unknown Source)
            at java.io.FileInputStream.<init>(Unknown Source)
            at java.io.FileReader.<init>(Unknown Source)
            at thetower.Level.loadLevel(Level.java:42)
            at thetower.TheTower.<init>(TheTower.java:30)
            at thetower.TheTower.main(TheTower.java:51)
    
    Exception in thread "main" java.lang.NullPointerException
            at thetower.Level.loadLevel(Level.java:49)
            at thetower.TheTower.<init>(TheTower.java:30)
            at thetower.TheTower.main(TheTower.java:51)
    
    C:\>
    Java Code:
        public void loadLevel() {
            System.out.println("Loading Level...");
            tiles = new Tile[sizeX][sizeY];
            
            File file = new File("level1.txt");
            System.out.println("Path : " + file.getAbsolutePath());
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader("level1.txt"));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Level.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,232
    Blog Entries
    7
    Rep Power
    24

    Default Re: Compiles in NetBeans but cannot find txt file after I have created a .jar

    Quote Originally Posted by Teedo View Post
    I have now tested this and it is looking for the file 'level1.txt' in the same directory as the_tower.jar, for some reason it's not looking INSIDE the jar?
    If you store something in a .jar file, that 'something' isn't a file, it is just a resource entry in the .jar file. The Class object has useful methods for those resources: getResource( ... ) and getResourceAsStream( ... )

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Compiles in NetBeans but cannot find txt file after I have created a .jar

    Thank you so much !
    It is now working like I wanted :)

    Here is the updated code for anyone else who is having this problem...

    Java Code:
        public void loadLevel() {
            System.out.println("Loading Level...");
            tiles = new Tile[sizeX][sizeY];
    
            InputStream stream = this.getClass().getResourceAsStream("levels/level1.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    DarrylBurke likes this.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,232
    Blog Entries
    7
    Rep Power
    24

    Default Re: Compiles in NetBeans but cannot find txt file after I have created a .jar

    Yep, that's the way to do it; remember if you want a resource x/y/resource it needs to stored relative to that class (i.e. in package /c/x/y where c is the package of your class); if you want a resource /x/y/resource it needs to be stored in package /x/y in the .jar file.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Netbeans created .jar file says missing main method...
    By ChexWithRaisins in forum NetBeans
    Replies: 19
    Last Post: 11-26-2011, 01:03 AM
  2. netbeans can't find class in .jar file
    By emosms in forum New To Java
    Replies: 7
    Last Post: 05-08-2011, 07:03 PM
  3. Replies: 2
    Last Post: 03-09-2009, 03:36 AM
  4. Compiles in Netbeans, error in textpad
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 05:50 AM
  5. How to find file created date.....
    By roshithmca in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2008, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •