1

我正在开发一个 Java 应用程序,该应用程序在其执行的某些时候旨在运行包含 PowerBuilder 指令的文件。

第一个想法是用 Java 重写整个文件,尽管可能有一种更简洁的方法 - 这是关于从 Java 应用程序执行某些内容以启动 PowerBuilder 以执行文件的指令。

问题是Java中是否有一些类或类似的东西来执行这个任务——我没有成功地通过一些名称暗示可能的解决方案的类,例如RunnableCallable

我应该开始从 PowerScript 翻译到 Java 吗?

4

2 回答 2

1

另一种选择是将 Powerscript 代码构建到 Web 服务中并通过常规 Web 服务调用来调用它。

于 2013-08-28T19:06:01.410 回答
1
public final class PowerBuilderProcessRunner {

    private PowerBuilderProcessRunner() {}

    private static class StreamHandler implements Callable<Void> {

        private BufferedReader reader;
        private OutputStream outputStream;
        private PrintWriter writer;

        public StreamHandler(InputStream inputStream, OutputStream outputStream) {
            this.reader = new BufferedReader(new InputStreamReader(inputStream));
            this.outputStream = outputStream;
            this.writer = new PrintWriter(this.outputStream);
        }

        @Override
        public Void call() throws Exception {

            String line;
            while ((line = reader.readLine()) != null) {
                writer.println("OUTPUT FROM POWERBUILDER: " + line);
                writer.flush();
                Thread.yield();
            }

            return null;
        }

    }

    // Call this method to start powerbuilder
    public static void spawnProcess(/* arguments go here */) throws Exception {

        ProcessBuilder builder = new ProcessBuilder(
                "powerbuilder.exe", // or similar
                "arg1", // taken from arguments -> must be string
                "arg2", // taken from arguments -> must be string
        );

        Process process = builder.start();

        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        threadPool.submit(new StreamHandler(process.getInputStream(), System.out));
        threadPool.submit(new StreamHandler(process.getErrorStream(), System.err));

        process.waitFor();
    }
}
于 2013-08-28T08:11:15.753 回答