Linux 上的 Java Runtime.exec 问题

发布于 2024-10-28 01:26:36 字数 1584 浏览 6 评论 0原文

我正在开发一个 Java 程序,旨在在 Linux 环境中使用,该程序创建一个运行另一个 Java 类的新 Java 进程,但我遇到了问题。到目前为止我终于解决了所有问题。 调用

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" })

在我的 Java 程序中

/bin/bash: /usr/lib/jvm/java-6-openjdk/jre/bin/java -classpath /home/kevin/workspace/Misc/bin HelloWorld: No such file or directory

会返回stdout/stderr。如果我尝试,

Runtime.getRuntime().exec(new String[] { "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" })

我会得到一个 Java 异常

Cannot run program "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'": java.io.IOException: error=2, No such file or directory
     ...
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory

最后,使用一个简单的方法

Runtime.getRuntime().exec("/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'")

给我一个

-classpath: -c: line 0: unexpected EOF while looking for matching `''
-classpath: -c: line 1: syntax error: unexpected end of file

来自 stdout/stderr 的异常。

同时,仅使用此文件(文件顶部没有 #!/bin/bash)创建一个新的一行 .sh 文件(并分配适当的权限)

/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'

会给出正确的输出,没有错误。

我知道 Runtime.exec 的使用非常复杂,很难完美,而且我之前已经解决了一些大问题,但是这个问题让我很困惑(例如 Runtime.exec 使用 StringTokenizer 搞砸了任何带有空格的命令在它们中,这就是为什么我调用接受 String 数组的重载)。但是,我仍然遇到问题,而且我不明白为什么。

I am working on a program in Java designed to be used on a Linux environment that creates a new Java process that runs another Java class, but I am having trouble with it. I have finally fixed all my issues up to this. Invoking

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" })

in my Java program returns

/bin/bash: /usr/lib/jvm/java-6-openjdk/jre/bin/java -classpath /home/kevin/workspace/Misc/bin HelloWorld: No such file or directory

in either stdout/stderr. If I try

Runtime.getRuntime().exec(new String[] { "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" })

I get a Java exception

Cannot run program "/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'": java.io.IOException: error=2, No such file or directory
     ...
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory

And finally, using a simple

Runtime.getRuntime().exec("/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'")

gives me a

-classpath: -c: line 0: unexpected EOF while looking for matching `''
-classpath: -c: line 1: syntax error: unexpected end of file

from stdout/stderr.

Meanwhile, creating a new one line .sh file (and assigning proper permissions) with only this (no #!/bin/bash at the top of the file)

/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'

gives the correct output with no errors.

I understand that the usage Runtime.exec is quite complicated to perfect, and I already solved some big problems I got from it before, but this problem just plain puzzles me (such as Runtime.exec's use of StringTokenizer screwing up any commands that have spaces in them, which is why I invoked the overload that accepts String arrays). However, I'm still getting problems with it and I don't understand why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雨落□心尘 2024-11-04 01:26:36

你的第一次尝试几乎是正确的。

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath /home/kevin/workspace/Misc/bin HelloWorld" })

您不需要额外的引号,因为传递单个 String 参数会自动有效地引用它。

Your first attempt was almost correct.

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath /home/kevin/workspace/Misc/bin HelloWorld" })

You don't need the extra quoting because passing individual String arguments effectively quotes it automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文