Linux 上的 Java Runtime.exec 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5479461/
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
Java Runtime.exec woes on Linux
提问by Kevin Jin
Hey guys. I'm 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'm having trouble with it. I've finally fixed all my issues up to this. Invoking
大家好。我正在开发一个 Java 程序,该程序旨在在 Linux 环境中使用,该程序创建一个运行另一个 Java 类的新 Java 进程,但我遇到了问题。我终于解决了我所有的问题。调用
Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'" })
in my Java program returns
在我的 Java 程序中返回
/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
我得到一个 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
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)
同时,仅创建一个新的一行 .sh 文件(并分配适当的权限)(文件顶部没有 #!/bin/bash)
/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.
我知道 Runtime.exec 的使用非常复杂到完美,我已经解决了我之前从中得到的一些大问题,但是这个问题只是让我感到困惑(例如 Runtime.exec 使用 StringTokenizer 搞砸了任何有空格的命令在它们中,这就是我调用接受字符串数组的重载的原因)。但是,我仍然遇到问题,我不明白为什么。
采纳答案by geekosaur
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.
您不需要额外的引用,因为传递单个String
参数会自动有效地引用它。