如何运行从不同的目录中的Java程序? [英] How do I run a java program from a different directory?

查看:200
本文介绍了如何运行从不同的目录中的Java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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

产生相同的结果。

produces the same results.

推荐答案

只是想一切我能想到的,我附和了命令,并看到有混合的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 /程序\\文件/...\"

Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."

我有previously遇到了这个问题,并与 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程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