How to execute batch in java
-
hi i have written this code to execute a batch file in windows 2003 server R2 but it does not execute anything. it opens the cmd.exe window and does nothing also the "Exit Value" and "Return Code" is 0. can anyone help me about this? is there any windows registry setting to restrict the execution of batch files programatically? Thank you!!!! try { Runtime runtime = Runtime.getRuntime(); Process process = null; try { System.out.println( "Running: " + fName + ".bat " ); process = runtime.exec( "cmd /c start " + fName + ".bat" ); int returnCode = process.waitFor(); System.out.println("Exit value " + process.exitValue()); System.out.println("Return code value " + returnCode); System.out.println("Finished running the SQL Loader script's process. " + " Exit Value = " + ( returnCode == 0? "Success" : "Failure" ) ); } catch ( Exception e ) { System.out.println(e.getMessage()); e.printStackTrace(); } } catch ( Exception e ) { System.out.println( "Error running the sqlldr script: " + e ); e.printStackTrace(); }
-
hi i have written this code to execute a batch file in windows 2003 server R2 but it does not execute anything. it opens the cmd.exe window and does nothing also the "Exit Value" and "Return Code" is 0. can anyone help me about this? is there any windows registry setting to restrict the execution of batch files programatically? Thank you!!!! try { Runtime runtime = Runtime.getRuntime(); Process process = null; try { System.out.println( "Running: " + fName + ".bat " ); process = runtime.exec( "cmd /c start " + fName + ".bat" ); int returnCode = process.waitFor(); System.out.println("Exit value " + process.exitValue()); System.out.println("Return code value " + returnCode); System.out.println("Finished running the SQL Loader script's process. " + " Exit Value = " + ( returnCode == 0? "Success" : "Failure" ) ); } catch ( Exception e ) { System.out.println(e.getMessage()); e.printStackTrace(); } } catch ( Exception e ) { System.out.println( "Error running the sqlldr script: " + e ); e.printStackTrace(); }
if your bat script contains spaces or special characters you will need to enclose it in quotes... Dunno if that helps.
-
hi i have written this code to execute a batch file in windows 2003 server R2 but it does not execute anything. it opens the cmd.exe window and does nothing also the "Exit Value" and "Return Code" is 0. can anyone help me about this? is there any windows registry setting to restrict the execution of batch files programatically? Thank you!!!! try { Runtime runtime = Runtime.getRuntime(); Process process = null; try { System.out.println( "Running: " + fName + ".bat " ); process = runtime.exec( "cmd /c start " + fName + ".bat" ); int returnCode = process.waitFor(); System.out.println("Exit value " + process.exitValue()); System.out.println("Return code value " + returnCode); System.out.println("Finished running the SQL Loader script's process. " + " Exit Value = " + ( returnCode == 0? "Success" : "Failure" ) ); } catch ( Exception e ) { System.out.println(e.getMessage()); e.printStackTrace(); } } catch ( Exception e ) { System.out.println( "Error running the sqlldr script: " + e ); e.printStackTrace(); }
also ensure your path to the bat is readable by javaw -> try putting it in "C:\yourbatscript.bat"
-
also ensure your path to the bat is readable by javaw -> try putting it in "C:\yourbatscript.bat"
-
i am getting the same results. the window title of cmd.exe is the exact path of the bat file but i didn't execute. maybe there is some kind of security setting for java not to invoke the batch files or a setting in windows server...
No i did this last week. Didn't set anything else. webtools.java:
private void jButton13ActionActionPerformed(ActionEvent event) {
cmdExec cmd = new cmdExec();
String s = (cmd.run("ping www.google.co.uk"));
jTextPane0.setText(jTextPane0.getText() + s + "\n-----------------------------\n");
}cmdExec.java:
package webtools;
import java.io.BufferedReader;
import java.io.InputStreamReader;public class cmdExec {
public String run(String cmdLine) {
String line;
String output = "";
try {
Process p = Runtime.getRuntime().exec(cmdLine);
BufferedReader input = new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
output += (line + '\n');
}
input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
return output;
}
}modified on Wednesday, April 22, 2009 10:13 AM