通过CMD从Java发送XML

时间:2014-02-20 10:05:58

标签: java xml netbeans openvas

我无法让此代码执行正确。在互联网上搜寻并尝试各种各样的事情后,它仍然不会。

下面的代码应该使用“omp”向OpenVAS服务器发送命令。此命令可以在同一台计算机上的命令行中正常工作。我认为它可能与传递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();
  }  


}                                         

非常感谢任何帮助

谢谢

1 个答案:

答案 0 :(得分:0)

您必须将字符串数组传递给Runtime #exec():

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

如果omp是一个exe,你可以直接传递:

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