This discussion is archived
8 Replies Latest reply: Dec 20, 2012 5:39 AM by TPD-Opitz-Consulting-com RSS

Problem with Runtime.getRuntime() !!! Need Help !!!

Ravi_Teja Newbie
Currently Being Moderated
I am using the following command for formatting the USB connected media (in the below case H: is the connected media). Format is happening properly but the problem is format is not happening until the java program execution is completed.
I used the following code in a simple java main class and this worked fine. But my application is vast and cant terminate/close it for completing the format task.
Any ideas???????

Process p = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","format H: /Q /fs:exFAT /v:Test"});
  • 1. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    What is the surounding code and when is this line executed?

    When do you expect the format to be executed?

    bye
    TPD
  • 2. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    baftos Expert
    Currently Being Moderated
    This is what happens if I manually execute your command:
    C:\temp>cmd.exe /c format E: /Q /fs:exFAT /v:KINGSTON
    Insert new disk for drive E:
    and press ENTER when ready...
    The type of the file system is EXFAT.
    QuickFormatting 953M
    Initializing the File Allocation Table (FAT)...
    Creating file system structures.
    Format complete.
         953.8 MB total disk space.
         953.7 MB are available.
    
           32,768 bytes in each allocation unit.
           30,518 allocation units available on disk.
    
               32 bits in each FAT entry.
    
    Volume Serial Number is 3E43-D109
    
    C:\temp>
    As you see, there is some output from the format command and some input expected from the user ('press ENTER whan ready').
    Did you implement the handling of stdout, stderr and stdin as decribed here: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html ?
  • 3. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    Ravi_Teja Newbie
    Currently Being Moderated
    I want the format to be executed and later the code behind should be executed.
  • 4. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    baftos Expert
    Currently Being Moderated
    The execution of format will not advance unless you java program, after Runtime.exec() will simulate reading the output of format and simulate te required ENTER for format to proceed. This procedure is not exactly easy, but fortunately the article explains it well.
    In pseudo code, instead of:
    Runtime.exec()
    CodeBehind()
    you should:
    Runtime.exec()
    ExtremelyImportantCodeFromTheArticleThatAllowsFormatToCompleteRightAway()
    CodeBehind()
    Actually I have no idea why after all the format is successful when the program exits.

    Edit: If the java stuff looks too heavy, try some command line magic like in:
    Process p = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", 
         "echo.", "|",                              // Queue an ENTER as input to the 'format' command that follows so that java does not have to do it
         "format", "E:", "/Q", "/fs:exFAT", "/v:KINGSTON",      // The format command...
         "1>", "nul", "2>", "nul"});                    // ...with output suppressed so java does not need to deal with it
    p.waitFor();
    All I can say is that the above works fine on my machine and I suspect it should work on any Windows because it does not seem to use any new shell stuff.
  • 5. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    Ravi_Teja Newbie
    Currently Being Moderated
    Awesome !! this works fine.
    Thanks a lot for the info.
  • 6. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    sabre150 Expert
    Currently Being Moderated
    baftos wrote:
    Process p = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", 
         "echo.", "|",                              // Queue an ENTER as input to the 'format' command that follows so that java does not have to do it
         "format", "E:", "/Q", "/fs:exFAT", "/v:KINGSTON",      // The format command...
         "1>", "nul", "2>", "nul"});                    // ...with output suppressed so java does not need to deal with it
    p.waitFor();
    All I can say is that the above works fine on my machine and I suspect it should work on any Windows because it does not seem to use any new shell stuff.
    When the formatting fails, and one day it will fail when something is not quite ready, you will know nothing about it since the code does nothing with 'stdout', 'stderr' or the return code from the waitFor(). For Runtime.exec() and ProcessBuilder this is equivalent to swallowing exceptions. Since you pass all 'stdout' and 'stderr' to the null device even handling the Process 'stdout' and 'stderr' will not help.
  • 7. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    baftos Expert
    Currently Being Moderated
    The return code from waitFor()?
  • 8. Re: Problem with Runtime.getRuntime() !!! Need Help !!!
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    baftos wrote:
    The return code from waitFor()?
    Yes, <tt>waitFor()</tt> returnes the exit code of the system command executed.

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points