Reputation: 301
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.
Upvotes: 0
Views: 244
Reputation: 14707
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.
Upvotes: 2