using URL#toString worked

using URL#toString worked fine for me. Seems the easiest. When having a File someFile then one can do someFile.toURI().toURL() to get it escaped correctly. I know that an URL pointing to resources inside a jar (jar:file://) does not work for the 'start' command. Also apparently the 'start' command knows how to handle URLs, but when using some other program instead of 'start' (e.g. C:\someProgram.exe) the behavior may be different.

public static void startFileWithWindowsDefaultProgram(final URL url)
{
	try
	{
		Runtime.getRuntime().exec( //
		         String.format("cmd /C \"start %1s\"", url.toString()), //
		         null, //
		         null);
	} catch (final Exception e)
	{
		e.printStackTrace();
	}
}
public static void main(final String[] args) throws Exception
{
	startFileWithWindowsDefaultProgram(new File("C:\\Program Files\\AC3Filter\\pic\\email.gif")
	         .toURI().toURL());
}

Reply

Optional. The content of this field is kept private and will not be shown publicly.
Optional. If you want your name to link to your homepage.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Lines and paragraphs break automatically.
More information about formatting options