gpt4 book ai didi

java - 从 Java 应用程序启动 PowerScript

转载 作者:行者123 更新时间:2023-11-30 04:15:38 25 4
gpt4 key购买 nike

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

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

问题是 Java 中是否有某个类或类似的类来执行此任务 - 我没有成功地浏览了一些类,这些类的名称暗示了可能的解决方案,例如 RunnableCallable .

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

最佳答案

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();
}
}

关于java - 从 Java 应用程序启动 PowerScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com