views:

302

answers:

3

Hi All,

I have a web application deployed in WebLogic. In one of my java file, I tried to read PleaseNote.txt as following:

File file = new File("PleaseNote.txt");

Now WebLogic is taking PleaseNote.txt from its domain directory.My question is:

[1] Why it is domain directory? Why not the directory where my java file which has the above line of code is in?

[2] Is there any configuration which I am not aware of , but did unknowingly, for WebLogic to look in its domain directory?

[3] What are the implications / side effects of using above line of code in production?

Any WeLogic experts, please respond.

Thank you Regards Chaitanya

A: 

Hi

It is domain directory because it's corresponds to value of user.dir system variable, the place where java reads/writes files if path not explicitly set.

Why domain directory corresponds to user.dir ? Because you start Weblogic server here.

Regards Alexander Rozhkov

Alexander Rozhkov
+3  A: 

Reading a file using that way makes your application less portable and not very robust: if you deploy your application on another application server, you'll have to find out where to put that PleaseNote.txt file again or the code will break. This breaks the WORA principle and I'd consider this as a bad practice.

So, I'd rather put this file in the classpath and use ClassLoader#getResourceAsStream(String name) to read it.

Pascal Thivent
A: 

when using new File(..) java looks for the file in the directory from where java.exe is started. In case of an weblogic domain, this is ofcourse the domain directory. This is default java behaviour.

When you want to load a file that is in de same directory as the class-file you are loading from, use ClassLoad.getResourcesAsStream(). If you want to load a resource from the classpath use the same method, but prefix your file with "/".

Salandur
What if I start WebLogic from some random directory? Will it still look in WL Domain Directory or in the random directory from where WL started?
Chaitanya MSV
i am not sure. i think it depends on the start script. if the script cd's into the domain dir, Java will look for the files in de domain dir, otherwise in the directory from where weblogic is started
Salandur