如何从不同的目录运行java程序?

发布于 2024-07-29 10:38:23 字数 552 浏览 3 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

任谁 2024-08-05 10:38:23

将您的目录添加到类路径示例:

java -classpath commandprogram CommandProgram

java -classpath directory_to_program Program

add your directory to classpath example:

java -classpath commandprogram CommandProgram

or

java -classpath directory_to_program Program
情栀口红 2024-08-05 10:38:23

在尝试了我能想到的所有方法之后,我回显了该命令,发现 Cygwin 路径和 Windows 路径混合在一起。 解决方案是将脚本更改为:

#!/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/...”

我以前遇到过这个问题并用以下方法解决了它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:

#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram

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.

耀眼的星火 2024-08-05 10:38:23

您必须使用点来分隔包,而不是斜线。

java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram

you have to use a dot to separate packages, not a slash.

java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram

傲性难收 2024-08-05 10:38:23

运行java文件的通常方法是将其保存在Java/Bin文件夹中并运行cmd

C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname

如果将文件保存在不同的目录中,例如D:,您可以在 cmd 提示符下使用以下命令:

D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin

The usual way of running a java file is to save it in the Java/Bin folder and Run cmd

C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname

If you save the file in different directory such as D:, you can use the following on the cmd prompt:

D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文