tomcat project need to read file

i have a tomcat project

and have a package called com.blah.bus

under that package i have a java class, NOT A SERVLET, file that connects to a database

I want to make that java class read a con.xml file in the tomcat project

i put that file in the same directory as the class, but it didn't read....

/*java.io.File file = new java.io.File("con.xml");

if (file.exists()) {

System.out.println("!!!!!!!! !!!!!can read the file");

} else {

System.out.println("!!!!!!!! Nope");

}*/

when i tried to print out the directory files, it give me all these jar that tomcat uses, like a server directory

my question is that how do i read the con.xml file? where should i put it? thanks

[782 byte] By [khan2265a] at [2007-11-26 13:12:51]
# 1
You need to do something like thisURL url = getClass().getClassLoader().getResource("con.xml");
reflex2javaa at 2007-7-7 17:30:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
but where can i actually put the con.xml file in my tomcat project?web-inf? place where my jsp pages are?thanks for your reply
khan2265a at 2007-7-7 17:30:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
if u put it in %CATLINA_HOME%\webapps\project folder.try accesing it usingtry String URL = ServletContext.getRealPath("")+"<FILE_NAME>" the rest is all upon u of how to use it.
RahulSharnaa at 2007-7-7 17:30:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
how do i do that if it not a servlet file....its just a regular java class in a tomcat projectthanks
khan2265a at 2007-7-7 17:30:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
this gives me an exception =(java.net.URL url = getClass().getClassLoader().getResource("con.xml");i put the file in every place i couldcan someone please show me how i would access a xml file from a class in a tomcat project, im going crazy =(
khan2265a at 2007-7-7 17:30:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

The getResource() method will look in the project classpath for the file. Since you specify no subdirectories the file needs to be in the top of the classpath somewhere. That would be the WEB-INF/classes folder of your project, store the xml file there. Or a better solution would be to create a directory WEB-INF/classes/res and then opening the file as follows:

java.net.URL url = getClass().getClassLoader().getResource("res/con.xml");

That should do it.

gimbal2a at 2007-7-7 17:30:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

> i put that file in the same directory as the class,

If you read this xml file only in a standalone java class,you can put this in the same directory with class,and you can get it by:

public class Test{

void readXml(){

URL xmlUrl=Test.class.getResource("con.xml");

...

}

}

If you read this xml file within jee container enviroment,you can get the context path by HttpServletRequest object.so you can put the configuration file(con.xml) in the home directory of web module or any other customized directories,even WEB-INF folder.

Regards,

mauvespan

mauvespana at 2007-7-7 17:30:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...