Windows pathname hell

I

Ike

I am trying to invoke a windows command whereby a path\filename is passed to
the sustem call. However, the path and filename contain whitespace, and do
not conform to the 8.3 spec that the windows system call is looking for
(please see my 4 lines of code below). Is there any kind of workaround
anyone is aware of for this? Thank you, Ike

String fullPathAndFilename = new String("C:\Documents and Settings\Ike
Eisenhower\Birds Storytime.doc");
String commander = new String("cmd /c start "+fullPathAndFilename);
Process process = Runtime.getRuntime().exec(commander);
process.waitFor();
 
T

Thomas Kellerer

Ike said:
I am trying to invoke a windows command whereby a path\filename is passed to
the sustem call. However, the path and filename contain whitespace, and do
not conform to the 8.3 spec that the windows system call is looking for
(please see my 4 lines of code below). Is there any kind of workaround
anyone is aware of for this? Thank you, Ike

String fullPathAndFilename = new String("C:\Documents and Settings\Ike
Eisenhower\Birds Storytime.doc");
String commander = new String("cmd /c start "+fullPathAndFilename);
Process process = Runtime.getRuntime().exec(commander);
process.waitFor();
Well, pathnames with spaces need to be enclosed in quotes. Did you try:

String commander = new String("cmd /c start \""+fullPathAndFilename + "\"");
 
A

Andrew Thompson

Ike said:
I am trying to invoke a windows command whereby a path\filename is
passed to the sustem call. However, the path and filename contain
whitespace,

AFAIU, only an URI will encode filenames
with whitespace correctly. Also '!', "/" etc...
 
F

Flip

I thought I would have some fun with this one. :> Ya, I need another
coffee. :> Comments enclosed. I used notepad.exe and tried to open a file
in a directory with known spaces in it. There are a few options to pick
from. :> Good luck.


//--------------------------------------------------------------------------
----------------------
package playground;

import java.io.File;
import java.io.*;

public class RunCommandTest {
public RunCommandTest() {

try{
//this is one way to put in the dir separator, use the
File.separatorChar constant
// String fullPathAndFilename = new String( "notepad.exe C:" +
File.separatorChar + "Documents and Settings" +
// File.separatorChar +
"phenry" + File.separatorChar +
// "ntuser.ini" );

//compiler won't like those backslashes, control chars :<
//String fullPathAndFilename = new String( "notepad.exe
C:\Documents and Settings\phenry\ntuser.ini" );

//this works for me as well
//String fullPathAndFilename = new String( "notepad.exe
C:/Documents and Settings/phenry/ntuser.ini" );

//put in escape char works too
//String fullPathAndFilename = new String( "notepad.exe
C:\\Documents and Settings\\phenry\\ntuser.ini" );

//trying to put in escape char with single quotes around the path
with spaces in it
//String fullPathAndFilename = new String( "notepad.exe
'C:\\Documents and Settings\\phenry\\ntuser.ini'" );

//trying to put in escape char with escaped double quotes around
the path with spaces in it
//String fullPathAndFilename = new String( "notepad.exe
\"C:\\Documents and Settings\\phenry\\ntuser.ini\"" );


//another thing to try is to use a File object?
File fullPathAndFilename = new File( "C:\\Documents and
Settings\\phenry\\ntuser.ini" );

System.err.println( "fullPathAndFilename=" + fullPathAndFilename );

//use this one when it's a String var up top
//String commander = new String( "cmd /c start " +
fullPathAndFilename.toString() );

//use this one when it's a File var up top
//this one works, yahoo
String commander = new String( "cmd /c start notepad.exe " +
fullPathAndFilename.toString() );

Process process = Runtime.getRuntime().exec( commander );
process.waitFor();
} catch( Exception ex ){
ex.printStackTrace();
}
}

public static void main(String[] args){
System.err.println("beg");
RunCommandTest run = new RunCommandTest();
System.err.println("fin");
}
}
 
J

Joona I Palaste

Flip said:
I thought I would have some fun with this one. :> Ya, I need another
coffee. :> Comments enclosed. I used notepad.exe and tried to open a file
in a directory with known spaces in it. There are a few options to pick
from. :> Good luck.
// String fullPathAndFilename = new String( "notepad.exe C:" +
File.separatorChar + "Documents and Settings" +
// File.separatorChar +
"phenry" + File.separatorChar +
// "ntuser.ini" );
//String fullPathAndFilename = new String( "notepad.exe
C:\Documents and Settings\phenry\ntuser.ini" );
//String fullPathAndFilename = new String( "notepad.exe
C:/Documents and Settings/phenry/ntuser.ini" );
//String fullPathAndFilename = new String( "notepad.exe
C:\\Documents and Settings\\phenry\\ntuser.ini" );
//String fullPathAndFilename = new String( "notepad.exe
'C:\\Documents and Settings\\phenry\\ntuser.ini'" );
//String fullPathAndFilename = new String( "notepad.exe
\"C:\\Documents and Settings\\phenry\\ntuser.ini\"" );
//String commander = new String( "cmd /c start " +
fullPathAndFilename.toString() );
String commander = new String( "cmd /c start notepad.exe " +
fullPathAndFilename.toString() );

