问题描述
我有一个 Java 程序,我希望它能够在我机器上的任何地方运行.我想从我的 Cygwin 命令提示符运行它.我已经编写了脚本来调用 java 程序.我将 java 程序的位置添加到类路径中,当我从 java 程序的目录中运行它们时,脚本会起作用.但是,当我尝试从任何其他目录运行时,我得到:
I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
这是我的脚本:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
将 java 行更改为以下内容:
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
产生相同的结果.
推荐答案
在尝试了我能想到的几乎所有方法后,我回显了命令,发现 Cygwin 路径和 Windows 路径混合在一起.解决方案是将脚本更改为:
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
然后CWD改为C:Program Files..."而不是/cygdrive/c/Program Files/..."
Then CWD changed to "C:Program Files..." instead of "/cygdrive/c/Program Files/..."
我之前遇到过这个问题,用cygpath -w
的方法解决了,但是后来稍微改了一下脚本,没注意到路径问题又回来了.
I had previously encountered this problem and solved it with the cygpath -w
solution, but then changed my script slightly and didn't notice that the path problem came back.
这篇关于如何从不同的目录运行 java 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!