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

来源:互联网 发布:支持xen的linux内核 编辑:程序博客网 时间:2024/06/05 23:55
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");}}


0 0