PDA

View Full Version : Sending string to java process



moiit
7th July 2011, 11:27
From Qt application, starts a java subprocess, and sends a string to it. No need any response from java process.
I want to use QProcess because i think it is more simple than QTcpSocket(not sure).
However, it seems the java subprocess cant recieve the string.
Consider the codes writed below:

Qt Code:

QProcess *java= new QProcess();
java->setWorkingDirectory("F:/");
java->start("java Test");
if(!java->waitForStarted())
return false;
java->closeWriteChannel();
QString str = "Test String";
//qDebug() << qPrintable(str);
std::cout<<qPrintable(str)<<std::endl;
//QTextStream out(stdout);
//out<<str;

Java Code:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str;
try {
str = reader.readLine(); }
catch (IOException exc) {
System.out.println("Can't read from stdin!");
return ; }
if(str != null)
System.out.println(str);

How can i get it work as expected? Any advice will be thankful!

ChrisW67
7th July 2011, 11:37
You don't send anything to the Java program, just to your program's std::cout stream. Try writing to the QProcess write channel before you close it at line 6.

mcosta
7th July 2011, 12:25
Have you tried with something like



QProcess *java= new QProcess();
...
..

QTextStream out (java); // QProcess is inherited from QIODevice
out << qPrintable (str);


?

moiit
7th July 2011, 12:47
You don't send anything to the Java program, just to your program's std::cout stream. Try writing to the QProcess write channel before you close it at line 6.

Thank you very much!
Just modified the code as below:

QProcess *java= new QProcess();
java->setWorkingDirectory("F:/");
java->start("java Test");
if(!java->waitForStarted())
return false;
QByteArray ba("Test String");
java->write(ba);
java->closeWriteChannel();
It works! Thanks again!



Have you tried with something like



QProcess *java= new QProcess();
...
..

QTextStream out (java); // QProcess is inherited from QIODevice
out << qPrintable (str);


?
Tried this but no luck, but thanks anyway!