Bufferedwriter.write () and Bufferedreader.readline () (included with runtime exec in Java)

Source: Internet
Author: User
Tags strcmp

Yesterday in the implementation of a Java program started executing C + + program, encountered some problems, first prepare to record it (for the benefit of people)


The test function that is ready to be implemented is to start a C + + executable in a Java program, and then the Java program communicates with the C + + program (the Java side sends the message to the C + + program, and the C + + process Returns a result message to the Java program. Continue round-robin until the exit command ends).


First, starting a C + + executable in a Java program is using Java's Runtime.getruntime (). exec (), Process class exec () official description http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html

"In addition, there is a Java Native Interface framework that implements Java's approach to invoking other programming languages."

* Description *:

using the Exec () method, all standards for C + + programs io (i.e. stdin , stdout and the stderr ) operation will pass through three streams (Getoutputstream (),getinputstream () and geterrorstream ()) REDIRECT to Parent process .

Therefore, the use of the above redirection characteristics can be achieved through the standard input and output Java and C + + program between the simple communication, in the processing of communication I/O, the use of BufferedReader and BufferedWriter as a buffer for communication input and output, convenient for message processing.


About ReadLine (), here's a blog post click Open Link http://blog.csdn.net/swingline/article/details/5357581


* Note *:

1. Because the C + + side isread by rowMessage, so be aware when sending messages on the Java sidea newline character must be added to the end of each message. This is why I am in Writetocpp.write (wstr+ "\ n"); +" \ n " bug For a long time, have been unable to find any reason for the java console Span lang= "en-US" style= "Font-family:simsun" > After the input message is sent java The program ran away. Later found in eclipse console

2. The following Java code opens up a thread to process message printing from the C + + side. Very useful!

3. "Reference from click to open link"

    • mistakenly think that readline () returns NULL if no data is read (because the other Read method returns 1 if no data is read), whereas ReadLine () is actually a Blocking Functions , when there is no data to read, it will always be blocked in that , rather than return null, because ReadLine () block, SYSTEM.OUT.PRINTLN (message) This sentence is not executed at all, so at the receiving end there will be no output. To execute to SYSTEM.OUT.PRINTLN (message), one method is to turn off the stream after sending the data, so that ReadLine () ends the blocking state and can get the correct result, but it is obvious that the data stream cannot be closed one line at a time Another way is to put SYSTEM.OUT.PRINTLN (message) in the while loop body.

Another problem has not been solved :

Starting a C + + program scenario for Java, how do I configure the use of Eclipse and Visual Studio (2013) so that I can continue to debug in Visual Studio after I have traced the debug Java program from eclipse to a C + + program?

I checked it out, as if the Attach to process feature in Visual Studio can be used to implement the interlock (in VS, turn on Debug-Attach to process.) and then find the Java process that is started check OK " I chose Javaw.exe "), but I tried for a long time without success. (At the beginning, I was prompted to find the PDB file, and I went to the "Microsoft Symbol Servers" Debugging-symbols, options, tools--- However, some PDB files are not loaded ... Hit the breakpoint is also displayed with a yellow circle with an exclamation point "prompt information see below")

Also please have experienced heroes can help to provide under the eclipse and the VS tune solution, thank you!!

Here is the Java and C + + source code that implements the bilateral communication

Java-side code snippet:

Import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Java.util.scanner;public class JavaCallCpp { Static BufferedWriter writetocpp;static BufferedReader readfromcpp;public static void Main (string[] args) throws IOException {try {Process execute = runtime.getruntime (). EXEC ("D:/workspace/vs/stringstream/debug/stringstream");// int exitval = Execute.waitfor (); writetocpp = new BufferedWriter (New OutputStreamWriter (Execute.getoutputstream ())); Readfromcpp = new BufferedReader (New InputStreamReader (Execute.getinputstream ())); MyThread mythread=new MyThread (readfromcpp); Mythread.start ()/*write to C++*/writetocpp.write ("has entered the Scentence\n "); Writetocpp.write (" aaaaa\n "); Writetocpp.flush ();//writetocpp.write (" quit\n ");//writetocpp.flush () ;/*read from C++*/writetocpp.write ("abcd\n"); Writetocpp.flush (); Scanner writer = new Scanner (system.in);//while (true) {String wstr = Null;while (True){Wstr=writer.next (), if (Wstr.equals ("Exit")) {writer.close (); break;} System.out.println (WSTR); Writetocpp.write (wstr+ "\ n"); Writetocpp.flush ();} Writetocpp.flush (); System.out.println ("Out of the while."); *while ((ReadLine = Readfromcpp.readline ()) = null) {System.out.println (readLine);} *///}//writerstring.close (); execute.destroyforcibly ();//forced termination of Execute.getinputstream (). Close (); readfromcpp.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Class MyThread extends Thread{private bufferedreader bfr;public MyThread (BufferedReader bfr) {this.bfr=bfr;} @Overridepublic void Run () {//TODO auto-generated method Stubsuper.run (); String readLine = null; Boolean _b=true;try {while (_b) {readline=bfr.readline (); if (readline==null) {_b=false;} Else{system.out.println (ReadLine);}}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


C + + Side program fragment:

void Fun1 () {string cmd;string line, Word;cout << ' Enter a scentence, and exit with typing \ "Quit\" "<<endl;whil E (getline (CIN, line) && line!= "quit") {StringStream stream, cout << stream.str () << endl;while (Stream >> Word) {cout << word << Endl;}}  while (true) {cout << ' please input a command, and exit with typing \ "exit\": "<<endl;getline (std::cin, cmd); if (0==STRCMP (Cmd.c_str (), "exit")) {//STRCMP Returns the value of Str1-str2 cout << "cmd is exit, exit 0 \ n"; exit (0);} else if (strcmp (Cmd.c_str (), "continue")!=0) {cout << ' cmd is not \ ' continue\ ' to break\n '; continue;} Else{cout << "CMD is continue, break while (true) \ n"; break;}} cout << "OK, pass over while (true) \ n";}



/* The following are (negligible) for debug issues */

tips for debugging the Eclipse and Visual Studio Debugs :

' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\bin\javaw.exe '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\ntdll.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\kernel32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\KernelBase.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\advapi32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\msvcrt.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\sechost.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\rpcrt4.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\user32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\gdi32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_ 6595b64144ccf1df_6.0.10586.0_none_8c15ae12515e1c22\comctl32.dll '. Symbols loaded. ' Javaw.exe ' (WIN32): Loaded ' C:\Windows\System32\combase.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\bcryptprimitives.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\imm32.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\msvcr100.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\server\jvm.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\psapi.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\wsock32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\ws2_32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\version.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\winmm.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\winmmbase.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\WindowS\system32\cfgmgr32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\verify.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\java.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\jdwp.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\npt.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\zip.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\shell32.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\windows.storage.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\shlwapi.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\kernel.appcore.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\WINDOWS\SYSTEM32\SHCORE.Dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\powrprof.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\profapi.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Program files\java\jdk1.8.0_65\jre\bin\dt_socket.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\NapiNSP.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\pnrpnsp.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\nlaapi.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\mswsock.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\dnsapi.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\nsi.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\winrnr.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\wshbth.dll '. Symbols loaded. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\FWPuclnt. DLL '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\bcrypt.dll '. Cannot find or open the PDB file. ' Javaw.exe ' (Win32): Loaded ' C:\Windows\System32\rasadhlp.dll '. Cannot find or open the PDB file.




Bufferedwriter.write () and Bufferedreader.readline () (included with runtime exec in Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.