Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-29
12
Medium Priority
?
1,639 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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