bash 从 Cygwin 设置 CLASSPATH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6163073/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:04:45  来源:igfitidea点击:

Setting the CLASSPATH from Cygwin

javabashcygwinclasspath

提问by parker.sikand

I use Eclipse to develop Java so I have a folder full of Eclipse Java Project folders. The /bin folder resides in each folder, so to run the project from Cygwin, the classpath must be set (on my system) to: "E:/programming/java/workspace/SomeProject/bin". Since there are ~40 projects in my folder, I'd rather make a script to add the paths to the CLASSPATH. My script seems to add the paths to CLASSPATH, but when I try to run Java I get a class not found error. In my .bashrc here is my script:

我使用 Eclipse 来开发 Java,所以我有一个充满 Eclipse Java Project 文件夹的文件夹。/bin 文件夹位于每个文件夹中,因此要从 Cygwin 运行项目,必须将类路径设置为(在我的系统上):“E:/programming/java/workspace/SomeProject/bin”。由于我的文件夹中有大约 40 个项目,我宁愿编写一个脚本来将路径添加到 CLASSPATH。我的脚本似乎将路径添加到 CLASSPATH,但是当我尝试运行 Java 时,我收到一个找不到类的错误。在我的 .bashrc 中,这是我的脚本:

JAVAWORKSPACE="/cygdrive/e/programming/java/workspace/*"
BIN="/bin;"
for f in $JAVAWORKSPACE
do
    if [ -d $f ] ; then
        export CLASSPATH="$f$BIN$CLASSPATH"
    fi
done

When I start Cygwin and echo $CLASSPATH, all of the directories show up, but java can't find the classes. I have also tried JAVAWORKSPACE="E:\programming\java\workspace\*but this resulted in nothing being added to CLASSPATH. If I go through the Windows settings and manually enter "E:/programming/java/workspace/MyProject/bin" to the CLASSPATH, command line Java has no trouble finding the classes. What's up with this? I'm not sure if it's a problem with the script or if CLASSPATH doesn't like unix-style paths. If I need to add windows paths, please help me change my script to do this. Thanks!

当我启动 Cygwin 并回显 $CLASSPATH 时,所有目录都显示出来,但 java 找不到这些类。我也尝试过,JAVAWORKSPACE="E:\programming\java\workspace\*但这导致没有向 CLASSPATH 添加任何内容。如果我通过 Windows 设置并在 CLASSPATH 中手动输入“E:/programming/java/workspace/MyProject/bin”,命令行 Java 可以轻松找到这些类。这是怎么回事?我不确定这是脚本的问题还是 CLASSPATH 不喜欢 unix 样式的路径。如果我需要添加 Windows 路径,请帮助我更改脚本以执行此操作。谢谢!

回答by P. Myer Nore

I don't have Cygwin set up right now, but I ran into this problem a number of years ago. Java knows nothing about Cygwin pathnames, and bash treats a single backslash as an escape character, stripping it before it can be transmitted to java(c). If you do

我现在没有设置 Cygwin,但几年前我遇到了这个问题。Java 对 Cygwin 路径名一无所知,bash 将单个反斜杠视为转义字符,在将其传输到 java(c) 之前将其剥离。如果你这样做

echo E:\programming\java\workspace\*

You'll see it outputs E:programmingjavaworkspace*, not what you're expecting. The key is to either escape the escape chars, like

您会看到它输出 E:programmingjavaworkspace*,而不是您所期望的。关键是要么逃避转义字符,比如

E:\programming\java\workspace\*

or even better, use cygpathlike this.

或者甚至更好,像这样使用cygpath