Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to execute batch in java

How to execute batch in java

Scheduled Pinned Locked Moved Java
helpjavadatabasesysadminwindows-admin
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cofi
    wrote on last edited by
    #1

    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(); }

    M 2 Replies Last reply
    0
    • C cofi

      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(); }

      M Offline
      M Offline
      Marc Firth
      wrote on last edited by
      #2

      if your bat script contains spaces or special characters you will need to enclose it in quotes... Dunno if that helps.

      Neonlight

      1 Reply Last reply
      0
      • C cofi

        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(); }

        M Offline
        M Offline
        Marc Firth
        wrote on last edited by
        #3

        also ensure your path to the bat is readable by javaw -> try putting it in "C:\yourbatscript.bat"

        Neonlight

        C 1 Reply Last reply
        0
        • M Marc Firth

          also ensure your path to the bat is readable by javaw -> try putting it in "C:\yourbatscript.bat"

          Neonlight

          C Offline
          C Offline
          cofi
          wrote on last edited by
          #4

          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...

          M 1 Reply Last reply
          0
          • C cofi

            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...

            M Offline
            M Offline
            Marc Firth
            wrote on last edited by
            #5

            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;
            }
            }

            Neonlight

            modified on Wednesday, April 22, 2009 10:13 AM

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups