1

I am trying to direct cmd output for tshark -i <interface> (wireshark utility) to JTextArea in a java program .

I tried an alternate way,like storing output from the prompt to a text file and then opening the file in the program with FileInputStream / BufferedInputStream. But this does not work as the output is real time(data is displayed according to transaction over interface) and I want the data to be displayed in the JTextArea on real time basis.

Since, the file cannot be read until it is completely written and closed the program gives me FileNotFoundException. The code fragment may give you a better insight to what I'm trying to do.

public void controlfunc(int n) {
/*
this is the complete function that takes the serial number of adapter as input,
then runs a command that starts tshark for listening on the adapter 
and saves output to prec file
*/  
String input = JOptionPane.showInputDialog(null, "Please enter the device 
serial number you want to start listening to ?", "Record manager Query",QUESTION_MESSAGE);
int id = Integer.parseInt(input);
    //send id value to a batch file for processing with tshark...
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("powershell.exe TSHARK -i " + id + " >> C:\\Users\\HP\\Documents\\response\\Server_Analyser\\src\\server_analyser\\sreverdump\\prec");
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Error in processing command !\nCheck Software Integrity !", "FATAL ERROR OCCURED !", ERROR_MESSAGE);
        e.printStackTrace();
}
// I need your help here. I cannot open this file and display data in real time. Any other approach is welcomed.
    //get output data from prec to text area output...
    try {
        int ch;
        FileInputStream fin = new FileInputStream("C:\\Users\\HP\\Documents\\response\\Server_Analyser\\src\\server_analyser\\sreverdump\\prec");
        BufferedInputStream bin = new BufferedInputStream(fin);
        while ((ch = bin.read()) != -1) {
            report.append("" + (char) ch);
        }
/*
There is a stop button that sends 0 as argument to the function and the 
code closes the file as no more data is to be read and kills the tshark 
process that was running.
*/ 
        if (n == 0) {
            bin.close();
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("powershell.exe killproc tshark.exe");
        }

    } catch (FileNotFoundException fe) {
        JOptionPane.showMessageDialog(null, "Source file is corrupted or lost !");
    } catch (IOException ex) {
        Logger.getLogger(recordman.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "The program has encountered an IOException !", "record manager Error ", ERROR_MESSAGE);
    }
}



I'm sorry if this is exaggerating. Also pardon me for my english.

Biswajit Roy
  • 508
  • 2
  • 7
  • 19
  • I think what you're looking for can be found here: https://stackoverflow.com/questions/29545611/executing-powershell-commands-in-java-program – Dom Jul 17 '17 at 16:19
  • 1
    Please help me on displaying data on real time basis. Like,the file cannot be closed before I display the output. I wish to display output like we see while executing **tshark -i ** command. The output doesn't stops until we issue a break command. I hope I cleared my point. Please help. – Biswajit Roy Jul 17 '17 at 16:33
  • 1
    There's no reason to use PowerShell. Run tshark.exe directly with its stdout set to a pipe, which your program reads. I can't readily help with the java code for that, but I'm sure it's possible. Also, since you want real-time output, ensure that tshark doesn't buffer data before writing to the pipe, else you won't see anything until it flushes its buffer. If it doesn't default to line buffering or no buffering, there may be a command-line option to enable it. – Eryk Sun Jul 17 '17 at 18:30
  • 1
    I forgot...sorry that was a slip ! I changed it now. – Biswajit Roy Jul 17 '17 at 18:38
  • @eryksun my program reads a file named prec. I think you mean piping the output to file prec means saving the output to file maybe or I don't get you. I got your words on not using buffer. – Biswajit Roy Jul 17 '17 at 18:46
  • Look [here](https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html). At a high level Java works with "streams". Certainly in this case it's implemented by setting the standard handles of the child process to pipe handles. – Eryk Sun Jul 17 '17 at 19:03

0 Answers0