Solved

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

Posted on 2006-06-29
12
1,519 Views
Last Modified: 2007-12-19
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
Comment
Question by:rohitdivas
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17015484
Try using process builder

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

Author Comment

by:rohitdivas
ID: 17015502
Hi Ajay,

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

Rohit
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17015508
> 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 92

Expert Comment

by:objects
ID: 17015537
spaces are not valid in a url, you could try:

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

Accepted Solution

by:
objects earned 125 total points
ID: 17015538
or

Runtime.getRuntime().exec("open "+obj.tiURL());
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17015625
what about
Runtime.getRuntime().exec(new String[] {"open", "file://" + test});
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17015656
File.toURI() will escape white spaces
0
 

Author Comment

by:rohitdivas
ID: 17015659
Following resolved the issue finally.,

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


RDH
0
 
LVL 92

Expert Comment

by:objects
ID: 17015670
> String pathToLaunch = fObj.getCanonicalPath().replaceAll(" ","%20");

easier (and safer) to doi what I suggested above :)
0
 

Author Comment

by:rohitdivas
ID: 17015863
Thanks objects,

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

RDH
0
 
LVL 92

Expert Comment

by:objects
ID: 17015898
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
 

Author Comment

by:rohitdivas
ID: 17016254
Thanks object,
RDH
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 59
ForLoop Example 3 49
check java version using powershell 13 95
Is Applet the way to go for my drag and drop system? 8 17
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question