Results 1 to 8 of 8
  1. #1
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default Weird problem running a batch file

    Hello all

    Here's some code first off :

    Java Code:
    public static void stopDb()
        {
            try 
            {
                Process p1 = Runtime.getRuntime().exec(new File("").getCanonicalPath() + "//db//mysql_stop.bat");
                if (p1.waitFor() == 0)
                {
                    Logs.getDbLog().log(Level.INFO, "Database stopped successfully");
                    String name = new File("").getCanonicalPath() + "//db//cleanup.bat";
                    Process p2 = Runtime.getRuntime().exec(name);
                    if (p2.waitFor() == 0)
                    {
                        Logs.getDbLog().log(Level.INFO, "Successfully cleaned mapped drive and process");
                        System.exit(0);
                    }
                    else
                    {
                        throw new Exception("Unable to complete execution of database cleanup script");
                    }
                }
                else
                {
                    throw new Exception("Unable to complete execution of database stop script");
                }
            } 
            catch (IOException ex) 
            {
                Logs.getDbLog().log(Level.SEVERE, "Unable to stop database : " + ex.getMessage(), ex);
                System.exit(1);
            }
            catch (InterruptedException ex)
            {
                Logs.getDbLog().log(Level.SEVERE, "Unable to start database : " + ex.getMessage(), ex);
                System.exit(1);
            }
            catch (Exception ex)
            {
                Logs.getDbLog().log(Level.SEVERE, "Unable to start database : " + ex.getMessage(), ex);
                System.exit(1);
            }
        }
    As you can see, this method stops my Db server and runs a cleanup script that ensures the mapped drives are gone and the process stops. For this example's purpose here's the content of the cleanup.bat file :

    for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do subst %%a: /D
    taskkill /IM mysqld-opt.exe /F

    What it does is remove any virtual drive (i will change it later on to only remove the one that was mapped, although it only removes the one i need right now) and kills the mysql process. The issue is that it seems to never finish. When i run it manually it works fine and does exactly what i want it to do. The mysql_stop.bat file works fine, no exceptions are thrown, it will just not finish.

    Any ideas?

  2. #2
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    BTW i do know that my last 2 exceptions have the wrong message, i just corrected it :)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It could be that you're running out of buffer space by not handling your input and output streams. Please have a look at this great article for more on this and other trips and traps: When Runtime.exec() won't

  4. #4
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Well i tried to call only cleanup.bat and even that doesn't work. See as i'm only calling a batch file i don't feel the method described in the article applies as they are calling an exe file. Am i correct?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There's more to the article than that, and even if you're only running a bat file, you'll still need to handle the streams. I'm not sure if the bat file needs to be called via cmd.exe or not, but it would be worth testing the suggestions from the article -- all of them.

  6. #6
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    There's more to the article than that, and even if you're only running a bat file, you'll still need to handle the streams. I'm not sure if the bat file needs to be called via cmd.exe or not, but it would be worth testing the suggestions from the article -- all of them.
    Will do sir!

  7. #7
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Well i added "cmd /c start " + before my command parameter and it works fine now! Weird...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Help with .bat batch file problem
    By Java_Mad in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 02:52 PM
  2. Weird download file problem
    By bubbless in forum Advanced Java
    Replies: 12
    Last Post: 05-28-2010, 04:14 PM
  3. Weird path problem when reading properties file
    By jerry_popperq in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 04:32 PM
  4. Running batch files in java
    By pradeesh in forum New To Java
    Replies: 10
    Last Post: 12-04-2008, 10:26 PM
  5. Problem in batch file
    By marwa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 11:24 AM

Posting Permissions

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