Link to home
Start Free TrialLog in
Avatar of Juuno
Juuno

asked on

working directory in web application

I have my Java project, say in C:\User\Workspace\myApplication
And theres an xml file in that folder: C:\User\Workspace\myApplication\myXML.xml

And in my java program, I will take that xml file to retrieve information. So, I need to call it from my program. And now I am loading that file like:
File xmlfile = new File(C:\\User\\Workspace\\myApplication\\myXML.xml)

And I know that its not a good way to load a file using directly the absolute path. So, I try to call it from my program dynamically. Here, my program is web application. And I use Tomcat.

First, I tried this way:

File directory = new File(.);
String loc = directory.getCanonicalPath();
File xmlfile = new File(loc + myXML.xml);

And, second I tried using: System.getProperty(user.dir);

Both first and second way give me the same result. When I tried to test those methods as Java application, they both gave me the path : C: \User\Workspace\myApplication

But when I tried to put it in my program which accepts input from JSP and run on server, then the result path is: C:\Eclipse. So, theres an error that the system cannot find the file.

Why it cannot find the actual path?
And how can I do it to load that xml file into my Java program.

Any reply would be greatly appreciated.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You'd be better off having a resources directory under your classes folder and load it with
InputStream in = getClass().getResourceAsStream("/resources/myXML.xml");

Open in new window

Avatar of Juuno
Juuno

ASKER

yeah.. I ve already tried it... my classes folder is in :
C: \User\Workspace\myApplication\build\classes

so, i created a folder 'resources' , it's path is : C: \User\Workspace\myApplication\classes\resources

and I changed it to: InputStream in = getClass().getResourceAsStream("/resources/myXML.xml");

but there's an errror: java.lang.IllegalArgumentException: InputStream cannot be null

And since I used DOM and File to parse the xml file for DOM,  how can I change that InputStream into File path?
Your classes directory should be under WEB-INF


>>And since I used DOM and File to parse the xml file for DOM,  how can I change that InputStream into File path?

Use an InputStream - that's more flexible

http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.io.InputStream)
Avatar of Juuno

ASKER

But classes folder already exist under workspace\myApplication\build.
So, do i need to copy it under WEB-INF folder?
Let's put it this way: it needs to *end up* under WEB-INF or it won't be loaded
Avatar of Juuno

ASKER

But it's already exist in build, not under WEB-INF, and under WEB-INF, there's only one file: web.xml.
So, I tried to copy the whole 'classes' folder under WEB-INF. But there's still the same error and I couldn't do it.
Sounds like your web app doesn't observe standards. In that case, just copy resources/myXML.xml under your classes folder
The following shows what the directory structure of a web app should be (you can ignore the weblogic stuff) and your resources folder should be under classes

http://edocs.bea.com/wls/docs61/webapp/basics.html#136976
Avatar of Juuno

ASKER

ya.. I changed my classes folder output to WEB-INF/classes in buld-path. But still have that error:
java.lang.IllegalArgumentException: InputStream cannot be null
Please post the full current path to the xml file
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial