从Webapp启动远程可执行文件

时间:2019-02-11 08:47:36

标签: java angular exe external processbuilder

好几次尝试后,我想改一下我的问题:

“我已经使用Angular 5(前端),spring-boot(后端)和Java 8开发了Web App

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

我的java类(通过使用@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。

enter image description here

我注意到此过程锁定了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

似乎启动了进程。

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

1 个答案:

答案 0 :(得分:1)

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

在Java ProcessBuilder类中可以帮助您启动命令(它将运行您的executable)。在这里,我们正在运行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