Saravanan
Saravanan

Reputation: 11592

How to give relative path for my file in java file?

I am using tomcat server for my java application(Servlet,jsp).In my servlet page calling one java class function.Actually the java file is written separately and i will call the function written inside it.With in the function, i have to read one config(user defined) file for some purpose.so, i am using File class for that.

But, here i have to give relative path of the config file(user defined).Because, now i am running this application in local windows server.But my live server is based on Linux.So, the file path is changed in linux.

File f1=new File("D:\tomcat\webapp\myapp\WEB-INF\src\point_config.txt");  -- windows
File f1=new File("D:\ravi\tomcat\webapp\myapp\WEB-INF\src\point_config.txt");  -- linux

So, i have to give relative path of the file that is common to both windows and linux machine.

Is there a way to do this?

Please guide me to get out of this issue?

Upvotes: 2

Views: 6315

Answers (2)

Deepak Bala
Deepak Bala

Reputation: 11185

The path of the config file leads into the WEB-INF folder

tomcat\webapp\myapp\WEB-INF\src\point_config.txt

Anything inside WEB-INF is protected and cannot be user-defined once the web application has launched. If you meant to read from a user-defined configuration file from the file system, please use an API like the common configuration API.

If you want to insist on keeping the file inside the WEB-INF folder, use the Class.getResourceAsStream() method to obtain the configuration instead. That would not make the configuration user-defined though.

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

Place your config file under your webapp WEB-INF/classes folder and read like this in code

InputStream is= 
   YourClassName.class.getResourceAsStream("point_config.txt");

Upvotes: 6

Related Questions