0

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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    what is the outcome of your code? – Amir Schnell Oct 10 '19 at 07:29
  • 1
    The quoting is clearly wrong. You probably want something like `processBuilder.command("bash", "-c","nikto -h \"$1\"", "", valueURL);` -- notice how the first argument after `bash -c "script; more script"` is a placeholder (it is used to populate `$0`). – tripleee Oct 10 '19 at 07:29
  • But then `bash -c` is not doing anything useful here; probably simplify to `processBuilder.command("nikto", "-h", valueURL); ` – tripleee Oct 10 '19 at 07:30
  • outcome is start of command "nikto" with parametre -h and web url. – Игорь Курилов Oct 10 '19 at 08:06
  • If i use , for example, processBuilder.command("bash", "-c","nikto -h google.com"); it work fine. But I wont put string from jtextfield from my GUI in place where google.com – Игорь Курилов Oct 10 '19 at 08:10
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. Also break a `String arg` into `String[] args` to account for things like paths containing space characters. – Andrew Thompson Oct 23 '19 at 11:50

1 Answers1

0
  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.

lotor
  • 1,060
  • 7
  • 18