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

发布于 2024-09-03 07:00:45 字数 530 浏览 5 评论 0原文

这是一个非常简单的 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());
  }
}

当我在 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

如何我说服java遵循符号链接?我希望有比自己实现重定向更简单的事情。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灼疼热情 2024-09-10 07:00:45

我也遇到了这个问题,所以我编写了一个 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

因为无论如何我都是从 cygwin shell 调用东西。如果您需要从window$环境启动脚本,请参见http://cygwin .com/ml/cygwin/2004-07/msg00163.html

cygpath 是转换路径字符串的建议方法...我到达此页面是因为我想打开一个 File 对象,具有可能是 cygwin 符号链接的硬编码路径。仍然不确定这一点...运行子进程似乎很极端,并且要求 cygpath 位于您的 Windows 路径上,或者 cygwin 目录位于每台计算机上的同一位置。

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.

╰◇生如夏花灿烂 2024-09-10 07:00:45

我喜欢 derpaderp 的答案,所以我根据自己的需要进行了调整。更一般的是不假设 -jar 选项并且不存在包含空格的参数问题。我已将此脚本移至 /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 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[@]}"
裸钻 2024-09-10 07:00:45

我认为这个问题没有一个简单的答案。正如各种页面所述,Cygwin 是一个应用程序套件而不是操作系统,并且 Sun 不支持 Cygwin 上的 Java。

但是,也许可以从源代码为 Cygwin 构建 JDK 6。当然,此页面意味着这是可能的。任何人都猜测这是否为您提供了一个能够理解 Cygwin 风格符号链接的 JDK ... :-)

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

挽心 2024-09-10 07:00:45

您使用的 Java 版本是什么? 这个 Java 教程 表明 NIO 知道文件系统链接,所以只要您是 Java1.4 或更高版本就应该没问题。它可能实际上是它正在谈论的 nio2,在这种情况下尝试使用 Java7 预发布。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文