Results 1 to 4 of 4

Thread: Sending string to java process

  1. #1
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Sending string to java process

    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:
    Qt Code:
    1. QProcess *java= new QProcess();
    2. java->setWorkingDirectory("F:/");
    3. java->start("java Test");
    4. if(!java->waitForStarted())
    5. return false;
    6. java->closeWriteChannel();
    7. QString str = "Test String";
    8. //qDebug() << qPrintable(str);
    9. std::cout<<qPrintable(str)<<std::endl;
    10. //QTextStream out(stdout);
    11. //out<<str;
    To copy to clipboard, switch view to plain text mode 

    Java Code:
    Qt Code:
    1. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    2. String str;
    3. try {
    4. str = reader.readLine(); }
    5. catch (IOException exc) {
    6. System.out.println("Can't read from stdin!");
    7. return ; }
    8. if(str != null)
    9. System.out.println(str);
    To copy to clipboard, switch view to plain text mode 

    How can i get it work as expected? Any advice will be thankful!
    Last edited by moiit; 7th July 2011 at 16:33.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sending string to java process

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending string to java process

    Have you tried with something like

    Qt Code:
    1. QProcess *java= new QProcess();
    2. ...
    3. ..
    4.  
    5. QTextStream out (java); // QProcess is inherited from QIODevice
    6. out << qPrintable (str);
    To copy to clipboard, switch view to plain text mode 

    ?
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending string to java process

    Quote Originally Posted by ChrisW67 View Post
    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:
    Qt Code:
    1. QProcess *java= new QProcess();
    2. java->setWorkingDirectory("F:/");
    3. java->start("java Test");
    4. if(!java->waitForStarted())
    5. return false;
    6. QByteArray ba("Test String");
    7. java->write(ba);
    8. java->closeWriteChannel();
    To copy to clipboard, switch view to plain text mode 
    It works! Thanks again!


    Quote Originally Posted by mcosta View Post
    Have you tried with something like

    Qt Code:
    1. QProcess *java= new QProcess();
    2. ...
    3. ..
    4.  
    5. QTextStream out (java); // QProcess is inherited from QIODevice
    6. out << qPrintable (str);
    To copy to clipboard, switch view to plain text mode 

    ?
    Tried this but no luck, but thanks anyway!
    Last edited by moiit; 7th July 2011 at 12:53.

Similar Threads

  1. [java] string.replace
    By mickey in forum General Programming
    Replies: 3
    Last Post: 7th September 2010, 08:04
  2. Sending signal to other process?
    By Boron in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2009, 10:06
  3. [java] manipulate a string
    By mickey in forum General Programming
    Replies: 5
    Last Post: 10th July 2008, 21:22
  4. Sending string from QT client to Java server
    By seanmu13 in forum Qt Programming
    Replies: 10
    Last Post: 3rd July 2007, 13:52
  5. Sending string from QT client to Java server
    By seanmu13 in forum Newbie
    Replies: 3
    Last Post: 3rd July 2007, 13:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.