简体   繁体   中英

Running system commands in Java through a config file

How do I run commands that are present in a config file through the same Java code? I want to run command line scripts through the same Java code; I could run it when I hardcode it but I want to run these commands from a config file, please let know how to do?

    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)

How do I run the commands in argument for exec from a xml config file,the commands that I ve put there are going to be in an xml config file, I want tun these commands from there.

Your old friend 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) {}

You can also refer this tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM