cygwin下如何让java识别符号链接
这是一个非常简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也遇到了这个问题,所以我编写了一个 shell 包装器,其中包含类似的内容,
因为无论如何我都是从 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
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 aFile
object with a hardcoded path that may be a cygwin symlink. still unsure about that one... running a subprocess seems extreme and requires eithercygpath
to be on your windows path or the cygwin directory to be in the same place on every computer.我喜欢 derpaderp 的答案,所以我根据自己的需要进行了调整。更一般的是不假设 -jar 选项并且不存在包含空格的参数问题。我已将此脚本移至
/usr/bin/java
并使其可执行。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.我认为这个问题没有一个简单的答案。正如各种页面所述,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 ... :-)
您使用的 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.