英文:
Launching emacs from java with arguments... not working
问题
我在Linux上使用Bash shell运行,并且有一些Java代码尝试使用ProcessBuilder启动Emacs,但需要使用特定的工具参数来启动Emacs。但它总是报告错误1并失败。
最佳方法是什么?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test5 {
public static void main(String[] args) {
// 测试带参数的情况
// 不工作
String command = "emacs -nw";
// 工作
//String command = "emacs";
testShellCommand(command);
}
public static void testShellCommand(String command) {
try {
ProcessBuilder processBuilder;
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
processBuilder = new ProcessBuilder("cmd.exe", "/C", command);
} else {
processBuilder = new ProcessBuilder("bash", "-c", command);
}
Process process = processBuilder.start();
// 读取命令的输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("执行命令的退出代码:" + exitCode);
if (exitCode != 0) {
System.err.println("执行命令失败,退出代码:" + exitCode);
System.exit(1);
}
} catch (IOException | InterruptedException e) {
System.err.println("执行命令时出错:" + e.getMessage());
System.exit(1);
}
}
}
以下是我的输出:
$ javac test5.java
$ java test5
执行命令的退出代码:1
执行命令失败,退出代码:1
<这里应该看到Emacs窗口打开...但没有>
英文:
I'm running under linux with the bash shell, and
I have some java code that is attempting to launch emacs using processbuilder... I need to luanch emacs with a spefic set of arguments to the tool... instead it always reports error 1 and fails.
What's the best way to do this?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test5 {
public static void main(String[] args) {
// Test with arguments
// Doesn't work
String command = "emacs -nw";
// works
//String command = "emacs";
testShellCommand(command);
}
public static void testShellCommand(String command) {
try {
ProcessBuilder processBuilder;
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
processBuilder = new ProcessBuilder("cmd.exe", "/C", command);
} else {
processBuilder = new ProcessBuilder("bash", "-c", command);
}
Process process = processBuilder.start();
// Read the output of the command
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("Command executed with exit code: " + exitCode);
if (exitCode != 0) {
System.err.println("Command execution failed with exit code: " + exitCode);
System.exit(1);
}
} catch (IOException | InterruptedException e) {
System.err.println("Error executing command: " + e.getMessage());
System.exit(1);
}
}
}
Here's my output
$ javac test5.java
$ java test5
Command executed with exit code: 1
Command execution failed with exit code: 1
<Should see emacs window open here...but don't>
答案1
得分: 1
你应该传递一个 String[] 以带有参数调用该命令。此外,如果你利用 ProcessBuilder.inheritIO(),一切都会简单得多。最后,你不需要指定 bash 或 cmd。可以像这样:
public static void main(String[] args) {
String[] command = { "emacs", "-nw" };
testShellCommand(command);
}
public static void testShellCommand(String[] command) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.inheritIO();
Process process = processBuilder.start();
int exitCode = process.waitFor();
System.out.println("Command executed with exit code: " + exitCode);
if (exitCode != 0) {
System.err.println("Command execution failed with exit code: " + exitCode);
System.exit(1);
}
} catch (IOException | InterruptedException e) {
System.err.println("Error executing command: " + e.getMessage());
System.exit(1);
}
}
英文:
You should pass a String[] to invoke the command with arguments. Also, things are much simpler if you take advantage of ProcessBuilder.inheritIO(). Finally, you don't need to specify bash or cmd. Something like,
public static void main(String[] args) {
String[] command = { "emacs", "-nw" };
testShellCommand(command);
}
public static void testShellCommand(String[] command) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.inheritIO();
Process process = processBuilder.start();
int exitCode = process.waitFor();
System.out.println("Command executed with exit code: " + exitCode);
if (exitCode != 0) {
System.err.println("Command execution failed with exit code: " + exitCode);
System.exit(1);
}
} catch (IOException | InterruptedException e) {
System.err.println("Error executing command: " + e.getMessage());
System.exit(1);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论