0

I am a bit confused with path in Java (using Eclipse). This is my file structure:

 Folder

    Subfolder

        file.txt

    jarfile.jar

So, I am trying to make the jar file parse data from file.txt and I use the following code:

Scanner in = new Scanner(this.getClass().getResourceAsStream("./Subfolder/file.txt"));

I have made a runnable jar file with Eclipse, put it in the Folder, but it does not work. What is it that I am doing wrong?

1
  • I'm not sure if this is helpful - but I sometimes need to check where my code is running from to work out relative paths so I just do (new java.io.File(".").getCanonicalPath(). Commented Jun 30, 2013 at 17:17

4 Answers 4

2

Since you use a resource file via a Class object, the path to the resource must be absolute:

getClass().getResourceAsStream("/Subfolder/file.txt");

Note that doing what you do is a bad idea, that is, opening a scanner on a resource which you don't have a reference to:

new Scanner(someInputStreamHere());

you have no reference to that input stream, therefore you cannot close it.

What is more, .getResource*() return null if the resource does not exist; in this case you'll get an NPE!

Recommended if you use Java 6 (using Guava's Closer):

final URL url = getClass().getResource("/path/to/resource");

if (url == null) // Oops... Resource does not exist
    barf();

final Closer closer = Closer.create();
final InputStream in;
final Scanner scanner;

try {
    in = closer.register(url.openStream());
    scanner = closer.register(new Scanner(in));
    // do stuff
} catch (IOException e) {
    throw closer.rethrow(e);
} finally {
    closer.close();
}

If you use Java 7, just use a try-with-resources statement:

final URL url = getClass().getResource("/path/to/resource");

if (url == null) // Oops... Resource does not exist
    barf();

final InputStream in;
final Scanner scanner;

try (
    in = url.openStream();
    scanner = new Scanner(in);
) {
    // do stuff
} catch (IOException e) {
    // deal with the exception if needed; or just declare it at the method level
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just as an example because java is platform independent look at getting the relative absolute or canonical path as needed I hope this gives you an idea of what to do.

/**
 * This method reads the AcronymList.xlsx and is responsible for storing historical acronyms
 * and definitions.
 * @throws FileNotFoundException
 * @throws IOException
 * @throws InvalidFormatException 
 */
public file readAcronymList() throws FileNotFoundException, IOException, InvalidFormatException {
    String accListFile = new File("src\\org\\alatecinc\\acronymfinder\\dal\\acIgnoreAddList\\AcronymList.xlsx").getCanonicalPath();
    File acFile = new File(accListFile).getAbsoluteFile();
    return acFile;
}

Comments

0

Use following code.

Scanner in = new Scanner( getClass().getResource("Subfolder/file.txt"));

1 Comment

Nope... .getResource() returns a URL
0

Why resource? Is the txt file embedded in the Jar file? It will load the file from the jar.

Just use a File or FileInputStream with the path you already put.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.