Forums Register Login

Unix/Linux path

+Pie Number of slices to send: Send
Hi All,

I know this is a trite question but I still can't find the answer. I'm doing a program that has to get the free disk space of a certain folder. The program has to be compatible with java 1.5 so I can't use getFreeSpace() method. So for this purpose I use:

It works fine but when the folder that has to be checked has intervals in the path ( e.g /mnt/test dir/) the command doesn't execute properly and when it tries to get the output from it the program breaks since nothing was output by the end.
I tried to parse the path of the folder by putting '\' before every interval but no luck either. It doesn't work with quotations. What is wrong with that exec() method?
Any suggestions or advises would be appreciated.

Thanks in advanse.
+Pie Number of slices to send: Send
The directory path needs to go in single quotes if directories include spaces i.e. for you
df -h '/mnt/test dir/'

So, the next challenge is to set your folderPath to include the single quotes but a bit of experimentation with escape characters should resolve that.
+Pie Number of slices to send: Send
Use the version of Runtime.exec() that accepts an array of Strings; put one word into each String -- i.e.,

exec(new String[] {"df", "-h", "/mnt/test dir/" });
+Pie Number of slices to send: Send
Thank you Ernest,

It worked fine. That was a great tip.
+Pie Number of slices to send: Send
Placing spaces in file/directory names is a horrible thing to do. Even Microsoft has finally wised up. I don't know about Apple.

The reason why it's horrible is because it confuses command-line parsers (and occasionally people, as well). On the original Macintosh, there was no command line to speak of, so they could get away from it, but most of the rest of the world is less GUI-oriented, so as a general rule, if you're given any choice in the matter, don't put spaces in paths.

You can escape spaces in Unix pathnames, using a backslash, but since the backslash is itself magic to Java, you have to double it up, as in "test\\ dir".
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1350 times.
Similar Threads
Executing java process as some other user
Working with Files
Convert OutputStream data to String
problem with Runtime.getRuntime().exec when running java in .bat
WebPage to Applet to WebPage to Java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:08:26.