2

I got a jar file test.jar that contains a folder resources which contains txtFile.txt.

I'm trying to access the file but the file seems to be null.

package main; import java.net.URL; public class Test { private Test() { URL file = this.getClass().getClassLoader().getResource("/resources/txtFile.txt"); System.out.println(file == null); } public static final void main(String[] args) { new Test(); } } 
5
  • If you unzip the jar do you see the file? Commented Jun 21, 2015 at 9:18
  • Yes, The File is in the resource Folder inside the Jar Commented Jun 21, 2015 at 9:36
  • resource or resources? Your snippet says resources Commented Jun 21, 2015 at 9:38
  • Looks good then can you link the jar? Commented Jun 21, 2015 at 9:41
  • mediafire.com/download/yrvs73re4zcl46x/test.jar Commented Jun 21, 2015 at 9:53

5 Answers 5

1

Try using getResource("resources/txtFile.txt"); (i.e. without the first /).

There should not be a leading slash when using the ClassLoader's version of getResource, it will be interpreted as an absolute path always.

Sign up to request clarification or add additional context in comments.

2 Comments

It's because when you use Class.getResource you use a '/' to indicate that the path is absolute, not relative to the class. It strips the leading '/' before delegating to the class loader.
@roby I see, thanks for the info. It is interesting that the very upvoted answer I originally linked to got this part wrong. I have removed that part from my answer and just provided the essence of it.
0

If you are using maven, src/main/resources and src/test/resources are txtFile.txt is there in either of the two, you don't need resources/ in path. You could use this insstead

this.getClass().getClassLoader().getResource("/txtFile.txt");

Comments

0

Instead of using stream as File, you can request for InputStream using following:

InputStream in = getClass().getResourceAsStream("/txtFile.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

Comments

0

You don't have to specify the resource folder . You can directly user the file name as

getClass().getResourceAsStream("txtFile.txt"); 

1 Comment

hmm. Can just give a try to put it in a package and call using the pckage name
0

So i did this

package main; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class Test { private Test() { Class<?> c = this.getClass(); ClassLoader cl = c.getClassLoader(); InputStream[] iss = new InputStream[8]; URL[] urls = new URL[8]; iss[0] = c.getResourceAsStream("/resources/txtFile.txt"); iss[1] = c.getResourceAsStream("resources/txtFile.txt"); iss[2] = c.getResourceAsStream("/txtFile.txt"); iss[3] = c.getResourceAsStream("txtFile.txt"); iss[4] = cl.getResourceAsStream("/resources/txtFile.txt"); iss[5] = cl.getResourceAsStream("resources/txtFile.txt"); iss[6] = cl.getResourceAsStream("/txtFile.txt"); iss[7] = cl.getResourceAsStream("txtFile.txt"); urls[0] = c.getResource("/resources/txtFile.txt"); urls[1] = c.getResource("resources/txtFile.txt"); urls[2] = c.getResource("/txtFile.txt"); urls[3] = c.getResource("txtFile.txt"); urls[4] = cl.getResource("/resources/txtFile.txt"); urls[5] = cl.getResource("resources/txtFile.txt"); urls[6] = cl.getResource("/txtFile.txt"); urls[7] = cl.getResource("txtFile.txt"); for (int i = 0; i < 8; i++) System.out.println("iss[" + i + "] is " + String.valueOf(iss[i] == null)); for (int i = 0; i < 8; i++) System.out.println("url[" + i + "] is " + String.valueOf(urls[i] == null)); int read; try { while ((read = iss[0].read()) != -1) System.out.print((char) read); } catch (IOException e) { } } public static final void main(String[] args) { new Test(); } } 

Outprint:

iss[0] is false iss[1] is true iss[2] is true iss[3] is true iss[4] is true iss[5] is false iss[6] is true iss[7] is true url[0] is false url[1] is true url[2] is true url[3] is true url[4] is true url[5] is false url[6] is true url[7] is true This is sample text. 

Conclusion: It worked when I used getClass().getResource("/resources/txtFile.txt") or getClass().getClassLoader().getResource("resources/txtFile.txt") ( getResourceAsStream() also works).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.