This discussion is archived
3 Replies Latest reply: Nov 19, 2012 7:36 AM by rp0428 RSS

How to format an USB drive from Java code?

Ravi_Teja Newbie
Currently Being Moderated
I tried with the code "Process p =Runtime.getRuntime().exec(sFormatCmd);" but landing up with the following error:

java.io.IOException: Cannot run program "format": CreateProcess error=2, The system cannot find the file specified
     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
     at java.lang.Runtime.exec(Runtime.java:615)
     at java.lang.Runtime.exec(Runtime.java:448)
     at java.lang.Runtime.exec(Runtime.java:345)

Can somebody help me regarding the same?
  • 1. Re: How to format an USB drive from Java code?
    rp0428 Guru
    Currently Being Moderated
    >
    I tried with the code "Process p =Runtime.getRuntime().exec(sFormatCmd);" but landing up with the following error:

    java.io.IOException: Cannot run program "format": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at java.lang.Runtime.exec(Runtime.java:615)
    at java.lang.Runtime.exec(Runtime.java:448)
    at java.lang.Runtime.exec(Runtime.java:345)

    Can somebody help me regarding the same?
    >
    No, they can't. When all you say is the equivalent of "my car won't go; can anyone help me make my car go?" you are on your own.

    The error message is pretty clear: 'The system cannot find the file specified'. We have no idea what file that is since you did not post any code that might tell us. All you posted is 'sFormatCmd' and that could contain anything.

    You should use ProcessBuilder to do tasks like that. See my replies in these two threads for examples that show the basics of what to do.
    Re: How to display sqlplus result in java?
    Re: Not able to invoke a process for cmd using Runtime.getRuntime()
  • 2. Re: How to format an USB drive from Java code?
    Ravi_Teja Newbie
    Currently Being Moderated
    Sorry for missing out what "sFormatCmd" variable is in my earlier post. Below is full piece of code I am trying to execute:

    .......
    String sFormatCmd = "format E: /Q /fs:exFAT";
    System.out.println("Format Command is: "+sFormatCmd);
    Process p = Runtime.getRuntime().exec(sFormatCmd);
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = null;
    while ((line = in.readLine()) != null) { 
    System.out.println(line);
    }
    .......
  • 3. Re: How to format an USB drive from Java code?
    rp0428 Guru
    Currently Being Moderated
    The two links I provided show how to do that. You need to run CMD.exe and pass it your parameters.

Legend

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