英文:
Is it possible to load Executable once and keep calling it multiple times in Java?
问题
我正在开发一个原型,在我的Java API中必须运行一个C#可执行文件。有一段代码调用了Matlab函数。以下是调用可执行文件的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();
}
每次都加载了可执行文件。有可能只加载一次该可执行文件,然后多次调用其方法,完成所有调用后退出模型吗?
英文:
I am working on prototype where in from my Java API I have to run an executable which in C#. There is code which inturn calls Matlab function.
Following is the java code to call the executable(an example)
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.
答案1
得分: 1
你可以检查一下你的进程封装器是否已经加载,或者在构造函数中加载它。
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();
}
另外不要忘记关闭你的流,我在代码片段中使用了 try-with-resources 来实现这一点。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论