在Java中,是否可能加载可执行文件一次,然后多次调用它?

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月29日 13:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64113314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定