用Java启动Emacs并传递参数…不起作用

huangapple go评论64阅读模式
英文:

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&#39;t work
        String command = &quot;emacs -nw&quot;;
		
        // works
        //String command = &quot;emacs&quot;;

        testShellCommand(command);
    }

    public static void testShellCommand(String command) {
        try {
            ProcessBuilder processBuilder;
            String osName = System.getProperty(&quot;os.name&quot;).toLowerCase();
            if (osName.contains(&quot;win&quot;)) {
                processBuilder = new ProcessBuilder(&quot;cmd.exe&quot;, &quot;/C&quot;, command);
            } else {
                processBuilder = new ProcessBuilder(&quot;bash&quot;, &quot;-c&quot;, 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(&quot;Command executed with exit code: &quot; + exitCode);

            if (exitCode != 0) {
                System.err.println(&quot;Command execution failed with exit code: &quot; + exitCode);
                System.exit(1);
            }
        } catch (IOException | InterruptedException e) {
            System.err.println(&quot;Error executing command: &quot; + 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
&lt;Should see emacs window open here...but don&#39;t&gt;

答案1

得分: 1

你应该传递一个 String[] 以带有参数调用该命令。此外,如果你利用 ProcessBuilder.inheritIO(),一切都会简单得多。最后,你不需要指定 bashcmd。可以像这样:

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 = { &quot;emacs&quot;, &quot;-nw&quot; };
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(&quot;Command executed with exit code: &quot; + exitCode);
if (exitCode != 0) {
System.err.println(&quot;Command execution failed with exit code: &quot; + exitCode);
System.exit(1);
}
} catch (IOException | InterruptedException e) {
System.err.println(&quot;Error executing command: &quot; + e.getMessage());
System.exit(1);
}
}

huangapple
  • 本文由 发表于 2023年6月1日 09:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定