javaxmlnetbeansopenvas

Sending XML via CMD, from Java


I am having trouble getting this code to execute correct. After a hunt about on the internet and trying various things, it still won't.

The code below should send a command to an OpenVAS server using "omp". This command works ok from the commandline on the same machine. I think it may have something to do with passing the XML.

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          

  try
  {
     Runtime rt = Runtime.getRuntime();

     String cmdString = "cmd /K omp -u admin -w admin --xml=\"<create_task><name>Nigni</name><comment>Deep scan on server 3</comment><config id='daba56c8-73ec-11df-a475-002264764cea'/><target id='2c57a13e-5174-4c88-83ec-13a1e18f1f75'/></create_task>\"";
     System.out.println(cmdString);
     Process pr = rt.exec(cmdString);
     // Process pr = rt.exec("c:\\helloworld.exe");

     BufferedReader input = new BufferedReader(new InputStreamReader(
           pr.getInputStream()));

     String line = null;

     while ((line = input.readLine()) != null)
     {
        System.out.println(line);
        jTextArea1.append(line + "\n\n");
     }

     int exitVal = pr.waitFor();
     System.out.println("Exited with error code " + exitVal);

  }
  catch (Exception e)
  {
     System.out.println(e.toString());
     e.printStackTrace();
  }  


}                                         

Any help would be greatly appreciated

Thanks


Solution

  • You must pass an string array to Runtime#exec():

    String[] cmdString = new String[]{"cmd","/K","omp -u admin -w admin ..."};
    Process pr = rt.exec(cmdString);
    

    if omp is an exe you can pass directly :

    String[] cmdString = new String[]{"omp","-u","admin","-w",...);
    Process pr = rt.exec(cmdString);