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

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

How do I run a java program from a different directory?

javabashscriptingcygwin

提问by Swoogan

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 "
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
"` java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram

Changing the java line to the following:

将 java 行更改为以下内容:

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

produces the same results.

产生相同的结果。

采纳答案by Swoogan

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 路径混合在一起。解决方案是将脚本更改为:

java -classpath directory_to_program Program

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 -wsolution, but then changed my script slightly and didn't notice that the path problem came back.

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

回答by woakas

add your directory to classpath example:

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

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

or

或者

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

回答by cd1

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

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

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

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

回答by Kaushik

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

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

##代码##

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

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

##代码##