how to give runtime input from qt creator to external jar



  • I am developing qt widgets application to run external jar file and display the jar output on qt gui touch diaplay.
    In such a way my external jar file,executed while user given runtime input then process and print the output.(Both read and write)
    Here, i executed jar file using QProcess. Process (jar execution) started running, waiting for user input. I cannot give runtime input to my external jar file through both qt console and terminal window. please suggest how to write input and read output back from external jar file through qt.

    This is our jar (sample.jar) contained java application file (.java). Export this .java file as executable sample.jar file.

    import java.util.Scanner;

    public class RuntimeInput {

    public static void main(String args[]){
    	System.out.println("Reading jar file");
    	
    	System.out.println("Enter Consumer No: ");
    	
    	Scanner inputReader = new Scanner(System.in);
           
        String name = inputReader.nextLine();
        System.out.println("Hi " + name);
    ``` 
    

    }
    }

    
    

    Expected Qt output:

    Run executable jar in cmd window getting like this,

    D:>java -jar sample.jar

    Reading jar file
    Enter Consumer No:
    124569896
    Hi 124569896

    mainwindow.cpp 
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QTextStream>
    #include <QByteArray>
    #include <QProcess>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
    
            cmd = new QProcess(this);
    
            connect( cmd, SIGNAL(readyReadStandardError()), this, SLOT(handleReadStandardError()) );
    
            connect( cmd, SIGNAL(readyReadStandardOutput()), this, SLOT(handleReadStandardOutput()) );
    
            QStringList arguments;
            arguments << "-jar" << "D:\\sample.jar";
    
            cmd->start("java",  arguments, QIODevice::ReadWrite);
    		
            //cmd->execute("java -jar D:\\sample.jar");
    
            if ( cmd->state() == QProcess::NotRunning ) {
               qDebug() << "The process is not running.It exits";
            }
            else if ( cmd->state() == QProcess::Starting ) {
                qDebug() << "The process is started, but the program has not yet been invoked.";
            }else if ( cmd->state() == QProcess::Running ) {
                qDebug() << "The process is running and is ready for reading and writing.";
            }
    
            if (!cmd->waitForStarted())  {
               qDebug() << "Not yet started";
               
            }
    
    }
    
    void MainWindow::handleReadStandardError()
    {
        QByteArray data = cmd->readAllStandardError();
        qDebug() << data;
        ui->textEdit->append(QString(data));
    }
    
    void MainWindow::handleReadStandardOutput()
    {
       QByteArray data = cmd->readAllStandardOutput();
       qDebug() << "Data:\n" << data << endl;
       ui->textEdit->append(QString(data));
    
    }
    
    
    

    QT Console output:

    Starting D:\ANNIE\QT Workspace\build-Samplewrite-Desktop_Qt_5_7_1_MinGW_32bit-Debug\debug\Samplewrite.exe...
    The process is running and is ready for reading and writing.
    Data:

    "Reading jar file \r\n
    Enter Consumer No: \r\n"



  • QProcess inherits from OIODevice, so we can use the write() function to write data to it's standard input. Use the standard C++ way to read console input, and pass that to the write function.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.