Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java. Starting Batch files

    Is it possible to start a batch file from java program?
    <JPM />

  • #2
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes it is.

    PHP Code:
    try {
          
    Runtime.getRuntime().exec(
              
    "cmd.exe /c start j:\\somefile.bat");
        }
        catch (
    Exception e) {
          
    e.printStackTrace();
        } 

    This should work for simple batch files, but there may be some classpath issues if your batch file starts looking for other files.

    Also, instead of the string I have there, you can also place the path to an exe and it will run, or any file for that matter. Windows will open it with whatever the default program is.

    This is just one method of doing it. There are others that are very similar. Later. Hope it works.

  • #3
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, it works great! Just on thing; what is the '/c' good for?
    <JPM />

  • #4
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To be totally honest, I'm not sure.

    cmd.exe is the command prompt. I believe the /c has something to do with opening the command window, although I could be incorrect. The start command runs the next token in the string. If you were to put a text file after it instead of a batch file, it should use your default editor to open the file. Hope this helped.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •