Results 1 to 12 of 12

Thread: [SOLVED] QProcess Java Application Not Working

  1. #1
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Arrow [SOLVED] QProcess Java Application Not Working

    Good Day,

    I'm having a problem calling a java function from Qt

    This is what I have in Qt
    Qt Code:
    1. QString program;
    2. program = "./java";
    3.  
    4. arg << "-jar";
    5. arg << "D:/Visual.jar";
    6. arg << "UniqueFilter "; //name of function within Visual.jar
    7. arg << "D:/inputImage.bmp"; //input image
    8. arg << "D:/result.txt"; //output file
    9. arg << "D:/resultImage.png"; //output image
    10.  
    11. QProcess *myProcess = new QProcess(this);
    12. myProcess->start(program, arg);
    To copy to clipboard, switch view to plain text mode 

    This is the actual function call from Command prompt (cmd) works
    Qt Code:
    1. //in cmd
    2. java -jar D:/Visual.jar UniqueFilter D:/inputImage.bmp D:/result.txt D:/resultImage.png
    To copy to clipboard, switch view to plain text mode 

    So Basically I'm using Java as my program name
    and all other inputs as arguments.
    It does not work

    What is the easiest way of calling java from Qt
    This java call is just needed for its resulting Image and Qt will process the rest

    Your help is much appreciated
    Kind Regards
    Last edited by 2lights; 3rd December 2013 at 15:38.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess Java Application Not Working

    Qt Code:
    1. program = "./java";
    To copy to clipboard, switch view to plain text mode 
    So it looks like your command is equivalent to
    Qt Code:
    1. ./java -jar D:/Visual.jar UniqueFilter D:/inputImage.bmp D:/result.txt D:/resultImage.png
    To copy to clipboard, switch view to plain text mode 
    Maybe this way :
    Qt Code:
    1. program = "java";
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QProcess Java Application Not Working

    No, it does not work
    Any other ideas ?
    Last edited by 2lights; 3rd December 2013 at 11:01.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess Java Application Not Working

    Connect to "error", "started" and "finished" signals of QProcess and call waitForStarted() on it. If you want to wait for this command to complete before doing anything else in your app, call waitForFinished().
    How do you check for errors now ?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess Java Application Not Working

    Did you start your program from the same cmd shell that you can run java on?
    If not, have you checked that your program sees the right environment? E.g. correct value of PATH?

    Cheers,
    _

  6. #6
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QProcess Java Application Not Working

    Qt Code:
    1. QProcess *myProcess = new QProcess(this);
    2. myProcess->start(program, arg);
    3.  
    4. qDebug() << myProcess->state(); //Ref 1
    5. qDebug() << myProcess->error(); //Ref 2
    To copy to clipboard, switch view to plain text mode 

    Within the enum QProcess::ProcessState (Ref 1) i get value 0, which by Qt doc it represents
    "The process is not running."
    and in the value for error (Ref 2) I get value 0, which by Qt doc it represents
    "The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program."
    At the moment I just check manually in specified dir to see if Output image has been created!

    Should I maybe invoke Command prompt(cmd) from Qt & then send the arguments to cmd

    I can call the java function from the cmd ("Windows Command Processor")
    I assumed that adding the same arguments to the QProcess will provide the same results.

    correct value of PATH?, What exactly should i check for or add?


    Added after 5 minutes:


    Tested the Java call from "Windows Command Processor" -> cmd it works

    I assumed calling it from Qt would be the same process
    What exactly do i look for within the PATH or What should I add?

    Should the Java and all the parameters be in the same diretory as the Qt project

    Or maybe should I call "cmd" from Qt with the predetermined arguments, not sure if it can be done though
    Last edited by 2lights; 3rd December 2013 at 11:48.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess Java Application Not Working

    QProcess will start your command asynchronously, you'll have to wait before checking if its running or any error occured. From Qt docs:
    Processes are started asynchronously, which means the started() and error() signals may be delayed. Call waitForStarted() to make sure the process has started (or has failed to start) and those signals have been emitted.

  8. #8
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QProcess Java Application Not Working

    Tried multiple methods but to no avail

    Are there any #includes I should add to my .cpp file to call function
    My function just initializes the the QProcess as above and tries to run it

    Do I need to create a Java virtual machine before I try calling QProcess

    Is there any simple tutorial I can follow?

    Kind Regards

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess Java Application Not Working

    My function just initializes the the QProcess as above and tries to run it
    And it does not wait for it to start and does not check for error values.
    Qt Code:
    1. {
    2. // ...
    3. QProcess *myProcess = new QProcess(this);
    4. connect(myProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(checkError(QProcess::ProcessError)));
    5. connect(myProcess, SIGNAL(started()), this, SLOT(processStarted()));
    6. connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processExited(int,QProcess::ExitStatus)));
    7. myProcess->start(program, arg);
    8. // .. here you exit the current function and wait for process to start / fail to start
    9. }
    10.  
    11. void MyClass::processStarted(){
    12. qDebug() << "Hurray ! MyProcess started!";
    13. }
    14.  
    15. void MyClass::checkError(QProcess::ProcessError err){
    16. qDebug() << "Process error: " << err;
    17. }
    18.  
    19. void MyClass::processExited(int exitCode, QProcessExitStatus status){
    20. qDebug() << "Process exited with code: " <<exitCode << ", status: " << status;
    21. }
    To copy to clipboard, switch view to plain text mode 

    Alternative is to call waitForFinished() after start(), but this can freeze the gui.
    It is important to understand the asynchronous nature of QProcess, this:
    Qt Code:
    1. QProcess *myProcess = new QProcess(this);
    2. myProcess->start(program, arg);
    3.  
    4. qDebug() << myProcess->state(); //Ref 1
    5. qDebug() << myProcess->error(); //Ref 2
    To copy to clipboard, switch view to plain text mode 
    will not work correctly in most of the cases, because you check the status and error value of QProcess before it is initialized and started.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess Java Application Not Working

    Quote Originally Posted by 2lights View Post
    Tested the Java call from "Windows Command Processor" -> cmd it works

    I assumed calling it from Qt would be the same process
    Sure, but the cmd shell might see a different environment than your program. Have you tried running your program from cmd?

    Quote Originally Posted by 2lights View Post
    What exactly do i look for within the PATH or What should I add?
    The path to the "java" executable, otherwise it has no way of finding it, right?

    Alternatively call java with it's full path. Not sure though how java finds it's own classpath.

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    2lights (3rd December 2013)

  12. #11
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Smile Re: QProcess Java Application Not Working

    Created a .bat file with all arguments then I called cmd
    Its not an efficient method but it seems to work

    Qt Code:
    1. p.start("cmd.exe", QStringList() << "/c" << "c:\\mybat.bat");
    2. if (p.waitForStarted())
    3. {
    4. p.waitForFinished();
    5. qDebug() << p.readAllStandardOutput();
    6. }
    7. else
    8. qDebug() << "Failed to start";
    To copy to clipboard, switch view to plain text mode 

    Taken from http://www.qtforum.org/article/21366...-using-qt.html

    Your comments on a better solution

    Kind Regards
    Last edited by 2lights; 3rd December 2013 at 15:35.

  13. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess Java Application Not Working

    Thanks guys tried what you guys posted
    Ok, so what is the output of the qDebug() in slots connected to error, exitCode etc. signals ?

    I just tested calling a class from java .jar file with QProcess, worked without problems. Here is the java class:
    Qt Code:
    1. public class Test {
    2. public static void main(String[] args) {
    3. if (args.length > 0)
    4. System.out.println(args[0]);
    5. else
    6. System.out.println("no args given");
    7. }
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    So it will print first argument or "no args given" in case of 0 args.
    I created a .jar file (with eclipse "export" option) and placed it in D:/test_java/test.jar.
    Next, the Qt project:
    Qt Code:
    1. // runJava.h
    2. #ifndef _RUN_J_H_
    3. #define _RUN_J_H_
    4.  
    5. #include <QObject>
    6. #include <QProcess>
    7. #include <QDebug>
    8.  
    9. class RunJava : public QObject{
    10. Q_OBJECT
    11. public:
    12. RunJava(QObject * parent = NULL) : QObject(parent){
    13. }
    14. ~RunJava(){
    15. }
    16. void runJava(){
    17. QProcess *myProcess = new QProcess(this);
    18. connect(myProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(checkError(QProcess::ProcessError)));
    19. connect(myProcess, SIGNAL(started()), this, SLOT(processStarted()));
    20. connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processExited(int,QProcess::ExitStatus)));
    21. connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(processReadyRead()));
    22. const QString program = "java";
    23. arg << "-jar";
    24. arg << "D:/test_java/test.jar";
    25. arg << "whatever";
    26. myProcess->start(program, arg);
    27. }
    28. private slots:
    29. void processStarted(){
    30. qDebug() << "Hurray ! MyProcess started!";
    31. }
    32.  
    33. void checkError(QProcess::ProcessError err){
    34. qDebug() << "Process error: " << err;
    35. }
    36.  
    37. void processExited(int exitCode, QProcess::ExitStatus status){
    38. qDebug() << "Process exited with code: " <<exitCode << ", status: " << status;
    39. emit done();
    40. }
    41. void processReadyRead(){
    42. if (QProcess * proc = qobject_cast<QProcess*>(sender())){
    43. qDebug() << "process output: " << proc->readAllStandardOutput();
    44. }
    45. }
    46. signals:
    47. void done();
    48. };
    49.  
    50. #endif
    To copy to clipboard, switch view to plain text mode 
    main.cpp:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include "runJava.h"
    3.  
    4. int main(int argc, char ** argv){
    5. QCoreApplication app(argc,argv);
    6. RunJava jv;
    7. QObject::connect(&jv, SIGNAL(done()), &app, SLOT(quit()));
    8. jv.runJava();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    .pro:
    Qt Code:
    1. ######################################################################
    2. # Automatically generated by qmake (2.01a) Wt 3. gru 15:45:58 2013
    3. ######################################################################
    4.  
    5. TEMPLATE = app
    6. TARGET =
    7. DEPENDPATH += .
    8. INCLUDEPATH += .
    9.  
    10. CONFIG += console
    11.  
    12. # Input
    13. HEADERS += runJava.h
    14. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

    After running the app, it outputs:
    Hurray ! MyProcess started!
    process output: "whatever
    "
    Process exited with code: 0 , status: 0

Similar Threads

  1. QProcess not working correctly
    By m_bishop in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2013, 17:36
  2. Replies: 3
    Last Post: 29th October 2011, 00:24
  3. Qt+QWebKit+Java+Flash not working
    By progDes in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2009, 17:49
  4. Qprocess not working
    By bruccutler in forum Newbie
    Replies: 3
    Last Post: 15th February 2007, 05:48
  5. QProcess not working in some cases
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2006, 10:58

Tags for this Thread

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.