简体   繁体   English

如何让java识别cygwin下的符号链接

[英]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: 这是一个非常简单的java程序来打印文件的第一行:

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: 当我在cygwin下运行此程序并将其传递给符号链接的名称时,它会打印符号链接的内容,而不是该链接的目标:

$ 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? 我如何说服java遵循符号链接? I was hoping there was something simpler than implementing the redirect myself. 我希望有一些比自己实现重定向更简单的东西。

I love derpaderp's answer so I adjusted it for my needs. 我喜欢derpaderp的回答,所以我根据自己的需要调整了它。 It is more general in not assuming a -jar option and not having problems with arguments containing spaces. 更通用的是不假设-jar选项并且没有包含空格的参数的问题。 I've moved this script to /usr/bin/java and made it executable. 我已将此脚本移至/usr/bin/java并使其可执行。

#!/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[@]}"

i had this problem too, so i wrote a shell wrapper that includes something like 我也有这个问题,所以我写了一个shell包装器,包括类似的东西

# 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. 因为我无论如何都要从cygwin shell中调用一些东西。 if you need to start the script from the window$ environment, see http://cygwin.com/ml/cygwin/2004-07/msg00163.html 如果您需要从窗口$环境启动脚本,请参阅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. cygpath是转换路径字符串的建议方法...我到达此页面是因为我想打开一个带有硬编码路径的File对象,该路径可能是一个cygwin符号链接。 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. 仍然不确定那个...运行一个子cygpath似乎极端,并要求cygpath在你的Windows路径或cygwin目录在每台计算机上的相同位置。

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. 正如各种页面所述,Cygwin是一个应用程序套件而不是操作系统,Sun不支持Cygwin上的Java。

However, it may be possible to build JDK 6 for Cygwin from the source code. 但是, 可以从源代码为Cygwin构建JDK 6。 Certainly, this page implies that it is possible. 当然, 这个页面暗示它是可能的。 Whether this gives you a JDK that understands Cygwin style symbolic links is anyones guess ... :-) 这是否给你一个了解Cygwin风格符号链接的JDK是任何人猜... :-)

What version of Java you are using? 您使用的是哪个版本的Java? 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. 这个Java教程表明NIO知道文件系统链接,所以只要你是Java1.4或更高版本,你就应该感到厌烦。 It might be that it is actually nio2 it is talking about, in which case try it with the Java7 pre-release. 它可能是它正在讨论的实际上是nio2,在这种情况下尝试使用Java7预发行版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM