如何从不同的目录运行java程序?
我有一个java程序,我希望能够从我的机器上的任何地方运行它。 我想从 Cygwin 命令提示符运行它。 我已经编写了调用java程序的脚本。 我将 java 程序的位置添加到类路径中,并且当我从 java 程序的目录运行脚本时,它们可以工作。 但是,当我尝试从任何其他目录运行时,我得到:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
这是我的脚本:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
将 java 行更改为以下内容:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
产生相同的结果。
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
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的目录添加到类路径示例:
或
add your directory to classpath example:
or
在尝试了我能想到的所有方法之后,我回显了该命令,发现 Cygwin 路径和 Windows 路径混合在一起。 解决方案是将脚本更改为:
然后 CWD 更改为“C:\Program Files\...”而不是“/cygdrive/c/Program\ Files/...”
我以前遇到过这个问题并用以下方法解决了它cygpath -w 解决方案,但后来稍微更改了我的脚本,没有注意到路径问题又回来了。
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:
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
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.您必须使用点来分隔包,而不是斜线。
you have to use a dot to separate packages, not a slash.
运行java文件的通常方法是将其保存在
Java/Bin
文件夹中并运行cmd
如果将文件保存在不同的目录中,例如
D:
,您可以在 cmd 提示符下使用以下命令:The usual way of running a java file is to save it in the
Java/Bin
folder and Runcmd
If you save the file in different directory such as
D:
, you can use the following on the cmd prompt: