이전에 이런 질문을 받았지만 때로 사람들이 겪었을 때 다른 대답이 도움이되지 않았 음을 알고 있습니다.Java 응용 프로그램 내에서 C 콘솔 응용 프로그램과 상호 작용하는 방법
C 애플리케이션을 시작하고 몇 가지 입력을 전달하여 메뉴를 탐색해야 마침내 필요한 것을 실행할 수 있습니다. 최종 결과 (결과)는 파일로 보내지 만 중간 출력 (콘솔에 인쇄 된 메뉴 및 하위 메뉴)은 디버깅을 위해 내 Eclipse 콘솔에 인쇄 된 것이 좋을 것입니다.
사용자 Vince posted on his question 및 이후 언급 한 내용을 기반으로 다음 코드를 작성했지만 나에게 도움이되지는 않습니다.
public final class InteractWithExternalApp {
private static PrintWriter printOut;
private static BufferedReader retrieveOutput;
private static Process p;
private EvaluationTests(){} // suppressing the class constructor
public static void Evaluate(String paramToApp) {
try
{
Runtime rt = Runtime.getRuntime() ;
p = rt.exec("C:\\Path\\To\\Desktop\\appName " + paramToApp);
InputStream in = p.getInputStream() ;
OutputStream out = p.getOutputStream();
retrieveOutput = new BufferedReader(new InputStreamReader(in));
printOut = new PrintWriter(out);
// print menu
if((line = retrieveOutput.readLine()) != null) {
System.out.println(line);
}
// send the input choice -> 0
printOut.println("0");
printOut.flush();
// print sub-menu
if((line = retrieveOutput.readLine()) != null) {
System.out.println(line);
}
// send the input choice
printOut.println("A string");
printOut.flush();
// print sub-menu
if((line = retrieveOutput.readLine()) != null) {
System.out.println(line);
}
/*
Repeat this a few more times for all sub-menu options until
the app finally executes what's needed
*/
}catch(Exception exc){
System.out.println("Err " + exc.getMessage());
}
return;
}
또한, 운동으로, 나는 here 주어진 예 다음, Windows 명령 프롬프트를 열고 그것에게 명령을 전송했습니다. cmd.exe가 잘 열렸지만, echo
명령을 전달하면 아무것도하지 못했습니다.
OutputStream stdin = p.getOutputStream();
InputStream stdout = p.getInputStream();
stdin.write(new String("echo test").getBytes());
stdin.flush();
아무도 손을 들어주지 마십시오. 내가 어디로 잘못 가고 있니?
. ProcessBuilder를 사용하십시오. 공백을보다 잘 처리합니다. – Jayan
@Jayan,'ProcessBuilder pb = new ProcessBuilder ("C : \\ Path \\ To \\ Desktop \\ appName", "paramToApp");와'pb.start()'와 같은 의미입니까? 공식적으로 질문에 답한 것이 아니기 때문에 그 문제가 유일한 문제인지 추측해야하며 나머지는 정상적으로 작동합니까? –
예. 그것은 시작일 것입니다. – Jayan