3

I have tried this in many ways, and followed several online instructions, such as this - Eclipse exported Runnable JAR not showing images But so far, no success.

I have a very simple program that reads a text file (test.txt) and prints out its contents to standard output. I am using Eclipse. Here is what I have done-

  • I put the text file into a folder called 'resources'
  • I right-clicked the resources folder, selected "Build path" and hit "Use as resource folder"
  • In my code, I tried two ways of addressing the file - (a) as "/test.txt" and (b) as "resources/test.txt. The latter method works when I run the program within Eclipse. However, neither method works when I export the project as a runnable jar file. The resource folder fails to be included in the jar file.

Any help will be much appreciated.

Addendum: Here is the code.

public class FileIOTest {

public static void main(String[] args) {
    String line;
    try {
        BufferedReader br = new BufferedReader(new FileReader(new File("/test.txt")));
        while((line = br.readLine()) != null){
            System.out.println(line);
        }

    br.close(); 
    }
    catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}

}

}

Community
  • 1
  • 1
user3516726
  • 626
  • 6
  • 15
  • Show actual code of how you load the file. – Mad Physicist Dec 11 '15 at 18:29
  • public class FileIOTest { public static void main(String[] args) { String line; try { BufferedReader br = new BufferedReader(new FileReader(new File("/test.txt"))); while((line = br.readLine()) != null){ System.out.println(line); } br.close(); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} } } – user3516726 Dec 11 '15 at 18:50
  • Possible duplicate of [Eclipse exported Runnable JAR not showing images](http://stackoverflow.com/questions/25635636/eclipse-exported-runnable-jar-not-showing-images) – RisingSun Dec 11 '15 at 18:57
  • 2
    By "Show actual code" they mean put it in the question, so it's formatted and clear. – GrizzlyManBear Dec 11 '15 at 19:02
  • @khuderm, if you read my post you will see that I have checked the very post that you say is a duplicate of mine. I have already checked that one and followed the directions, to no avail. – user3516726 Dec 11 '15 at 19:33
  • @BearArmatis, I have added code to my post. I was unable to edit out the unformatted code that I initially posted. – user3516726 Dec 11 '15 at 19:34

3 Answers3

6

You can not access a resource in a Jar file through a FileReader because it is not a file on the file system. Java does have a mechanism for accessing resources in your classpath (including in Jars): ClassLoader.getResourceAsStream(). You can use it with your default classloader:

BufferedReader br = new BufferedReader(new InputStreamReader(
                        ClassLoader.getSystemClassLoader()
                                   .getResourceAsStream("test.txt")));

Note that getResourceAsStream returns null instead of throwing an exception, so you may want to get the stream first, check if for null, then either throw an exception or process it.

You will want to give the full path within the jar as your path.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 1
    It's how you access stuff that you would normally get from a ClassLoader. Basically, anything that comes from within your application distribution (i.e., is on your application's classpath) should be read this way. The `null` return value reflects this: things loaded this way are much more reliable than things from the file system. – Mad Physicist Dec 11 '15 at 19:45
2

Files work differently from resources. If you want a resource, then you should not be trying to use any of the File(Xxx) variants, which will look on the file system.

The link you provided, used Class.getResource which returns a URL, but there is also Class.getResourceAsStream which returns an InputStream, which you can wrap in an InputStreamReader. For example

InputStream is = FileIOTest.class.getResourceAsStream("/text.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2

You can create jar without eclipse. For example, if I have a Demo class in package src (directory is the same), and the resource file (named testing.txt) is in resources directory. I add a Manifest.txt file @ the root of the directory :

Manifest-Version: 1.0
Main-Class: src.Demo

The Demo class looks like :

package src;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo {

    public static void main(String... args) {

        try (BufferedReader br = new BufferedReader(new InputStreamReader(Demo.class.getResourceAsStream("/resources/testing.txt"))))
        {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The testing.txt file is like :

hello world
How are you?

Compile Demo.java to obtain Demo.class

javac src/Demo.java 

Create jar

jar cfm myJar.jar Manifest.txt src/* resources/*

And run it

java -jar myJar.jar 

In eclipse, just add the resources folder at the classpath (be sure it is in the jar), and read it as a classpath resource (the getResourceAsStream method)