views:

153

answers:

2

Here's a very simple java program to print the first line of a file:

import java.io.*
public class test {
  public static void main(String[] args) throws IOException {
    System.out.print(new BufferedReader(new FileReader(args[0])).readLine());
  }
}

When I run this program under cygwin and pass it the name of a symbolic link, it prints the contents of the symbolic link, not the target of that link:

$ echo foo > testfile
$ ln -s testfile symlink_to_testfile
$ java test testfile
foo
$ java test symlink_to_testfile
!<symlink> ?t e s t f i l e

How do I convince java to follow the symlink? I was hoping there was something simpler than implementing the redirect myself.

A: 

I don't think there is a simple answer to this. As various pages state, Cygwin is an application suite rather than an OS, and Sun does not support Java on Cygwin.

However, it may be possible to build JDK 6 for Cygwin from the source code. Certainly, this page implies that it is possible. Whether this gives you a JDK that understands Cygwin style symbolic links is anyones guess ... :-)

Stephen C
A: 

What version of Java you are using? This Java Tutorial indicates that NIO is aware of filesystem links, so you should be aok as long as you're Java1.4 or later. It might be that it is actually nio2 it is talking about, in which case try it with the Java7 pre-release.

jowierun
This is with 1.6.0_20, HotSpot VM. The package they recommend (java.nio.file) doesn't exist until Java7. I don't think I want to go Java7 just yet.
Keith Randall