Skip to main content



      Home
Home » Language IDEs » ServerTools (WTP) » java.io.FileNotFoundException(eclipse can not find XML file using relative path)
icon5.gif  java.io.FileNotFoundException [message #525050] Mon, 05 April 2010 11:19 Go to next message
Eclipse UserFriend
Please understand that my command of english is very poor.

I'm using 'Eclipse Java EE IDE for Web Developers'.
And Build id is '20100218-1602'

I created local server using apache-tomcat-6.0.26.

then created java source code below.

Here is my java class source.


public class JAXPTest {
public static void main(String[] args) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
/* absolute path
//InputSource is = new InputSource(new FileReader("D:\\workspace\\Study\\build\\classes\\a.xml")); */
InputSource is = new InputSource(new FileReader("a.xml")); // relative path
parser.parse(is, new DefaultHandler());
}
}

When I try debugging in IDE(eclipse), the following error stack is generated. ( Of cousre, a.xml file exists in the same directory of the JAXPTest .class file.)

Exception in thread "main" java.io.FileNotFoundException: a.xml
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at JAXPTest.main(JAXPTest.java:22)

But when I used absolute path, no error was generated.
All is good.
May be the matter is relative path.


When I use java command in JDK direct, exception is not generated.

Why eclise or tomcat local server can't find a.xml file using relative path?

please teach me in very kind english.


Re: java.io.FileNotFoundException [message #525064 is a reply to message #525050] Mon, 05 April 2010 13:11 Go to previous messageGo to next message
Eclipse UserFriend
There are a few issues here.

The key point is that when Eclipse runs a Java class, it will use a "current directory" which is the root directory of the project, not the root directory of the source tree of the project. They are not the same.

In any case, it's probably better to read files from the classpath instead of a filesystem path. You would do this with http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader. html#getResourceAsStream%28java.lang.String%29 instead of what you're doing.

A couple of other minor points:

You have a "standalone" class with a "main" method. You don't need to use Tomcat for that. Tomcat is for running web applications, not standalone classes.

When specifying a path on Windows in code, you don't need to do "D:\\stuff\\sturff". You can just do "D:/stuff/stuff". You won't need to know this when you change to use "getResourceAsStream()", but it may be useful in the future.
Re: java.io.FileNotFoundException [message #525430 is a reply to message #525064] Tue, 06 April 2010 10:48 Go to previous message
Eclipse UserFriend
Thank you.

Probly your knowledgement might save my quriosity.

I understood like following.

Because eclipse JVM use the root directory as current directory, the code reading resources from file path using relative path might generate exception.

Instead of that. It would be better that the we write codes reading resources using classpath mechanism.

And that those codes guarantees that developers need to know flatform dependent knowleges like you said.

My understanding is right??

thank you in advance for your advice.

Previous Topic:Can I link a dynamic web project source and output folders to external directories ?
Next Topic:test
Goto Forum:
  


Current Time: Tue Jul 01 05:31:03 EDT 2025

Powered by FUDForum. Page generated in 0.03192 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top