如何从其他目录运行java程序?

时间:2023-01-13 16:33:13

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程序,我希望能够在我的机器上的任何地方运行。我想从我的Cygwin命令提示符运行它。我已经制作了脚本来调用java程序。我将java程序的位置添加到类路径中,当我从java程序的目录运行它们时脚本工作。但是,当我尝试从任何其他目录运行时,我得到:

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行更改为以下内容:

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

produces the same results.

产生相同的结果。

4 个解决方案

#1


add your directory to classpath example:

将您的目录添加到classpath示例:

java -classpath commandprogram CommandProgram

or

java -classpath directory_to_program Program

#2


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:

在尝试了我能想到的一切之后,我回应了命令,看到Cygwin路径和Windows路径混合在一起。解决方案是将脚本更改为:

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

然后CWD改为“C:\ Program Files \ ...”而不是“/ 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.

我之前遇到过这个问题并用cygpath -w解决方案解决了这个问题,但后来稍微改变了我的脚本并且没有注意到路径问题又回来了。

#3


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

你必须用一个点来分隔包,而不是斜线。

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

java -cp“$ C​​WD / classes; $ CWD / lib / AJarFile.jar”commandprogram.CommandProgram

#4


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

运行java文件的常用方法是将其保存在Java / Bin文件夹和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 :)中,则可以在cmd提示符下使用以下命令:

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

#1


add your directory to classpath example:

将您的目录添加到classpath示例:

java -classpath commandprogram CommandProgram

or

java -classpath directory_to_program Program

#2


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:

在尝试了我能想到的一切之后,我回应了命令,看到Cygwin路径和Windows路径混合在一起。解决方案是将脚本更改为:

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

然后CWD改为“C:\ Program Files \ ...”而不是“/ 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.

我之前遇到过这个问题并用cygpath -w解决方案解决了这个问题,但后来稍微改变了我的脚本并且没有注意到路径问题又回来了。

#3


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

你必须用一个点来分隔包,而不是斜线。

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

java -cp“$ C​​WD / classes; $ CWD / lib / AJarFile.jar”commandprogram.CommandProgram

#4


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

运行java文件的常用方法是将其保存在Java / Bin文件夹和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 :)中,则可以在cmd提示符下使用以下命令:

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