java cygwin下无法访问jarfile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16634211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to access jarfile under cygwin
提问by Escribblings
I know there are many "unable to access jarfile" questions on here, but I do feel this is different enough to warrant its own thread.
我知道这里有很多“无法访问 jarfile”的问题,但我确实觉得这足以保证它自己的线程。
I am writing a walkthrough, part of this walkthrough involves installing Cygwin and running a .jar file.
我正在编写演练,本演练的一部分涉及安装 Cygwin 和运行 .jar 文件。
The problem is that this .jar file needs to be called from multiple directories, and rather than have my readers have to enter the full path to the .jar every time they need to run it, I would like them only to have to enter the .jar file command after having made a simple configuration to Cygwin.
问题是这个 .jar 文件需要从多个目录调用,而不是让我的读者每次需要运行它时都必须输入 .jar 的完整路径,我希望他们只需要输入对 Cygwin 进行简单配置后的 .jar 文件命令。
I have tried adding the PATH to ~/.bashrc and have also tried adding the CLASSPATH, but have had no success.
我尝试将 PATH 添加到 ~/.bashrc 并尝试添加 CLASSPATH,但没有成功。
Every time I invoke java -jar file.jar
I get Error: Unable to access jarfile file.jar
每次我调用java -jar file.jar
我得到Error: Unable to access jarfile file.jar
What should I do to resolve this?
我应该怎么做才能解决这个问题?
[Edit]
[编辑]
I have spoken to my bro-in-law, who knows a bit about Linux, and he has suggested that I create a wrapper to execute the jar, I have had a quick search, but can't find anything simple.
我和我的姐夫谈过,他对 Linux 有点了解,他建议我创建一个包装器来执行 jar,我已经快速搜索了,但找不到任何简单的东西。
Any suggestions?
有什么建议?
回答by John Prior
Cygwin already provides a way to convert between UNIX (POSIX)-style paths and Windows (MS-DOS)-style paths, it's called "cygpath".
Cygwin 已经提供了一种在 UNIX (POSIX) 样式路径和 Windows (MS-DOS) 样式路径之间转换的方法,称为“ cygpath”。
Try:
尝试:
java -jar `cygpath -w ./foo/bar/file.jar command file.1 file.2`
Note: Those are backticks surrounding the cygpath invocation.
注意:这些是围绕 cygpath 调用的反引号。
Using cygpath eliminates the need to write your own bash script to pass things to, and the jar file can be located anywhere on your computer.
使用 cygpath 无需编写您自己的 bash 脚本来传递内容,并且 jar 文件可以位于您计算机上的任何位置。
For example, to make a bash script that minifies JavaScript code using Google's Closure Compiler in an bash environment-agnostic way:
例如,要制作一个 bash 脚本,以一种与 bash 环境无关的方式使用谷歌的闭包编译器来缩小 JavaScript 代码:
#!/bin/bash
# Script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Path to Closure Compiler
COMPILER=$HOME/archive/closure-javascript.jar
# Java requires converting paths from UNIX to Windows under Cygwin
case "$(uname -s)" in
CYGWIN*) COMPILER=$(cygpath -w $COMPILER)
esac
# Java must be in the PATH
COMMAND="java -jar $COMPILER"
# Allow overriding default filename from the command line
cd $SCRIPT_DIR
rm *.min.js > /dev/null 2>&1
# Minify all files in the directory
for js in *.js; do
$COMMAND --js=$js --js_output_file=$(basename $js .js).min.js
done
This script should execute correctly under Cygwin and Linux.
这个脚本应该在 Cygwin 和 Linux 下正确执行。
回答by Yaakov
java -jar file.jar works only if file.jar is in your current working directory. If not, you need to provide the path to file.jar. Note that if you're using Oracle Java, you need to be aware that it is a native Windows program and does not understand Cygwin's *NIX paths.
java -jar file.jar 仅当 file.jar 位于您当前的工作目录中时才有效。如果没有,您需要提供 file.jar 的路径。请注意,如果您使用的是 Oracle Java,则需要注意它是本机 Windows 程序并且不了解 Cygwin 的 *NIX 路径。
As for your brother-in-law's advice, he is correct that Java programs are usually launched via wrapper scripts on *NIX platforms. If you're using Oracle Java, that would look like:
至于你姐夫的建议,他是正确的,Java 程序通常是通过 *NIX 平台上的包装脚本启动的。如果您使用的是 Oracle Java,则如下所示:
#! /bin/sh
exec java -jar $(cygpath -w /path/to/file.jar) "$@"
回答by Escribblings
OK, this has now been resolved.
好的,现在已经解决了。
I have managed to create a bash script that calls the .jar and it's arguments, and thanks to Bernie Zimmerman's Blogi have managed to get it to run.
我已经设法创建了一个 bash 脚本来调用 .jar 和它的参数,感谢Bernie Zimmerman 的博客,我已经设法让它运行。
The issue appears to be that while Cygwin is a Linux environment using Posix filenames, Java still wants windows filenames.
问题似乎是,虽然 Cygwin 是使用 Posix 文件名的 Linux 环境,但 Java 仍然需要 Windows 文件名。
The .jar in question has 2 possible commands, and enacts these commands on a file and creates another file.
有问题的 .jar 有 2 个可能的命令,并在文件上执行这些命令并创建另一个文件。
So I can now replace
所以我现在可以替换
java -jar ./foo/bar/file.jar command file.1 file.2
java -jar ./foo/bar/file.jar command file.1 file.2
with
和
jar.sh command file.1 file.2
jar.sh command file.1 file.2
the bash script I created is:
我创建的 bash 脚本是:
#!/bin/bash
java -jar "C:\foo\bar\file.jar" "$1" "$2" "$3"
#!/bin/bash
java -jar "C:\foo\bar\file.jar" "$1" "$2" "$3"