使用get.Runtime.exec()运行Bin文件?

时间:2013-05-24 19:27:27

标签: java runtime.exec

这是hello.exe的C ++代码:

#include<iostream.h>
#include<conio.h>
int  main()
{
  cout<<"Hello world\n";
  getch();
  cout<<"I bypass error\n";
  return 0;
}

我需要从Java程序运行hello.exe。

我想学习Java以便调用子进程bin文件并将IO的控制执行发送到那些bin文件。

就像在这个hello.exe中一样,它将打印hello world,如果我能够输入值,那么我只能看到“我绕过错误”。

这是我的Java程序:

package procs;

import java.io.*;
import java.lang.*;
import java.util.*;

public class Procs
{
   public static void main(String[] args)
   {

      Scanner scan = new Scanner(System.in);

      try
      {
         int exitVal;
         char c = 'a';
         Process process = Runtime.getRuntime().exec(new String[]{"C:/hello.exe"});
         OutputStream stdin = process.getOutputStream();
         InputStream stderr = process.getErrorStream();
         InputStream stdout = process.getInputStream();

         BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
         //PrintStream writer = new PrintStream(new BufferedOutputStream(stdin));
         //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
         // i uses both printstream and Buffered writer

         System.out.println("lets start<<<<<<");
         do
         {
            c = (char)reader.read();
            System.out.print(c);
         }
         while (c != '\n');
         //writer.Print(c);  i uses  both print
         //writer.write(c);  and the writer
         writer.flush();
         do
         {
            c = (char)reader.read();
            System.out.print(c);
         }
         while (c != '\n');
         exitVal = process.waitFor();
         System.out.println("Exited with error code " + exitVal);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}

它在lets start<<<<<

之后挂起

它甚至没有显示你好世界

还帮助我完成这一部分:

exitVal=process.waitFor();

像这样,我们有一些暂停命令来暂停执行过程

2 个答案:

答案 0 :(得分:1)

在C ++中,getch();不起作用。使用

int a;
cin>>a;

代替。

答案 1 :(得分:1)

writer.write(c);
writer.flush();

上述语句写入子流程的stdin,但getch()似乎无法从stdin读取,因为它没有获得输入。您应该使用cin >> c;c = getc(stdin)代替。 getch()似乎对stdout产生了一些奇怪的影响,因为替换它会使刷新cout过时。

以下是一些有效的代码:

// C++

#include<iostream>

using namespace std;

int  main()
{
    cout<<"Hello world\n";
    char c;
    cin >> c;
    cout<<"input received "  << c << endl;
    cout<<"I bypass error\n";
    return 0;
}

// Java

BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

System.out.println("lets start<<<<<<");
do {
 c = (char) reader.read();
   System.out.print(c);
} while (c != '\n' && c != -1 && c != Character.MAX_VALUE);
System.out.print('\n');
System.out.print("(1) stdout of subprocess consumed.");

c = (char) System.in.read();
writer.write(c);
writer.flush();


do {
 c = (char) reader.read();
   System.out.print(c);
} while (c != '\n' && c != -1 && c != Character.MAX_VALUE);
System.out.print('\n');
System.out.print("(2) stdout of subprocess consumed.");

exitVal = process.waitFor();
System.out.println("Exited with error code " + exitVal);

本声明

exitVal=process.waitFor();

只是意味着当前线程(在你的情况下是主线程)必须等待子进程完成并捕获它返回的值(参见Process#waitFor()的javadoc)。该值是C ++程序中return 0;返回的值。

<强>输出

lets start<<<<<<
Hello world

(1) stdout of subprocess consumed.a
input received a

(2) stdout of subprocess consumed.Exited with error code 0

请注意,a之后的(1) ... consumed.只是控制台中的输入,它不是输出的一部分。