1

I have attempted about everything I have seen regarding this issue on stackoverflow but can't seem to correctly retrieve an xml file using the .getClass method. I am continuing to get a null pointer exception but can't figure out why. I have tried several variations. If I am doing anything wrong please let me know. Also, if you require any additional information just ask :D. Here are some of the variations I have tried

URL configURL = new URL("" + Transcriber.class.getClass().getClassLoader().getResourceAsStream("/config.xml"));
ConfigurationManager cm = new ConfigurationManager(configURL);

URL configURL = new URL("" + Transcriber.class.getClass().getClassLoader().getResource("/config.xml"));
ConfigurationManager cm = new ConfigurationManager(configURL);

InputStream configURL = Transcriber.class.getClass().getClassLoader().getResourceAsStream("/config.xml");
ConfigurationManager cm = new ConfigurationManager(configURL.toString());

I should also note that when using eclipse I am able to retrieve the xml. However, when converted to an executable jar I cannot.

EDIT:

URL configURL = new URL("" + Transcriber.class.getResourceAsStream("config.xml"));

Console output =

Exception in thread "main" java.net.MalformedURLException: no protocol: java.io.BufferedInputStream@238be9f2
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at speechcapture.Transcriber.main(Transcriber.java:53)
  • 1
    Where is the `config.xml` resource in the jar? – Sotirios Delimanolis Apr 14 '14 at 06:26
  • It is located in the same package folder with Transcriber.class (bin/speechcapture/) –  Apr 14 '14 at 06:29
  • A previous post explains this problem perfectly http://stackoverflow.com/questions/1119740/loading-files-with-classloader – AurA Apr 14 '14 at 06:32
  • @AurA I seem to have attempted the approaches listed in the link unless I missed one when reading over the answers provided. –  Apr 14 '14 at 06:55

1 Answers1

2

Your first problem is that

Transcriber.class.getClass()

returns the Class object for the Class class. What you wanted was

Transcriber.class

The second problem is that, assuming your config.xml resource is in the same package as the Transcriber class, then you need to either provide the fully qualified path to the resource or get it relative to the Transcriber class' package. So either

URL configURL = Transcriber.class.getResource("config.xml");

or

URL configURL = Transcriber.class.getResource("/com/example/config.xml");
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I have tried URL configURL = new URL("" + Transcriber.class.getResourceAsStream("config.xml")); as well and config.xml is in the same package as the Transcriber class. –  Apr 14 '14 at 06:32
  • 1
    @user2916286 Both of the entries in my answer will work _if_ both the class `Transcriber` and the `config.xml` file are in the same package. Note how the `URL` is constructed. – Sotirios Delimanolis Apr 14 '14 at 06:34
  • I added it as written but I am still seeing the error null pointer exception. But with adding your's I am now seeing a malformed URL exception. –  Apr 14 '14 at 06:50
  • @user2916286 Post the exact one you're using and the full stack trace. Do this in an edit your question. – Sotirios Delimanolis Apr 14 '14 at 06:52
  • Just posted in question –  Apr 14 '14 at 07:00
  • 1
    @user2916286 Note the difference between my code and yours. Why are you constructing your `URL` like that? – Sotirios Delimanolis Apr 14 '14 at 07:02
  • I had tried both getResource then switched to getResourceAsStream. –  Apr 14 '14 at 07:06
  • @user2916286 if you're using `getResourceAsStream` why do you need a URL? – Sotirios Delimanolis Apr 14 '14 at 07:09
  • What would you recommend? I had tried InputStream as well. The issue is that while these will work in eclipse (like yours). It is giving me issues when turning it into jar. –  Apr 14 '14 at 07:14
  • @user2916286 You either use `getResourceAsStream` and get an `InputStream` or use `getResource` and get a `URL`. The way you were using it didn't make any sense. As for the Eclipse vs jar issue, make sure Eclipse is building the jar correctly with all the resources added where they should be. – Sotirios Delimanolis Apr 14 '14 at 07:21