Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2006-06-29
12
Medium Priority
?
1,837 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 375 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

Get expert help—faster!

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

607 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