Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default format USB stick, using Java and Batch file.

    hey all,

    basically what im trying to do is to format a USB memory stick by creating a batch file with the settings i want and executing it. however when i execute the batch file from java, the system hangs and the format doesn't work. if i stop the application, the format completes correctly, its driving me insane!

    Java Code:
            String outPath = "C:\\Users\\me\\Documents\\formatter\\format.bat";
    
            FileWriter fw = new FileWriter(outPath);
            BufferedWriter batchFile = new BufferedWriter(fw);
    
            batchFile.write("Format M: /FS:FAT32 /V:test /Q");
            batchFile.close();
    
                Process formatting = Runtime.getRuntime().exec(outPath);
                 
                formatting.waitFor();
    
    
    proceed with rest of code......................................
    thanks for any help.
    L.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: format USB stick, using Java and Batch file.

    What happens when you don´t write a batch file but execute that line directly? (format is an executable file itself if I'm not mistaken).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: format USB stick, using Java and Batch file.

    Batch files are not executable and don't run. Rather the OS's command processor runs them, and so you must call them with this in mind. Something like this might work:

    Java Code:
    Process formatting = Runtime.getRuntime().exec("cmd.exe", "/c", outPath);

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: format USB stick, using Java and Batch file.

    Quote Originally Posted by Fubarable View Post
    Batch files are not executable and don't run. Rather the OS's command processor runs them, and so you must call them with this in mind. Something like this might work: [ ... ]
    Is that needed? I thought that format is an executable file itself, so why fire up an entire command processor as well?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: format USB stick, using Java and Batch file.

    You're probably right.

  6. #6
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: format USB stick, using Java and Batch file.

    Quote Originally Posted by Fubarable View Post
    Batch files are not executable and don't run. Rather the OS's command processor runs them, and so you must call them with this in mind. Something like this might work:

    Java Code:
    Process formatting = Runtime.getRuntime().exec("cmd.exe", "/c", outPath);
    i tried what you suggested, but to no avail. is there something wrong with the code in the batch file? as if i click the bat file manually it just cycles through the format code endlessly until i close the CMD window,but it doesn't actually format the USB. if i run the bat file as administrator it works fine. but that doesn't make sense if its permissions, as if i run it from the java application, yes it hangs, but when i stop the process it has completes the format process! ITS DRIVING ME CRAZY!

    thanks again for any more suggestions.
    L.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: format USB stick, using Java and Batch file.

    Did you try my suggestion? (see above)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. start/compile java from .bat batch file
    By emosms in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 05:34 PM
  2. Help with .bat batch file problem
    By Java_Mad in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 02:52 PM
  3. Batch file to Run Java application
    By nitishlnt in forum Eclipse
    Replies: 1
    Last Post: 10-24-2008, 08:46 PM
  4. PNG file format decoder in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:51 PM
  5. eclipse batch file
    By doug99 in forum Eclipse
    Replies: 6
    Last Post: 04-24-2008, 03:59 PM

Posting Permissions

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