代码之家  ›  专栏  ›  技术社区  ›  anakin59490

从webapp启动远程可执行文件

  •  0
  • anakin59490  · 技术社区  · 5 年前

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

    “我开发了具有角5(前端)、Spring Booad(后端)和Java 8的Web应用程序。

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

    我的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 回复  |  直到 5 年前
        1
  •  1
  •   Ankesh    5 年前

    你可以用你的 SpringBoot 后端来实现这一点。

    在爪哇 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