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:
public class Test {
public static void main(String[] args) {
if (args.length > 0)
System.out.println(args[0]);
else
System.out.println("no args given");
}
}
public class Test {
public static void main(String[] args) {
if (args.length > 0)
System.out.println(args[0]);
else
System.out.println("no args given");
}
}
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:
// runJava.h
#ifndef _RUN_J_H_
#define _RUN_J_H_
#include <QObject>
#include <QProcess>
#include <QDebug>
Q_OBJECT
public:
}
~RunJava(){
}
void runJava(){
connect(myProcess,
SIGNAL(error
(QProcess::ProcessError)),
this,
SLOT(checkError
(QProcess::ProcessError)));
connect(myProcess, SIGNAL(started()), this, SLOT(processStarted()));
connect(myProcess,
SIGNAL(finished
(int,
QProcess::ExitStatus)),
this,
SLOT(processExited
(int,
QProcess::ExitStatus)));
connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(processReadyRead()));
arg << "-jar";
arg << "D:/test_java/test.jar";
arg << "whatever";
myProcess->start(program, arg);
}
private slots:
void processStarted(){
qDebug() << "Hurray ! MyProcess started!";
}
void checkError
(QProcess::ProcessError err
){ qDebug() << "Process error: " << err;
}
void processExited
(int exitCode,
QProcess::ExitStatus status
){ qDebug() << "Process exited with code: " <<exitCode << ", status: " << status;
emit done();
}
void processReadyRead(){
if (QProcess * proc
= qobject_cast<QProcess
*>
(sender
())){ qDebug() << "process output: " << proc->readAllStandardOutput();
}
}
signals:
void done();
};
#endif
// runJava.h
#ifndef _RUN_J_H_
#define _RUN_J_H_
#include <QObject>
#include <QProcess>
#include <QDebug>
class RunJava : public QObject{
Q_OBJECT
public:
RunJava(QObject * parent = NULL) : QObject(parent){
}
~RunJava(){
}
void runJava(){
QProcess *myProcess = new QProcess(this);
connect(myProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(checkError(QProcess::ProcessError)));
connect(myProcess, SIGNAL(started()), this, SLOT(processStarted()));
connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processExited(int,QProcess::ExitStatus)));
connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(processReadyRead()));
const QString program = "java";
QStringList arg;
arg << "-jar";
arg << "D:/test_java/test.jar";
arg << "whatever";
myProcess->start(program, arg);
}
private slots:
void processStarted(){
qDebug() << "Hurray ! MyProcess started!";
}
void checkError(QProcess::ProcessError err){
qDebug() << "Process error: " << err;
}
void processExited(int exitCode, QProcess::ExitStatus status){
qDebug() << "Process exited with code: " <<exitCode << ", status: " << status;
emit done();
}
void processReadyRead(){
if (QProcess * proc = qobject_cast<QProcess*>(sender())){
qDebug() << "process output: " << proc->readAllStandardOutput();
}
}
signals:
void done();
};
#endif
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QCoreApplication>
#include "runJava.h"
int main(int argc, char ** argv){
RunJava jv;
QObject::connect(&jv,
SIGNAL(done
()),
&app,
SLOT(quit
()));
jv.runJava();
return app.exec();
}
#include <QCoreApplication>
#include "runJava.h"
int main(int argc, char ** argv){
QCoreApplication app(argc,argv);
RunJava jv;
QObject::connect(&jv, SIGNAL(done()), &app, SLOT(quit()));
jv.runJava();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
.pro:
######################################################################
# Automatically generated by qmake (2.01a) Wt 3. gru 15:45:58 2013
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
CONFIG += console
# Input
HEADERS += runJava.h
SOURCES += main.cpp
######################################################################
# Automatically generated by qmake (2.01a) Wt 3. gru 15:45:58 2013
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
CONFIG += console
# Input
HEADERS += runJava.h
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
Bookmarks