2

我无法让此代码正确执行。在互联网上搜索并尝试了各种方法后,它仍然不会。

下面的代码应该使用“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();
  }  


}                                         

任何帮助将不胜感激

谢谢

4

1 回答 1

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);
于 2014-02-20T10:55:44.777 回答