【问题标题】:Using java to run multiple commands in terminal?使用java在终端中运行多个命令?
【发布时间】:2023-03-06 01:33:01
【问题描述】:

有没有办法在 Java 中编写和运行 .bat 文件然后删除它们,或者在不使用批处理文件的情况下在 java 中这样做,我需要在自己的提示实例上加载每个客户端。

这是我的加载器类

public class TBotLoader implements Runnable {

    private Thread t;
    private String name;
    private String password;
    private int script;
    private String acc;
    private String proxy;
    private int world;

    public TBotLoader(String name, String password, int script, String acc, String proxy, int world){
        this.name = name;
        this.password = password;
        this.script = script;
        this.acc = acc;
        this.proxy = proxy;
        this.world = world;
        System.out.println("Creating Thread: " + acc);
    }
    @Override
    public void run() {
        final String home = System.getProperty("user.home");
        try {
            Runtime.getRuntime().exec("java -jar " + home + "/Documents/proj/jar.jar" + " -s " + this.script + " -a " + this.acc + " -n " + this.name + " -pw " + this.password + " -w " + this.world + " -proxy " + this.proxy);
            Thread.sleep(50);
        } catch (Exception e) {
            System.out.println("Failed to spawn clients");
            System.out.println(e.getMessage());
        }
    }

    public void start() {
        System.out.println("Starting thread: " + this.acc);
        t = new Thread(this, acc);
        t.start();
    }
}

【问题讨论】:

  • 你说的是终端(unix),还有bat文件(Windows),你的目标是什么操作系统?
  • Windows,误用“终端”一词。请替换为“提示”。
  • 这里的问题很难诊断。请提供 MVCE。至于 .bat 的编写,我非常怀疑 Java 是否支持编写批处理文件,因为它们是特定于平台的。
  • .bat 文件可以只是具有该扩展名的文本文件,因此 Java 可以处理这些。
  • Java 可以编写 .bat 文件,因为它们只是带有 bat 扩展名的文本文件。

标签: java command line


【解决方案1】:

由于.bat 文件只是文本文件,您可以使用FileOutputStream 来创建它们。然后我会查看Runtime.getRuntime().exec("");ProcessBuilder 来执行它们,然后在完成后删除文件。

另见

Runtime
ProcessBuilder

【讨论】:

  • 感谢您能够理解我的问题!一切正常!再次感谢!
猜你喜欢
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
相关资源
最近更新 更多