Keith Randall
Keith Randall

Reputation: 23265

How to get java to recognize symbolic links under cygwin

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.

Upvotes: 1

Views: 1502

Answers (4)

wnrph
wnrph

Reputation: 3393

I love derpaderp's answer so I adjusted it for my needs. It is more general in not assuming a -jar option and not having problems with arguments containing spaces. I've moved this script to /usr/bin/java and made it executable.

#!/bin/sh    

JAVA_PATH="/cygdrive/c/Program Files/Java/jre7/bin/java"


declare -a WINDOWS_ARGS
i=0
for ARG in "$@"
do

    if [ -e "$ARG" ]; then
        # pathname argument is only converted if the file exists,
        # so this trick may not be appropriate everywhere...
        WINDOWS_ARGS[$i]="`cygpath -w $ARG`"
    else
        WINDOWS_ARGS[$i]="$ARG"
    fi
    (( i++ ))
done
"$JAVA_PATH" "${WINDOWS_ARGS[@]}"

Upvotes: 2

derpaderp
derpaderp

Reputation: 21

i had this problem too, so i wrote a shell wrapper that includes something like

# java runs as native windows program, so convert pathnames
WINDOWS_ARGS=""
for ARG in $*
do
if [ -e $ARG ]
# pathname argument is only converted if the file exists,
# so this trick may not be appropriate everywhere...
then
WINDOWS_ARGS="$WINDOWS_ARGS `cygpath -w $ARG`"
else
WINDOWS_ARGS="$WINDOWS_ARGS $ARG"
fi
done
java -jar `cygpath -w myprogram.jar` $WINDOWS_ARGS

because i'm invoking things from the cygwin shell anyway. if you need to start the script from the window$ environment, see http://cygwin.com/ml/cygwin/2004-07/msg00163.html

cygpath is the suggested way to convert path strings... i arrived at this page because i want to open a File object with a hardcoded path that may be a cygwin symlink. still unsure about that one... running a subprocess seems extreme and requires either cygpath to be on your windows path or the cygwin directory to be in the same place on every computer.

Upvotes: 2

Paul Jowett
Paul Jowett

Reputation: 6581

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.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718758

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 ... :-)

Upvotes: 0

Related Questions