从Java应用程序启动PowerScript

时间:2013-08-28 07:48:42

标签: java powerbuilder interaction

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

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

问题在于Java中是否有类或类似的东西来执行此任务 - 我已经通过一些名称提示可能解决方案的类(例如RunnableCallable)失败了

我应该开始从PowerScript转换为Java吗?

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:1)

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