By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

Running External Commands With Spaces in Names

P: n/a
I need to be able to run external commands from within a Java program, on
Linux and Windows (and eventually OSX). Under most circumstances there is
no problem, but if the path I specify as part of the command has any spaces
in it, or an argument for the command is a path with a space in it, or if I
redirect the output to a file with a space in the filename, it doesn't work
on Linux. I tried putting the paths in both single and double quotations,
and also tried using a backslash to escape the space within the path.

Nothing seems to work and it seems almost impossible to run an external
command from Java if there is a space in the filename. (I'm using 1.4.2.)

Any help on this would be appreciated.

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:y6R6c.46569$SR1.79605@attbi_s04...
I need to be able to run external commands from within a Java program, on
Linux and Windows (and eventually OSX). Under most circumstances there is
no problem, but if the path I specify as part of the command has any spaces in it, or an argument for the command is a path with a space in it, or if I redirect the output to a file with a space in the filename, it doesn't work on Linux. I tried putting the paths in both single and double quotations,
and also tried using a backslash to escape the space within the path.

Nothing seems to work and it seems almost impossible to run an external
command from Java if there is a space in the filename. (I'm using 1.4.2.)

Any help on this would be appreciated.

Hal

In Windows, try the DOS long filename shortcut. "Program Files" becomes
"Progra~1". Spaces are eliminated, I believe, so "A Directory" would be
"ADirec~1". It numbers sequentially, so "A Directory" and "A Directory with
Pictures" would come out "ADirec~1" and "ADirec~2", respectively.
Jul 17 '05 #2

P: n/a
Hal Vaughan wrote:
I need to be able to run external commands from within a Java program, on
Linux and Windows (and eventually OSX). Under most circumstances there is
no problem, but if the path I specify as part of the command has any spaces
in it, or an argument for the command is a path with a space in it, or if I
redirect the output to a file with a space in the filename, it doesn't work
on Linux. I tried putting the paths in both single and double quotations,
and also tried using a backslash to escape the space within the path.

Nothing seems to work and it seems almost impossible to run an external
command from Java if there is a space in the filename. (I'm using 1.4.2.)

Any help on this would be appreciated.

Hal


Hal,

Without knowing exactly how you are executing the commands, it will be
difficult to help you. In general, I would suggest that you use the
version of Runtime.exec() that accepts a an array of Strings instead of
the one accepting a single String. Each element in the array becomes a
command parameter to the executed process so there is no need for
quoting, et cetera.

Second, how are you "redirecting output"? Java only supports this by
using the streams that result from the Process object returned by
Runtime.exec(). If you were using that technique, you would have
probably said that you cannot open a file spaces in the name. So it
seems that you are likely using a shell to redirect output. This will
complicate matters. You must be aware that it is the shell (MS-DOS,
bash, csh, etc.) that handles stream redirection using the > and <
characters.

Ray

Jul 17 '05 #3

P: n/a
Raymond DeCampo wrote:
Hal Vaughan wrote:
I need to be able to run external commands from within a Java program, on
Linux and Windows (and eventually OSX). Under most circumstances there
is no problem, but if the path I specify as part of the command has any
spaces in it, or an argument for the command is a path with a space in
it, or if I redirect the output to a file with a space in the filename,
it doesn't work
on Linux. I tried putting the paths in both single and double
quotations, and also tried using a backslash to escape the space within
the path.

Nothing seems to work and it seems almost impossible to run an external
command from Java if there is a space in the filename. (I'm using
1.4.2.)

Any help on this would be appreciated.

Hal
Hal,

Without knowing exactly how you are executing the commands, it will be
difficult to help you. In general, I would suggest that you use the
version of Runtime.exec() that accepts a an array of Strings instead of
the one accepting a single String. Each element in the array becomes a
command parameter to the executed process so there is no need for
quoting, et cetera.


I didn't even realize that another one existed. (One problem with being
self taught is that I always have to dig out what I need and often don't
see something "close by" or related that might help.) Using an array for
arguments might help me in some cases. Thanks!
Second, how are you "redirecting output"? Java only supports this by
using the streams that result from the Process object returned by
Runtime.exec(). If you were using that technique, you would have
probably said that you cannot open a file spaces in the name. So it
seems that you are likely using a shell to redirect output. This will
complicate matters. You must be aware that it is the shell (MS-DOS,
bash, csh, etc.) that handles stream redirection using the > and <
characters.
That explains it! So Runtime doesn't call the shell? That would make sense
and explain some of the problems I've had.

Thanks! Your post explains a lot and points me in a a few new directions.

Hal
Ray


Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.