aspose file tools*
The moose likes Beginning Java and the fly likes How to define relative path in side java program for the following project structure? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to define relative path in side java program for the following project structure?" Watch "How to define relative path in side java program for the following project structure?" New topic
Author

How to define relative path in side java program for the following project structure?

Kumar Maddu
Greenhorn

Joined: Sep 21, 2010
Posts: 6
The following is the package structure in eclipse ide:
|--EclipseProject
| |--src
| | |--beans
| | |+Client.java
| |--applicationContext.xml

I am try to using the following two statements to access applicationContext.xml file inside the Client.java but it is printing FileNotFound.Please give me the solution how to define relative path of file.Please Generalize it to any file.


File file=new File("applicationContext.xml");
File file=new File("./applicationContext.xml");
File file=new File("../applicationContext.xml");

if(!file.exists()){
System.out.println("File Not Found");
}

It is working for only absolute path only that is for following path:
File file=new File("E:/MyWork/workspace/TestSpringPrjEnv/src/applicationContext.xml");


ALL IS WELL:
Kumar
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It depends on where you're running it from--in other words, the "current" directory.

You'd be better off loading it as a classpath resource.
Kumar Maddu
Greenhorn

Joined: Sep 21, 2010
Posts: 6
David Newton wrote:It depends on where you're running it from--in other words, the "current" directory.

You'd be better off loading it as a classpath resource.


Hi David,

Please give me example how to load it as a classpath
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://www.java2s.com/Tutorial/Java/0417__Spring/UseClassPathXmlApplicationContext.htm
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to define relative path in side java program for the following project structure?