baopham
baopham

Reputation: 17

Launch url on Java by command line

I have a issue for this code . Please help me review :

Runtime.getRuntime().exec("cmd.exe /c start chrome " + url);

This code is OK if I launch the url which does not contain parameter, but i put parameter to this url,the link does not work and dont see parameter on url. thanks

Upvotes: 0

Views: 3978

Answers (3)

Aniruddha Sarkar
Aniruddha Sarkar

Reputation: 1923

The url contains various symbols that cmd may think illegal to use. For example

 cd new folder

is illegal as there is a space between so u must use

cd "new folder"

Just the same you must enclose the url in "" to make it work. Thus you must use:

Runtime.getRuntime().exec("cmd.exe /c start chrome \"" + url + "\"");

Or use this to directly launch the url in default browser:

Desktop.getDesktop.browse(uri);

Note wherever there is a \ in the url you must use \\ in place of it. So that java doesnot mistakes it as an escape sequence.

Upvotes: 0

Krzysiek
Krzysiek

Reputation: 682

Try this:

Runtime.getRuntime().exec("cmd.exe /c start chrome \"" + url + "\"");

Upvotes: 2

Atuos
Atuos

Reputation: 521

Use this:

Desktop.getDesktop.browse(uri);

Upvotes: 3

Related Questions