有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

cmd通过配置文件在Java中运行系统命令

如何通过相同的Java代码运行配置文件中的命令?我希望通过相同的Java代码运行命令行脚本;我可以在硬编码时运行它,但我想从配置文件运行这些命令,请告诉我怎么做

    try { 
        Runtime rt = Runtime.getRuntime(); 
        Process pr = rt.exec("cmd /c dir"); 
        Process pr = rt.exec("C:/bea/wlserver_10.3/server/bin/setWLSEnv.cmd && java weblogic.Admin -url server:port -username system -password passwd CLUSTERSTATE -clusterName cluster");
         BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line=null; 
        while((line=input.readLine()) != null) 
        { 
          System.out.println(line); 
          int exitVal = pr.waitFor(); 
          System.out.println("Exited with error code "+exitVal); 
         }
}finally{
}}}} catch(Exception e)

如何从xml配置文件中运行exec参数中的命令,我放在那里的命令将放在xml配置文件中,我希望从那里运行这些命令


共 (1) 个答案

  1. # 1 楼答案

    你的老朋友Runtime.getRuntime.exec()

    try {
        String str ="C:/somefile.txt";
        Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
        System.out.println(str);
        } catch (Exception ex) {}
    

    你也可以参考this tutorial