anirudh
anirudh

Reputation: 4176

FileNotFoundException when accessing a file from JSP but works if accessed from plain java application

I am trying to access a properties file from java and it works file when I run it as a stand-alone java application. But, when I call the method accessing the file from a jsp page running on a tomcat server, I get a FileNotFoundException.

I think that when the files are deployed into a server, their path changes, and thats why the exception occurs from a web-app but not directly in java. Below is the project explorer view of my project.

enter image description here

I am accessing the nWMS properties file from the LabelRequestMessages class in java. Below is the code with which I access the file in java.

in = new FileInputStream("resources-dev/com/infosys/gidl2/shiplabel/mybatis/config/"
                            + propsDB); //propsDB has the file name
            props.load(in);

Could someone please tell me how to provide the path so that the file is accessible when deployed in a tomcat server.

Upvotes: 1

Views: 355

Answers (3)

Keerthivasan
Keerthivasan

Reputation: 12880

Try changing it to

new FileInputStream("com.infosys.gidl2.shiplabel.mybatis.config."+ propsDB);

Upvotes: 1

bdkosher
bdkosher

Reputation: 5883

You'll need to reference the file from the classpath instead. Try

in = this.getClass().getClassLoader().getResourceAsStream("om/infosys/gidl2/shiplabel/mybatis/config/nWMS_database.properties");

Make sure that the resource-dev directory is considered a source dir so that it gets included in the WEB-INF/classes of your war file.

Upvotes: 1

RMachnik
RMachnik

Reputation: 3684

You are trying to get resources inside your .war application please consider using that

private static final String ENDINGS_FILE_NAME = "com/infosys/gidl2/shiplabel/mybatis/config/" + propsDB;
...
InputStream is = getClass( ).getResourceAsStream(ENDINGS_FILE_NAME); 

But first remember to set resources folder in your web.xml, because tomcat needs to know where search it.

Upvotes: 1

Related Questions