my dog learned polymorphism*
The moose likes Servlets and the fly likes Absolute paths same - but 1 file can be found, and another can't Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Absolute paths same - but 1 file can be found, and another can Watch "Absolute paths same - but 1 file can be found, and another can New topic
Author

Absolute paths same - but 1 file can be found, and another can't

Mike McCoy
Greenhorn

Joined: Aug 25, 2006
Posts: 3
I have a problem with reading in from a file - when the file is created with an absolute path then file.exists() = true, but with a relative path file.exists() = false, even though both the absolute and relative file return the same canonical path. The code is presented below.

I am running on Windows XP. The class is being called from a servlet, I was unsure whether to post in the I/O forum or Servlet forum, but as the class itself is not a servlet I posted here.

The file C:\Program Files\Apache Group\Tomcat 4.1\properties\my.properties exists on the file system.

The working directory is C:\Program Files\Apache Group\Tomcat 4.1 - verified this by outputting system properties

##############################################

mSitePropPath = "properties" + File.separator;

//Create the two files
File fRelative = new File(mSitePropPath + "my.properties");
File fAbsolute = new File("C:\\Program Files\\Apache Group\\Tomcat 4.1\\properties\\my.properties");

//Compare the paths
String fRelativeCanonicalPath = fRelative.getCanonicalPath();
String fAbsoluteCanonicalPath = fAbsolute.getCanonicalPath();
System.out.println("FRelative Canonical Path[" + fRelativeCanonicalPath + "]");
System.out.println("FAbsolute Canonical Path[" + fAbsoluteCanonicalPath + "]");
System.out.println("Are paths equal[" + fRelativeCanonicalPath.equals(fAbsoluteCanonicalPath) + "]");

//Now do these files exist?
System.out.println("Frelative exists[" + fRelative.exists() + "]");
System.out.println("FAbsolute exists[" + fAbsolute.exists() + "]");
System.out.println("Compare the files[" + fRelative.compareTo() + "]");

##################################################

#Output
FRelative Canonical Path[C:\Program Files\Apache Group\Tomcat 4.1\properties\my.properties]
FAbsolute Canonical Path[C:\Program Files\Apache Group\Tomcat 4.1\properties\my.properties]
Are paths equal[true]
Frelative exists[false]
FAbsolute exists[true]
Compare the files[18]

####################################################
So, as you can see, the files have the same canonical path, but are not equal (the relative path does not even exist).
I have tried looking at the file name in different ways (getAbsolutePath(), and toURL().toExternalForm()) but the paths always come up as identical.

Any thoughts? Thanks
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

Welcome to the JavaRanch, Mike.
What version of Java are you using? I tested your code with 1.4.2_08 and 1.5.0_06 and it works as expected:


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Mike McCoy
Greenhorn

Joined: Aug 25, 2006
Posts: 3
I'm running under Java 1.4.2

For more clarity, I only get the error when running from a .war file under Tomcat (running Tomcat 4.1), called from a servlet, as mentioned above - when I call the class directly through a test class, I get the results I expect, File.exists() returns 'true' for both files.

Sorry if I didn't make that clear before.

Should I have posted this under Servlets rather than I/O forum? Still new here.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

That is odd. Let's move this conversation to the Servlets forum to see if anyone there has any ideas.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Absolute paths same - but 1 file can be found, and another can't