Reputation: 6784
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:
Why it is domain directory? Why not the directory where my java file which has the above line of code is in?
Is there any configuration which I am not aware of , but did unknowingly, for WebLogic to look in its domain directory?
What are the implications / side effects of using above line of code in production?
Any WeLogic experts, please respond.
Thank you Regards Chaitanya
Upvotes: 0
Views: 5413
Reputation: 570595
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 (Write Once, Run Anywhere) 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.
Upvotes: 3
Reputation: 6453
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 "/".
Upvotes: 0
Reputation: 81
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
Upvotes: 0