Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. how to give runtime input from qt creator to external jar
QtWS25 Last Chance

how to give runtime input from qt creator to external jar

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
2 Posts 1 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnnieAlbert
    wrote on 26 May 2017, 03:42 last edited by
    #1

    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"

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AnnieAlbert
      wrote on 8 Jun 2017, 05:34 last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved