cancel
Showing results for 
Search instead for 
Did you mean: 

usage of getResourceAsStream

Former Member
0 Kudos

Hi

We are facing a strange problem with respect to usage of getResourceAsStream method in Class and ClassLoader.

primarily we have a java servlet which loads some caches reading from some xml file some configuration parameters . we were able to read these files

using .getClass().getClassLoader().getResourceAsStream()

The code snippet which loads the file in the servlet

this.getClass().getClassLoader().getResourceAsStream(file);

But simultaneously we use the same code for reading some other configuration files which is outside the Web AS .

These are stand alone java clients where we place the config xml's in the classpath and try reading using the above code , and it fails . Is there any way i can reuse this since using Class.getResourceAsStream(file) for stand alone and using this.getClass().getClassLoader().getResourceAsStream(file) will replicate the fw on both side . Is there any other way we can avoid this ?

Any help on this is appreciated .

regards

rajesh

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Rajesh,

You should be able to work with config files with your way from standalone java client if you add the configuration files to a jar file and put it in the class path. For simplicity yoy may add those in the jar with the class files.

btw, the difference between the class loader and class usage is that should put

classLoader.getResourceAsStream(com/rajesh/myconfig.xml)

class.getResourceAsStream(/com/rajesh/myconfig.xml)

or if your class is from the com.rajesh package you can just put

class.getResourceAsStream(myconfig.xml)

I am not sure if that's what you are asking by mentioning the two methods

HTH

Peter

Former Member
0 Kudos

Hi peter

Thanks for your reply , but the problem here is since these are configuration files which we require to frequently change , the approach of putting in a jar file will not be feasible . we want to have it as part of classpath . any guess as to how can i tackle this issue

regards

rajesh

Former Member
0 Kudos

Hi Rajesh,

It's not neccessary to put those in a jar file, just at least to me it's more convenient. You need to make sure the directory is in the class path. I.e. if your config file is

c:\temp\myConfig.xml

then put c:\temp in the classpath :

java -classpath ".;c:\temp" MyRemoteClient

and call the method with param "myConfig.xml".

getClassLoader().getResourceAsStream("myConfig.xml")

You may check what is the current classpath from a remote client by :

System.out.println(System.getProperty("java.class.path"));

HTH

Peter

Former Member
0 Kudos

Hi peter

I tried doing that way but still it fails .

I have my .cfg file in some other folder and put that folder in the classpath of the NWDS.but Still it fails

The correct code snippet which iam using is :

prop.load(Config.class.getClassLoader().getResourceAsStream("/"+file));

it throws java.lang.NullPointerException

at java.io.Reader.<init>(Reader.java:61)

at java.io.InputStreamReader.<init>(InputStreamReader.java:80)

at java.util.Properties.load(Properties.java:266)

at com.tpt.thresher.common.util.Config.<init>(Config.java:160)

regards

rajesh

Former Member
0 Kudos

Hi Rajesh,

try without the slash.

Config.class.getClassLoader().getResourceAsStream(file));

that should be working if you have set it correctly the class path.

Best Regards

Peter

Answers (1)

Answers (1)

Former Member
0 Kudos

getResourceAsStream internally makes use of methods <i>getResource</i> and <i>findResource</i> in the same order as specified. The search order for a perticular resource is:

1.first search the parent class loader for the resource.

2.if the parent is null the path of the class loader built-in to the virtual machine is searched.

So assuming that you are using the defualt classloader for loading your class, just check if your system property java.class.path is pointing at runtime to a proper location.

Regards,

~Amol