[英]Is it possible to load Executable once and keep calling it multiple times in Java?
I am working on prototype where in from my Java API I have to run an executable which in C#.我正在研究原型,我必须在我的 Java API 中运行一个 C# 中的可执行文件。 There is code which inturn calls Matlab function.
有代码反过来调用 Matlab 函数。 Following is the java code to call the executable(an example)
以下是调用可执行文件的java代码(示例)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
matProcessWrapper = new ExecWrapper.ExecWrapperBuilder
("C:\\Matlab\\HelloWorld\\bin\\Release\\netcoreapp3.1\\HelloWorld.exe")
.setErrorStream(errorStream)
.setOutputStream(outputStream)
.setTimeOutMilliSeconds(30*1000L)
.build();
try {
matProcessWrapper.executeProcessSync();
} catch (IOException e) {
e.printStackTrace();
}
The executable is loaded each time.每次都会加载可执行文件。 is it possible to load this executable only once and then call its method again and again and once all the calling is done I can exit the model.
是否可以只加载这个可执行文件一次,然后一次又一次地调用它的方法,一旦所有调用完成,我就可以退出模型。
You could check if your process wrapper is already loaded or load it in your constructor.您可以检查您的流程包装器是否已加载或将其加载到您的构造函数中。
public void execute() {
if (matProcessWrapper == null) {
loadMatProcessWrapper();
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream()) {
matProcessWrapper.executeProcessSync();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadMatProcessWrapper() {
matProcessWrapper = new ExecWrapper.ExecWrapperBuilder
("C:\\Matlab\\HelloWorld\\bin\\Release\\netcoreapp3.1\\HelloWorld.exe")
.setErrorStream(errorStream)
.setOutputStream(outputStream)
.setTimeOutMilliSeconds(30 * 1000L)
.build();
}
Also don't forget to close your streams, I did this in my code snippet with try with resources.也不要忘记关闭您的流,我在我的代码片段中使用资源进行了尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.