java调用exe并且获取返回值的例子


1 、 C代码

int main(char * args)

{

printf("%s",args[1]);

return 1;//为java中要获取的返回值

}

 2、Java代码

import java.io.*;
class Main {
 static Process p;
static public void openApplication(String filePath) throws InterruptedException{
try {
p=java.lang.Runtime.getRuntime().exec(filePath);
 
//while   ((line   =   inputBufferedReader.readLine())   !=   null)   {
//System.out.println(line);
System.out.print(p.toString());
BufferedInputStream br = new BufferedInputStream(p.getInputStream());
BufferedOutputStream br1 = new BufferedOutputStream(p.getOutputStream());
int ch;
StringBuffer text = new StringBuffer("获得的信息是: \n");

while ((ch = br.read()) != -1) {
 text.append((char) ch);
}
int   retval   =   p.waitFor();

 System.out.println(text+br1.toString());
 System.out.println(retval);

//}
 


} catch (IOException e) {
e.printStackTrace();
} finally{
 //System.out.print(p.exitValue());
}
}
public static void main(String args[]) throws InterruptedException {

 openApplication("c:\\NONAME.exe");
}
}


你可能感兴趣的:(Java学习笔记)