从 Webapp 启动远程可执行文件

Launch remote executable from Webapp

好的,经过几次尝试,我想重新表述一下我的问题:

“我开发了 Web 应用程序 Angular 5(前端),spring-boot(后端)和 Java 8

下一步是从界面启动安装在远程服务器上的合作伙伴软件。 这是一个带有一些参数的 .exe 程序,但我想通过启动 putty

来测试

我的javaclass(使用@Ankesh 回答)

@Service
public class DictService extends CoreServices {

   public ApiResponse<?> launch(String idWS, String ipp, String nom, String prenom, String ddn) {

    try {
        boolean isWindows = System.getProperty("os.name")
                  .toLowerCase().startsWith("windows");

        String path = "C:\klinck\PuTTY\putty.exe";
        ProcessBuilder builder = new ProcessBuilder();
        if (isWindows) {
            // builder.command("cmd.exe", "/c", "dir");
             builder.command("cmd.exe", "/c", path);
        } else {
            // this is for bash on linux (can be omitted)
            builder.command("sh", "-c", "ls");
        }
        System.out.println(builder);
        // builder.directory(new File(System.getProperty("user.home")));
        // Start the process here
        // Redirect the errorstream
          builder.redirectErrorStream(true);
          System.out.println(""+builder.redirectErrorStream());
        System.out.println("before start");
        Process process = builder.start();
        System.out.println("after start");
        // Follow the process to get logging if required
        StreamGobbler  streamGobbler = 
          new StreamGobbler (process.getInputStream(), System.out::println);

        //Submit log collection to and executor for proper scheduling and collection of loggs
        System.out.println("before executors submit");
        Executors.newSingleThreadExecutor().submit(streamGobbler);

        // Collect exit code
        System.out.println("before waitFor");
        int exitCode = process.waitFor();
        System.out.println("after waitFor");
        // validate if the appliction exited without errors using exit code
        assert exitCode == 0;

    } catch (Exception ure) {
        return new ApiResponse<>(false, "Une erreur interne est survenue. Merci de contacter le support", null, 0);
    }

    return new ApiResponse<>(true, null, null, 0);
}
private static class StreamGobbler implements Runnable {
    private InputStream inputStream;
    private Consumer<String> consumer;

    public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
        this.inputStream = inputStream;
        this.consumer = consumer;
    }

    @Override
    public void run() {
        System.out.println("StreamGlobber run");
        new BufferedReader(new InputStreamReader(inputStream)).lines()
          .forEach(consumer);
    }
}

}

进程已启动但未显示 GUI。

我注意到这个进程锁定了 tomcat 日志文件(stderr 和 stdout)

我看到这是可能的: Best Way to Launch External Process from Java Web-Service? Executing external Java program from a webapp

但是我没有成功修改这段代码。

这是日志:

java.lang.ProcessBuilder@7b1ead05
true
before start
after start
before executors submit
before waitFor
StreamGlobber run

似乎进程已启动。

我不明白....我该如何解决?

您可以使用 SpringBoot 后端来实现此目的。

在java ProcessBuilder class中可以帮助你启动命令(这将运行你的executable)。这里我们 运行ning cmd.exe 在系统路径上可用。

ProcessBuilder builder = new ProcessBuilder();
if (isWindows) {
    builder.command("cmd.exe", "/c", "dir");
} else {
    // this is for bash on linux (can be omitted)
    builder.command("sh", "-c", "ls");
}
builder.directory(new File(System.getProperty("user.home")));
// Start the process here
Process process = builder.start();

// Follow the process to get logging if required
StreamGobbler streamGobbler = 
  new StreamGobbler(process.getInputStream(), System.out::println);

//Submit log collection to and executor for proper scheduling and collection of loggs
Executors.newSingleThreadExecutor().submit(streamGobbler);

// Collect exit code
int exitCode = process.waitFor();
// validate if the appliction exited without errors using exit code
assert exitCode == 0;

参考:https://www.baeldung.com/run-shell-command-in-java