Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java & symbolic links in windows

Tags:

java

windows

I've been playing with java.nio.file.Files and stumbled upon a strange issue. I have a symbolic link, but Files.isSymbolicLink() and symbolic link attribute of Files.readAttributes() show different results.

Here's how I create the link:

D:\DEV\test>mklink /D link1 components
symbolic link created for link1 <<===>> components

Relevant java code:

Path symLinkDirectory = Paths.get("D:\\DEV\\test\\link1");
DosFileAttributes dosFileAttributes = Files.readAttributes(symLinkDirectory, DosFileAttributes.class);

System.out.println(String.format(
        "Files.isSymbolicLink(): %b, dosFileAttributes.isSymbolicLink(): %b", 
        Files.isSymbolicLink(symLinkDirectory), dosFileAttributes.isSymbolicLink()));

Gives me this output:

Files.isSymbolicLink(): true, dosFileAttributes.isSymbolicLink(): false

Could anyone tell me why attributes report that the file is not a symbolic link? Am I missing something? Is this happening on unix too?

like image 741
Justas Avatar asked Nov 16 '13 22:11

Justas


People also ask

What Java is used for?

Developers use Java to construct applications in laptops, data centres, game consoles, scientific supercomputers, cell phones, and other devices. Java is the world's third most popular programming language, after Python and C – according to the TIOBE index, which evaluates programming language popularity.

What exactly is Java?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power a large share of today's digital world, by providing the reliable platform upon which many services and applications are built.

Which language Java is?

The Java™ Programming Language is a general-purpose, concurrent, strongly typed, class-based object-oriented language. It is normally compiled to the bytecode instruction set and binary format defined in the Java Virtual Machine Specification.

Is Java a easy language?

Java is easy to learn. Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. Java is object-oriented. This allows you to create modular programs and reusable code.


1 Answers

You need to add LinkOption.NOFOLLOW_LINKS to the invocation of readAttributes to get the attributes of the link itself instead of the link target.

DosFileAttributes dosFileAttributes = Files.readAttributes(symLinkDirectory,
                        DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
like image 173
x4u Avatar answered Sep 29 '22 00:09

x4u