4

I am working on a application which first require to check the available free disk space before running any operation. We have set some Default required Space limit like 512MB, So if any working drive does not have more then 512mb space my program will prompt for less memory space available, please make sufficient space to run the program.

I am using following code for it.

long freeSpace = FileSystemUtils.freeSpaceKb() * 1024; 

here I am coverting size into byte first to compare with our standard required size. Due to the above statement i am gettign following exception:

Error-Command line returned OS error code '3' for command [cmd.exe, /C, dir /-c "F:\MyApp\"]Stacktrace java.io.IOException: Command line returned OS error code '3' for command [cmd.exe, /C, dir /-c "F:\MyApp"]
at org.apache.commons.io.FileSystemUtils.performCommand(FileSystemUtils.java:506)
at org.apache.commons.io.FileSystemUtils.freeSpaceWindows(FileSystemUtils.java:303)
at org.apache.commons.io.FileSystemUtils.freeSpaceOS(FileSystemUtils.java:270)
at org.apache.commons.io.FileSystemUtils.freeSpaceKb(FileSystemUtils.java:206)
at org.apache.commons.io.FileSystemUtils.freeSpaceKb(FileSystemUtils.java:240)
at org.apache.commons.io.FileSystemUtils.freeSpaceKb(FileSystemUtils.java:222)...

The OS returned Error Code is '3' thats mean it is not normal termination.

So now how can I resolve this issue ?

I also found alternative method available in java 1.6 - How to find how much disk space is left using Java?

new File("c:\\").getFreeSpace();


---------------------------------
**More Details :** 
---------------------------------
OS Architecture : amd64
Temp Dir : c:\temp\
OS Name : Windows 7
OS Version : 6.1 amd64
Jre Version : 1.6.0_45-b06
User Home : C:\Users\Tej.Kiran
User Language : en
User Country: US
File Separator : \
Current Working Directory : F:\MyApp\
Community
  • 1
  • 1
Tej Kiran
  • 2,218
  • 5
  • 21
  • 42
  • What Windows version is this error occuring on? Are you using Java 6 or 7? Can you post the stacktrace for the SecurityException as well? – Philipp Reichart May 01 '13 at 09:40
  • Hi Philipp, Thanks for reply, I edited my question, the last new File().getFreeSpace() I did not use, I just found it as a alternative. I tried with java 6 as well as java 7 both – Tej Kiran May 01 '13 at 09:45
  • `new File("c:\\").getFreeSpace();` works well on my machine atleast – Jatin May 01 '13 at 09:49
  • Can any one let me know is it better to use ew File("c:\\").getFreeSpace(); instead of long freeSpace = FileSystemUtils.freeSpaceKb() * 1024; and why ? – Tej Kiran May 01 '13 at 10:05
  • Again, what windows version is this occuring on? `FileSystemUtils.freeSpaceKb()` works fine on Windows 8; error code 3 means "path not found". Are you running the program from a network drive or from a local drive? What is the output of adding `System.out.println(new File(".").getAbsolutePath());` in your program? – Philipp Reichart May 01 '13 at 10:40
  • What version of `commons-io` library do you use? What happened if you run command `dir /-c "F:\MyApp\"` manually from console? What happened if you point disk explicitly: `FileSystemUtils.freeSpaceKb("F:")`? – Maxim Kolesnikov May 01 '13 at 12:07
  • Hello Philipp - OS is Windows 7 and System.out.println(new File(".").getAbsolutePath()) printing "F:\MyApp\" --------------------------------- More Details : --------------------------------- OS Architecture : amd64 Temp Dir : c:\temp\ OS Name : Windows 7 OS Version : 6.1 amd64 Jre Version : 1.6.0_45-b06 User Home : C:\Users\Tej.Kiran User Language : en User Country: US File Separator : \ Current Working Directory : F:\MyApp\ – Tej Kiran May 02 '13 at 05:09

1 Answers1

0

You can try executing that command from a prompt. Run cmd.exe and enter the following:

cmd.exe /C dir /-c "F:\MyApp\"
echo %errorlevel%

Error code 3 means the path doesn't exist, but in this case I wonder if it is related to permissions. Any non-zero errorlevel is a problem. If your Java app needs to know the free space on the drive it is installed on, you can do something like this:

// returns something like "file:/C:/MyApp/my/pkg/MyClass.class"
// -OR- "jar:file:/C:/MyApp/myjar.jar!/my/pkg/MyClass.class"
String myPath = my.pkg.MyClass.class.getResource(MyClass.class).toString();
int start = myPath.indexOf("file:/") + 6;
FileSystemUtils.freeSpaceKb(myPath.substring(start, myPath.indexOf("/", start));

Obviously this code wouldn't work in an applet, but that shouldn't be surprising. The substring logic should also be more robust, but this is just a simple example.

ngreen
  • 1,559
  • 13
  • 22