2

I am trying to load a file called Test from my class, but somehow Java says, that the file does not exist, while it clearly does and neither the URL object url is null nor does it contain an invalid path and when I debug the program, the File object file has the right path stored in it. When I print out file.getPath() and paste it into the Windows Explorer it opens up just fine. I am running Eclipse, but I tried running the program in a console and it doesn't work either.

public static void main(String[] args) { URL url = Test.class.getResource("/Test"); File file = new File(url.toExternalForm()); if (!file .exists()) { System.out.println("File does not exist: " + file.getPath()); System.exit(-1); } } 

I tried it with getResource("Test"), File("Test") and File("/Test") as well, but that didn't work either. I don't know why this happens know, since I work often with files and never had a problem.

The file I want to load is located in a source folder and yes, I checked, it is recognized as a source folder in Eclipse and is in the classpath. By the way the file is actually just called Test without an extension.

Bin folder:

bin/ |___package/Test.class |___Test 

Output (Project is called "Other"):

File does not exist: file:\F:\Development\CoolDirectory\Other\bin\Test 
10
  • 1
    FilePath should not have "file:\" prefix. Commented Aug 26, 2019 at 11:34
  • 1
    Thanks @shan I found a fix thanks to you. I falsely used url.toExternalForm() instead of url.getFile() Commented Aug 26, 2019 at 11:39
  • Please tell us what you really want to do. Accessing a resource via getResource(..) is one way, but a very complicated and error prone. If you want read the file getResourceAsStream(..) is the better alternative. Commented Aug 26, 2019 at 11:41
  • Your assuming that the URL returned by getResource is a file:; that sounds like a bad idea. If the resource doesn't exist, getResource will return null. Couldn't you use url.openStream() to read the file? Commented Aug 26, 2019 at 11:46
  • There's no file extension for the string of "/Test" you provided. What type of file is this? It would make more sense to have something like "Test.txt" or "Test.pdf" as your argument. Also, it should be !file.exists() instead of !file. exists() - there shouldn't be a space. Commented Aug 26, 2019 at 11:50

1 Answer 1

3

Using Test.class.getResource("/Test"); can cause a lot of trouble as you never know if the resource is a plain file or inside a JAR file are somehow not directly accessible.

Therefore the preferred way is to use getResourceAsStream(String) which returns an InputStream you can directly read from.

The following example used Java 9+ featured:

 byte[] data = null; try (InputStream in = Test.class.getResourceAsStream("/Test")) { if (in == null) { System.out.println("Resource '/Test' does not exist"); System.exit(-1); } data = in.readAllBytes(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Why would getResource() cause troubles in a JAR? I thought the only difference between those two was that getResource returns an URL, while getResourceAsStream returns an InputStream and is a shorthand for getResource
@Sheldon A file inside a Jar file can't be accessed directly using File class. Therefore for every case you have to implement specific ways. getResourceAsStream() does everything for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.