java - 从 java 运行一个 .sh 脚本

标签 java linux terminal runtime sh

我有一个游戏,我要启用自动重启功能。每24小时发生一次,很容易制作。但是,问题是,我不知道如何打开运行“run.sh”(执行 Java 游戏服务器)。

代码将首先关闭所有内容,在调用 System.exit 之前,它应该在新终端中打开 run.sh。我该怎么做?

这是我目前使用的方法,但它给了我这个错误。

public static boolean start() {
        try {
            List<String> command = new ArrayList<String>();
            command.add("cd /root/Dropbox/[SALLESY] 742 Server");
            command.add("java -Xmx1024m -Xss2m -Dsun.java2d.noddraw=true -XX:+DisableExplicitGC -XX:+AggressiveOpts -XX:+UseAdaptiveGCBoundary -XX:MaxGCPauseMillis=500 -XX:SurvivorRatio=16 -XX:+UseParallelGC -classpath bin:data/libs/* com.sallesy.Application");
            ProcessBuilder builder = new ProcessBuilder(command);
            Process proc = builder.start();
            BufferedReader read = new BufferedReader(new InputStreamReader(
                    proc.getInputStream()));
            try {
                proc.waitFor();
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
            while (read.ready()) {
                System.out.println(read.readLine());
            }
        } catch (Exception e) { 
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

错误:

sh script.sh
Cannot run program "cd /root/Dropbox/[SALLESY] 742 Server": error=2, No such file or directory
Could not start a new session...

最佳答案

尝试转义字符串:

"cd /root/Dropbox/\[SALLESY\]\ 742\ Server"

关于java - 从 java 运行一个 .sh 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21800790/

相关文章:

java - JAXB 解码器的handleEvent 未被调用

linux - awk 解析输出并提取值

linux - sudo apt update 被 ubuntu 中的 403 错误中断

ubuntu - 将终端输出到多列。

linux - Ubuntu - 终端作业执行结束后的通知/警报

java - Print Writer 和继承

java - 为什么即使对于不相等的字符串,equalsignorecase 也会返回 true

linux - 由于docker无法在ubuntu 18.04上更新和升级

linux - 如何在命令后附加字符串?

java - 什么可能导致在 GZip 压缩数据流中创建错误的 EOF