getContextClassLoader() returns NULL

sun67
sun67 used Ask the Experts™
on
Hello,
I have problems with this code:

public class Config   extends CompositeConfiguration {
...      
    /*Default configuration file*/
    private static final String DEFAUL_CONFIG_FILE = "c:\\j\\resources\\properties\\config.xml";
    URL configURL1 = Thread.currentThread().getContextClassLoader().getResource(configFile);
...
}

The problem is configURL1 is always NULL after the code executes. I've hardcoded the full path to config.xml to ensure that its looking in the right place, but still get NULL.
I'm using Eclipse for the IDE.

Does anybody have any ideas ?
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
A resource string in Java is always a URL. It works in relation to the classpath. What is your classpath?
Top Expert 2016

Commented:
You could try

URL configURL1 = Thread.currentThread().getContextClassLoader().getResource(new File(DEFAUL_CONFIG_FILE).toURL());

If you are referencint a File on the File System you can simply use a File
URL configURL1 = new File(configFile).toURL();
      or
The getResource method is used to pull files from the classpath so you need to specify the file has it exists on the classpath so for example if your config file exists at c:\j\resources\properties\config.xml and the c:\j folder is a folder on your classpath then you can reference it like so.      
URL configURL1 = Thread.currentThread().getContextClassLoader().getResource("resources/properties/config.xml");
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
I tried

URL configURL1 = Thread.currentThread().getContextClassLoader().getResource(new File(DEFAUL_CONFIG_FILE).toURL().toString());

but it still returns NULL,

I added c:\j\ to the classpath and tried
URL configURL1 = Thread.currentThread().getContextClassLoader().getResource("../resources/properties/config.xml");

that returned NULL as well.

I tried
configFile = "../resources/properties/config.xml";
URL configURL1 = new File(configFile).toURL();

and that works well.  I'm going to have to use getContextClassLoader() alot, so it would be nice to know why it still returns NULL. Otherwise, I'll go ahead and accept as a solution.

Thanks
Make c:\j\ be in the classpath and try
URL configURL1 = Thread.currentThread().getContextClassLoader().getResource("resources/properties/config.xml");
(IE with out the ../)

Author

Commented:
I tried
URL configURL1 = Thread.currentThread().getContextClassLoader().getResource("resources/properties/config.xml");

that is working.
getContextClassLoader() seems to work without the "../". I had to move the folder /resources/ down another level in my project directory but that's ok with me! Thank you both.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial