Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a bug in java jdk?

Tags:

When I got a java.lang.File class with the code File file = new File("e:/");, of course I got a File class represented the e:\ directory.

But if I got a File class with code File file = new File("e:"); and I just in the drive E:, then I got a File class represented current directory.

Assume I'm in directory E:\dir\, And this directory have a file named Test.java. It's content is:

import java.io.File;
public class Test {
    public static void main(String[] args) {
        File file = new File("e:"); 
        File[] files = file.listFiles(); 
        for(File f: files){ 
            System.out.println(f + " " + f.exists()); 
        }
    }
}

Open the cmd tool and navigate to the directory e:\dir, execute the following command in it:

E:\dir> javac Test.java
E:\dir> java Test

I got:

e:\Test.class false
e:\Test.java false

Is this a java jdk bug?


Additional information from @JimGarrison:

I ran this code

public class Foo3
{
    public static void main(String[] args)  throws Exception
    {
        File f = new File("D:");
        System.out.println(f.getCanonicalPath());
        for (File x : f.listFiles())
            System.out.println(x + " " + x.getCanonicalPath() + " " + x.getAbsolutePath() + " " + x.exists() + " " + x.getAbsoluteFile().exists());
    }
}

in Eclipse (which lives on my D: drive) and got the following output:

D:\dev\src\pdxep
D:\.classpath D:\dev\src\pdxep\.classpath D:\dev\src\pdxep\.classpath false true
D:\.project D:\dev\src\pdxep\.project D:\dev\src\pdxep\.project false true
D:\.settings D:\dev\src\pdxep\.settings D:\dev\src\pdxep\.settings false true
D:\gallery D:\dev\src\pdxep\gallery D:\dev\src\pdxep\gallery false true
D:\pom.xml D:\dev\src\pdxep\pom.xml D:\dev\src\pdxep\pom.xml false true
D:\src D:\dev\src\pdxep\src D:\dev\src\pdxep\src false true
D:\target D:\dev\src\pdxep\target D:\dev\src\pdxep\target false true

Which confirms there's something funny going on.

Java Bug 8130462 seems to be related as it has to do with relative vs absolute paths specifically in Windows.

like image 791
xuejianbest Avatar asked Mar 25 '16 01:03

xuejianbest


1 Answers

It's not a bug.

  • E:/ means that you specify both a drive and a directory

  • E: means you only specify a drive, the directory is left to the default value.

Note: Now what people think of as current directory is actually default directory. i.e. what is applied by default when none is specified. It's just the same if you don't specify the drive at all, the default (current default) will apply.

This is the way it works on most File Systems.

like image 83
Alain Pannetier Avatar answered Nov 02 '22 05:11

Alain Pannetier