What's with all the String(String) constructors? Can't you just assign
the string literal directly to the variable?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I said 'play as you've never played before', not 'play as IF you've never
played before'!"
- Andy Capp
 
L

Lee Fesperman

Ike said:
I am trying to invoke a windows command whereby a path\filename is passed to
the sustem call. However, the path and filename contain whitespace, and do
not conform to the 8.3 spec that the windows system call is looking for
(please see my 4 lines of code below). Is there any kind of workaround
anyone is aware of for this? Thank you, Ike

String fullPathAndFilename = new String("C:\Documents and Settings\Ike
Eisenhower\Birds Storytime.doc");
String commander = new String("cmd /c start "+fullPathAndFilename);
Process process = Runtime.getRuntime().exec(commander);
process.waitFor();

Use the form of exec() that takes a String[]:

Runtime.getRuntime().exec(new String[] { "cmd", "/c", "start", fullPathAndFilename });
 
J

Joona I Palaste

Use the form of exec() that takes a String[]:
Runtime.getRuntime().exec(new String[] { "cmd", "/c", "start", fullPathAndFilename });

Shouldn't that be:
Runtime.getRuntime().exec(new String[] { new String("cmd"),
new String(new String("/c")), new String(new String(new String("start"))),
new String(new String(new String(new String(fullPathAndFileName)))) });
?
 
T

Tor Iver Wilhelmsen

Joona I Palaste said:
Shouldn't that be:
Runtime.getRuntime().exec(new String[] { new String("cmd"),
new String(new String("/c")), new String(new String(new String("start"))),
new String(new String(new String(new String(fullPathAndFileName)))) });
?

No.
 
J

Joona I Palaste

Tor Iver Wilhelmsen said:
Joona I Palaste said:
Shouldn't that be:
Runtime.getRuntime().exec(new String[] { new String("cmd"),
new String(new String("/c")), new String(new String(new String("start"))),
new String(new String(new String(new String(fullPathAndFileName)))) });
?

Why? This style seems to be used everywhere else.
 
G

Guest

Lee Fesperman said:
Use the form of exec() that takes a String[]:
Runtime.getRuntime().exec(new String[] { "cmd", "/c", "start",
fullPathAndFilename });

Shouldn't that be:
Runtime.getRuntime().exec(new String[] { new String("cmd"), new String(new
String("/c")), new String(new String(new String("start"))), new String(new
String(new String(new String(fullPathAndFileName)))) }); ?

"cmd" is already a String. Since they are immutable, why use up memory
for a new String?

new String( new String( new String( new String( "text ) ) ) ) is a big
waste of time and memory.

La'ie Teachie
 
P

Pete Gieser

La?ie Techie said:
Lee Fesperman said:
Use the form of exec() that takes a String[]:
Runtime.getRuntime().exec(new String[] { "cmd", "/c", "start",
fullPathAndFilename });

Shouldn't that be:
Runtime.getRuntime().exec(new String[] { new String("cmd"), new String(new
String("/c")), new String(new String(new String("start"))), new String(new
String(new String(new String(fullPathAndFileName)))) }); ?

"cmd" is already a String. Since they are immutable, why use up memory
for a new String?

new String( new String( new String( new String( "text ) ) ) ) is a big
waste of time and memory.

La'ie Teachie

Hey Joona, guess you better include a smiley next time...

Pete
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top