简体   繁体   English

如何在Java中执行“带有返回值的exe”?

[英]How to execute “an exe with return value” in java?

I created an executable using cpp which accepts two integer values and then returns the sum. 我使用cpp创建了一个可执行文件,该文件接受两个整数值,然后返回总和。 I'm executing the EXE with Java using the following code: 我正在使用以下代码通过Java执行EXE:

try {
    pr = rt.exec("C:\\Users\\babesha.fm\\Desktop\\SUM.exe 10 5");
    int exitVal = pr.waitFor();

    System.out.println("Exited with error code " + exitVal);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I used the return statement while creating the exe. 我在创建exe时使用了return语句。 But the problem is when I executed this exe by using the above java code. 但是问题是当我通过使用上面的Java代码执行此exe时。 pr.waitFor(); pr.waitFor(); statement returns the sum value instead of successful exicution.Did I done anything wrong . 语句返回总和值而不是成功的exitution。我做错了什么。

you could try this: 您可以尝试以下方法:

Process p = Runtime.getRuntime().exec("xxx.EXE p1 p2");
// This is as same as what we do at DOS Prompt.
InputStream is = p.getInputStream();
int n = 0;
while(n != -1)
{
n = is.read();
System.out.print(n);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM