Link to home
Start Free TrialLog in
Avatar of rohitdivas
rohitdivas

asked on

Runtime.getRuntime().exec("pen file "..) fails if path has spaces. Why ?

Hi,
Developing a application on Mac OS X 10.4 and using java 1.5 release 4.

I need to open the folder containing my application through my application.

The program works file if there are no "blank white" spaces in the path, but fails if there are some spaces in between the path. Below it the piece of code. Please suggest a way to resolve the issue.

For example, if the application is run from following path

Applications/RDH myproject/test program

the app fails to open the "test program" directory but if i run the application from

Application/RDH/test

it runs,

///////////////////
import java.awt.Frame;
import java.awt.FileDialog;
import java.io.File;
import java.io.IOException;
import com.apple.mrj.MRJFileUtils;

public class FileTest
{

    public static void main(String[ ] args)
    {
        //new ExecTest();
        new FileTest();
            System.exit(0);
    }
      
      public FileTest()
      {
       try
                {
               
                        File obj = new File(".");
                        String test = obj.getCanonicalPath();
                        System.out.println("test="+test);
                        Runtime.getRuntime().exec("open file://"+test);

                        }
                catch (IOException exc)
                {
                    exc.printStackTrace();
                }
      }
}
//////////////////

Please let me know incase my query is not clear.

Regards,
RDH
Avatar of Ajay-Singh
Ajay-Singh

Try using process builder

ProcessBuilder pb = new ProcessBuilder("open", "file://"+test);
Process p = pb.start();
Avatar of rohitdivas

ASKER

Hi Ajay,

The issue is not related with the exec command but about the white space b/w the path.

Rohit
> Runtime.getRuntime().exec("open file://"+test);
Since there is space in path, the above exec calls treats the individual tokens a arguments for the command open.
Avatar of Mick Barry
spaces are not valid in a url, you could try:

Runtime.getRuntime().exec("open file://"+URLEncoder.encode(test, "UTF8"));
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what about
Runtime.getRuntime().exec(new String[] {"open", "file://" + test});
File.toURI() will escape white spaces
Following resolved the issue finally.,

String pathToLaunch = fObj.getCanonicalPath().replaceAll(" ","%20");
Runtime.getRuntime().exec("open file://"+pathToLaunch);


RDH
> String pathToLaunch = fObj.getCanonicalPath().replaceAll(" ","%20");

easier (and safer) to doi what I suggested above :)
Thanks objects,

B/w whom do i give the points now ? :)

RDH
imo, I told u how to fix it :)
Using replaceAll() does the same as what I suggested, advantage of my suggestion is it will correct *all* invalid characters. Not just spaces.
Do as you feel fit.
Thanks object,
RDH