tags:

views:

71

answers:

3

Hi all,

it works perfectly fine if I paste the following URL directly into my IE address bar:

http://translate.google.cn/translate?hl=zh-CN&sl=zh-CN&tl=en&u=http%3A%2F%2Fnews.baidu.com%2Fns%3Fword%3D%25B0%25C2%25B0%25CD%25C2%25ED

it will translate some Chinese news pages into English ones.

but if I call this address in a Java program like:

Process q=Runtime.getRuntime().exec(
  "cmd /c start http://translate.google.cn/translate?hl=zh-CN&sl=zh-CN&tl=en&u=http%3A%2F%2Fnews.baidu.com%2Fns%3Fword%3D%25B0%25C2%25B0%25CD%25C2%25ED"); 

It will only return to the Google translate main page.

Wonder what went wrong there.

and it would be great if you help me how to realize this(opening the Google translated result page simplying by supplying the URL,in a Java program).

Many thanks.

A: 

My guess is : Google might be checking to see the user agent. They would do it to discourage automated programs and any DOS attacks.

schar
Nope, that's not it.
Joey
@Johannes Rössel You are correct. My mistake.
schar
+4  A: 

The & character is a special character for the shell. Try quoting the argument instead:

Process q=Runtime.getRuntime().exec(
    "cmd /c start \"\" \"http://translate.google.cn/translate?hl=zh-CN&sl=zh-CN&tl=en&u=http%3A%2F%2Fnews.baidu.com%2Fns%3Fword%3D%25B0%25C2%25B0%25CD%25C2%25ED\"");

Note the empty quotes in front of the argument; they are necessary for start.

By the way, you can easily try out whether your command line works at all by copying it into the command line and watching the result. In your case it produced the following:

H:>cmd /c start http://translate.google.cn/translate?hl=zh-CN&sl=zh-CN&tl=en&u=http%3A%2F%2Fnews.baidu.com%2Fns%3Fword%3D%25B0%25C2%25B0%25CD%25C2%25ED
'sl' is not recognized as an internal or external command,
operable program or batch file.
'tl' is not recognized as an internal or external command,
operable program or batch file.
'u' is not recognized as an internal or external command,
operable program or batch file.

which gives pretty good clues about what goes wrong here.

However, if you are on Java 6 you can also use the browse method of the Desktop class. This has the benefit of working on other systems than Windows as well.

Joey
This works well, I retract my answer
Oskar Kjellin
@Kurresmack, I tend to just try it out which usually quickly tells me what exactly goes wrong. Especially helpful in vague question situations :-)
Joey
Yeah I had a similar problem today at work (tho not too similar cause it wasn't programming relates) that the url did not work. Then the solution was to copy the url unframed... Should have tried it this way however. Tried using cmd just before you edited your post and got the same result :(
Oskar Kjellin
@Johannes can you also format the code in the question, to keep the SO awesome? :)
Lipis
+1  A: 

I would say that it because it is framed. Try right clicking the translated page and get the URL that way

Oskar Kjellin