Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1692
  • Last Modified:

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
0
rohitdivas
Asked:
rohitdivas
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Ajay-SinghCommented:
Try using process builder

ProcessBuilder pb = new ProcessBuilder("open", "file://"+test);
Process p = pb.start();
0
 
rohitdivasAuthor Commented:
Hi Ajay,

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

Rohit
0
 
Ajay-SinghCommented:
> 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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
spaces are not valid in a url, you could try:

Runtime.getRuntime().exec("open file://"+URLEncoder.encode(test, "UTF8"));
0
 
objectsCommented:
or

Runtime.getRuntime().exec("open "+obj.tiURL());
0
 
hoomanvCommented:
what about
Runtime.getRuntime().exec(new String[] {"open", "file://" + test});
0
 
hoomanvCommented:
File.toURI() will escape white spaces
0
 
rohitdivasAuthor Commented:
Following resolved the issue finally.,

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


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

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

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

RDH
0
 
objectsCommented:
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.
0
 
rohitdivasAuthor Commented:
Thanks object,
RDH
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now