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");
}
}