Results 1 to 5 of 5
  1. #1
    nandj987 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default File not found when using jar

    I'm using Eclipse for development. I can run my code inside the Eclipse environment without a problem but when I export the project to a runnable jar, I get a FileNotFoundException when trying to execute the jar file.

    Here are the results of running from the command line:
    prmpt> java -jar GamesAhoy.jar
    Opening file: GamesAhoy/SightWords.txt
    Resource path:file:/C:/GamesAhoy.jar!/GamesAhoy/SightWords.txt
    java.io.FileNotFoundException: file:\C:\GamesAhoy.jar!\GamesAhoy\SightWords.txt (The filename, directory name, or volume label syntax is incorrect)


    I took a look at the jar file and it appears to be in the file. Below is the contents of the jar file:

    GamesAhoy.jar
    |GameAhoy
    |---WordList.class
    |---SightWords.txt
    |META-INF
    |---MANIFEST.MF


    The code I'm trying to execute is below:


    private void LoadWordFile( String acWordFilePath )
    {
    Scanner lcScanner = null;

    System.out.println("Opening file: " + acWordFilePath);
    Class myClass = WordList.class;
    ClassLoader loader = myClass.getClassLoader();
    URL myURL = loader.getResource(acWordFilePath);
    String path = myURL.getPath();
    path = path.replaceAll("%20", " ");
    System.out.println("Resource path:" + path);
    lcScanner = new Scanner(new BufferedReader(new FileReader(path)));
    }

    I call the above code like this: LoadWordFile("GamesAhoy/SightWords.txt");


    Eclipse information:
    Version: 3.6.1
    Build id: M20100909-0800

    Any help would be appreciated. Thanks in advance.

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

    Default

    Files don't exist inside of Jars; resources do. So don't try to read anything held in the Jar as a File as it simply won't work. The Class class has a getResourceAsStream method and you can use this to get your resource from the Jar, then use an InputStreamReader then a BufferedReader.

  3. #3
    nandj987 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks! Worked like a charm.

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

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

Similar Threads

  1. file not found
    By smartshahezad in forum JDBC
    Replies: 2
    Last Post: 03-04-2010, 11:14 AM
  2. Javac, file not found
    By Zorobay in forum New To Java
    Replies: 1
    Last Post: 02-15-2010, 11:30 AM
  3. javac:file not found
    By lovearora in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 03:20 AM
  4. javac file not found
    By teamkeff in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 10:17 PM
  5. file not found outside of Netbeans
    By annie in forum NetBeans
    Replies: 0
    Last Post: 06-21-2008, 10:10 PM

Posting Permissions

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