Results 1 to 7 of 7

Thread: JAR file paths

  1. #1
    atholon is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default JAR file paths

    Hi,
    I have some classes and text files in the same directory contained in a package. I ultimately want to run my program from a jar but right now I cannot seem to access the text files from inside the package.

    My classes can't find the text files when I use FileReader even though they are in the same directory. I am not sure what I can do to fix the path issue. Can someone help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If the text files will be in the Jar file, then don't access them as files but rather as resources. I'm not sure where in the tutorials this is found, but if you search the Sun Java tutorials you'll find a section on this.

  3. #3
    atholon is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    I tried:

    Java Code:
       InputStream b = FileIO.class.getClass().getResourceAsStream(fileName);
          
            try {
                //FileReader a = new FileReader(getPath(fileName));
                InputStreamReader a = new InputStreamReader(b);
    With the same result, null pointer.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    atholon is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    The classes are in the same package/same directory.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Not the classes -- where is the data file?

  7. #7
    atholon is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    This is what the jar looks like:
    cs235/
    cs235/javaskills/
    cs235/Main.class
    cs235/javaskills/TestDriver.class
    cs235/javaskills/test1.txt
    cs235/javaskills/test2.txt
    cs235/javaskills/test3.txt
    cs235/javaskills/test4.txt

    I am trying to access the text files from TestDriver
    Last edited by atholon; 05-03-2010 at 02:10 AM.

Similar Threads

  1. a little help with file paths
    By ashton in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 05:08 PM
  2. getting paths and directories
    By marco in forum Java Applets
    Replies: 3
    Last Post: 11-25-2007, 08:28 AM

Tags for this Thread

Posting Permissions

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