简体   繁体   中英

How run .sh script with user input from textfield in Java?

I try start.sh script with user input from txtfield. Idea: user write parametre in txt fild. When push the button and script start with this parametre.

My code:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
String valueURL;
valueURL = URLtxt.getText().toString();              
ProcessBuilder processBuilder = new ProcessBuilder();
// -- Linux --
// Run a shell command
processBuilder.command("bash", "-c","nikto -h", valueURL,");     

try {

Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");         
}
int exitVal = process.waitFor();
if (exitVal == 0)                                            
 {
    this.hide();
    ScanWS sws = new ScanWS() ;
    sws.setVisible(true);
  1. An event istener is triggered in UI thread, so the processing must be fast . Your listener implementation waits for an external process , it can freeze your ui. All the process triggering and caring logic must start in a separate Thread.

  2. A process is quite an old construction and it has input/output issue. A process writes something during it's work into a buffer of it's output stream and when the buffer is all filled - the process hungs , waiting for the output stream to make some space. So, both process.input and process.output processors also must work in separate threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM