0

这是 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();

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

4

2 回答 2

1

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

int a;
cin>>a;

反而。

于 2013-05-25T09:40:52.067 回答
1
writer.write(c);
writer.flush();

上面的语句写入stdin子进程但getch()似乎没有读取,stdin因为它没有获得输入。您应该使用cin >> c; orc = getc(stdin)代替。getch()甚至似乎对 on 有一些奇怪的影响,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 )。该值是return 0;C++ 程序中返回的值。

输出

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.只是控制台中的输入,而不是输出的一部分。

于 2013-05-25T10:45:25.097 回答