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
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="
Runtime.getRuntime().exec(
}
catch (IOException exc)
{
exc.printStackTrace();
}
}
}
//////////////////
Please let me know incase my query is not clear.
Regards,
RDH
ASKER
Hi Ajay,
The issue is not related with the exec command but about the white space b/w the path.
Rohit
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.
Since there is space in path, the above exec calls treats the individual tokens a arguments for the command open.
spaces are not valid in a url, you could try:
Runtime.getRuntime().exec( "open file://"+URLEncoder.encode (test, "UTF8"));
Runtime.getRuntime().exec(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what about
Runtime.getRuntime().exec( new String[] {"open", "file://" + test});
Runtime.getRuntime().exec(
File.toURI() will escape white spaces
ASKER
Following resolved the issue finally.,
String pathToLaunch = fObj.getCanonicalPath().re placeAll(" ","%20");
Runtime.getRuntime().exec( "open file://"+pathToLaunch);
RDH
String pathToLaunch = fObj.getCanonicalPath().re
Runtime.getRuntime().exec(
RDH
> String pathToLaunch = fObj.getCanonicalPath().re placeAll(" ","%20");
easier (and safer) to doi what I suggested above :)
easier (and safer) to doi what I suggested above :)
ASKER
Thanks objects,
B/w whom do i give the points now ? :)
RDH
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.
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.
ASKER
Thanks object,
RDH
RDH
ProcessBuilder pb = new ProcessBuilder("open", "file://"+test);
Process p = pb.start();