[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

File Object Issue with Windows (Absolute Path Appending project directory)

I have a program that is using File objects that worked just fine when I was running it on Linux, but I use it on Windows and I am having a problem.

I am pulling my path string from my database and then constructing a File object. This all works fine in Linux. But, when I try to run it on Windows, it is appending what appears to be the directory that I am running the program from, to the front of my path. So, from looking over the javadocs, my best guess is that it is interpreting my path as being a _relative_ path rather than an absolute path. But, this is what I am sending from the DB: E:\dir1\dir2\subdir3. I've tried adding extra \ so that it is more or less E:\\dir1\dir2\subdir3 with no luck. (Again, JavaDocs seemed to indicate that the double \ would tell Java this is an absolute path.)

If I run tests for isDir or isFile, it returns false. It also returns false for isAbs, which would be obvious since, again, it is feeling the need to append my string to the current path.

The question is: How do I get Java to realize this is an absolute path? I've been pulling my hair out for some time on this now.

I doubt that it matters, but I'm using NetBeans for a GUI for this.
  • 7
  • 6
1 Solution
Can you post System.out.println(path);

then the code you're running to create the File
kls1Author Commented:
Relevant Print Output:

ArchiveFullDir:  E:\\MyDir\NetBeans\archive1
rootpath E:\MyDir\NetBeans\archive1
parent: E:\MyDir\NetBeans
is file:false
isDir:false exists:false
The specified directory does not exist. E:\MyDir\NetBeans\AuthoringTool\ E:\WU\NetBeans\archive1

archiveRootDir is a declared previously and initialized to E:\Mydir\NetBeans\archive

int nextArchiveIndex = getNextArchiveIndex();
        String archiveFullDir = archiveRootDir+nextArchiveIndex;
File archiveRootPath= null;
        //archiveRootPath = new File(archiveFullDir);
        archiveRootPath = new File(archiveFullDir);
        System.out.println ("rootpath" + archiveRootPath.getPath());
        System.out.println ("parent:" + archiveRootPath.getParent());
        System.out.println ("isAbs:" + archiveRootPath.isAbsolute());
        System.out.println ("is file:" + archiveRootPath.isFile());

 while (nextArchiveIndex != -1) {
            if(!archiveRootPath.isDirectory() || !archiveRootPath.exists()){
                System.out.println ("isDir:" + archiveRootPath.isDirectory() +
                                     " exists:" + archiveRootPath.exists());
                System.out.println("The specified directory does not exist. " + archiveRootPath.getAbsolutePath());
                throw new FileNotFoundException("The specified directory does not exist. " + archiveRootPath.getAbsolutePath());
 //more code using File object
nextArchiveIndex = getNextArchiveIndex();
            archiveFullDir = archiveRootDir+nextArchiveIndex;
            System.out.println("archiveFullDir: " + archiveFullDir);
            archiveRootPath= new File(archiveFullDir);
} //end while
If that path doesn't exist, you need to do

archiveRootPath= new File(archiveFullDir);
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

kls1Author Commented:
The path E:\\MyDir\NetBeans\archive1 does exist.

I don't know if I managed to change something over the last week, but this also worked on Windows about a week ago....

Is is possible there is some setting that got mixed up?
kls1Author Commented:
Just to be safe, I added archiveRootPath.mkdirs(); right after the creation of the File. It did not change any of the listed output above or seem to help any.
>>The path E:\\MyDir\NetBeans\archive1 does exist.

Can you post the output of

dir E:\MyDir\NetBeans\archive1
kls1Author Commented:
I'm not sure I follow what you mean.

If I check on the File object whether it's a directory or whether it existis, I get the following (my code for that is above)
isDir:false exists:false

If I add a call to listFiles on the File object, I get:


If you actually mean to do a directory listing from the command prompt:
C:\>dir E:\MyDir\NetBeans\archive1
 Volume in drive E is Data
 Volume Serial Number is 8430-1478

 Directory of E:\MyDir\NetBeans\archive1

05/19/2006  03:25p      <DIR>          .
05/19/2006  03:25p      <DIR>          ..
05/19/2006  03:25p                 641 1-1-1.zip
05/19/2006  03:25p                 641 1-1-2.zip
05/19/2006  03:25p                 641 1-1-3.zip
05/19/2006  03:25p                 641 1-1-4.zip
05/19/2006  03:25p                 641 1-1-6.zip
               5 File(s)          3,205 bytes
               2 Dir(s)  42,882,297,856 bytes free
kls1Author Commented:
This is what gets my attention:

The specified directory does not exist. E:\MyDir\NetBeans\AuthoringTool\ E:\MyDir\NetBeans\archive1

The dir string output came from calling archiveRootPath.getAbsolutePath()

I think that this call should only return the second part: E:\MyDir\NetBeans\archive1

So, it's clearly appending my directory rather than creating a totally fresh directory.
Personally i'd store the path as

kls1Author Commented:
I tried running this command line and it does the same thing, except it uses the directory I'm running from comand line to append my directory to.
Constructing a file can be done out of anything. Check this

                        String path = "E:\\\\dir1\\dir2\\subdir3";
                        File f = new File(path + path);
                        System.out.printf("f is absolute path?: %b\n", f.isAbsolute());
                        System.out.printf("f is directory?: %b\n", f.isDirectory());
                        System.out.printf("f is exists?: %b\n", f.exists());


f is absolute path?: true
f is directory?: false
f is exists?: false
kls1Author Commented:

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now