0

I have read every single question and answer regarding this topic and I have tried a multitude of the suggested solutions, none of which are working, currently I am trying to use the URL resource method but url always returns null. Here is my current pseudo setup:

/src/java/pkg/name/is/long/meatpackage/MyClassFile.java

/src/java/pkg/name/is/long/meatpackage/myresources/myresourcefile.txt

@SuppressWarnings("rawtypes")
Class thisClass = Class.forName("pkg.name.is.long.meatpackage.MyClassFile");
ClassLoader cLoader = thisClass.getClassLoader();
//note, I have tried a plethora of variations to the path for the file, this is just one example
URL url = cLoader.getResource("pkg/name/is/long/meatpackage/myresources/myresourcefile.txt");
System.out.println("Found the file!!: " + url);

Seriously, it can't be THIS hard to include a text file in your jar and access it at runtime can it?

When I list the jar it shows that the file is in pkg/name/is/log/meatpackage/myresources/myresourcefile.txt.

Another approach I have tried is:

//along with multiple path combinations for the file the InputStream is always null.    
//multiple paths include but are not limited to:
// "pkg/name/is/long/meatpackage/myresources/myresourcefile.txt"
// "/pkg/name/is/long/meatpackage/myresources/myresourcefile.txt"
// "/myresources/myresourcefile.txt"
// "/myresourcefile.txt"
// "myresourcefile.txt"
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("myresourcefile.txt"); 
BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
JoGotta
  • 215
  • 2
  • 13

1 Answers1

0

You can simplify this using getClass().getResourceAsStream() or MyClassFile.getResourceAsStream() - you don't need to talk to the classloader directly.

Note that the resource name is slash separated, and if you specify / then it's from the root of the JAR or from the base of the class path, otherwise it's implicitly from the same package directory as the class is.

For example:

package com.example; public class Foo { static { Foo.class.getResourceAsStream("bar.txt"); // looks for /com/example/bar.txt Foo.class.getResourceAsStream("/bar.txt"); // looks for /bar.txt } }

If you're developing inside Eclipse, note that the getResourceAsStream only looks inside the compiled output directory (i.e. whatever is in bin). So if you have your resource outside of there, copy it into the src directory (so it gets copied over to the bin directory at build time) and it should find it properly.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • So, does the method where this is being called need to be static? I've tried this prior to posting because it's one of the suggestions on a similar question but just to be sure I tried it again a few minutes ago. Neither methods of getResourceAsStream returns a non null InputStream. Again, when I list the contents of the jar the file is there and I have tried putting the file in just about every location you can think of. While it's really only appropriate to put it in a resources directory in the same package with the class, I have tried moving it up the directory structure nothing works. – JoGotta Jan 15 '14 at 17:07
  • Static didn't make a difference.. I changed the method to static and used the MyClassFile.class.getResourceAsStream to no avail. I am ready to give up .. – JoGotta Jan 15 '14 at 17:24
  • No, it doesn't need to be static. Verify that your file is in the compiled output directory, and ensure the file reference begins with / if you want to put in the fully qualified package. – AlBlue Jan 19 '14 at 00:58
  • I verified that the file is in the compiled jar and I have tried every combination of path imaginable .. I have also tried moving the file to various locations within the project. I finally gave up and just put the contents of the file in a class.. seems a silly thing to do but it is what it is and the sprint deadline is rapidly approaching.. – JoGotta Jan 21 '14 at 17:01