Cant find filepaths when blank space in folder name?

T

Tomas

Hi,

I have an java application that can launch windows files with theese code
lines that a nice person here provided me with:

public static final void openFileAndExecuteAssociatedApplication(String fullPathAndFileName)
{
try
{
Process p = Runtime.getRuntime().exec("cmd /c start " + fullPathAndFileName);
p.waitFor();
}
catch (Throwable ex){ex.printStackTrace();}
}
But if there is blank spaces in folder names(i.e e:\my files\test.txt)) the
system cant find the file and gives this errormessage:

"Cannot find the file 'e:\my'...."
Any suggestions how to solve this issue?

Regards
/Tomas
 
C

Chris Smith

Tomas said:
I have an java application that can launch windows files with theese code
lines that a nice person here provided me with:

public static final void openFileAndExecuteAssociatedApplication(String fullPathAndFileName)
{
try
{
Process p = Runtime.getRuntime().exec("cmd /c start " + fullPathAndFileName);
p.waitFor();
}
catch (Throwable ex){ex.printStackTrace();}
}
But if there is blank spaces in folder names(i.e e:\my files\test.txt)) the
system cant find the file and gives this errormessage:

"Cannot find the file 'e:\my'...."
Any suggestions how to solve this issue?

Yep. The Runtime.exec(String) method provides some very primitive
command line parsing, as described in the specification of the method
from the API docs. That parsing always breaks a command line into
separate parameters at the spaces. If you want something more advanced,
use the Runtime.exec(String[]) variant of this method, passing each
command separately. Looks like this:

Process p = Runtime.getRuntime().exec(new String[] {
"cmd", "/c", "start", fullPathAndFileName });

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Steven Coco

Tomas said:
I have an java application that can launch windows files
[...]
But if there is blank spaces in folder names(i.e e:\my files\test.txt)) the
system cant find the file and gives this errormessage:

"Cannot find the file 'e:\my'...."
Any suggestions how to solve this issue?

Write all the application functionality you need on your computer in
Java :) --custom; stable... Fulfilled.

--

..Steven Coco.
.........................................................................
When you're not sure:
"Confess your heart" says the Lord, "and you'll be freed."
 
G

ggg

You can try to enclose fullPathAndFileName with " symbols :

Process p = Runtime.getRuntime().exec("cmd /c start \"" +
fullPathAndFileName + "\"");
 
C

Christian Kaufhold

ggg said:
You can try to enclose fullPathAndFileName with " symbols :

Process p = Runtime.getRuntime().exec("cmd /c start \"" +
fullPathAndFileName + "\"");

No, you can't. It will still split at the spaces.

See Chris' reply.

<snip bottom quote>


Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top