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.