Java Runtime.getRuntime().exec
当我在 Windows 命令提示符中运行此命令时,它会打开默认浏览器并加载相应的页面:
START: http://google.com
我尝试使用下面的 Java 代码执行相同的操作,但收到错误消息。
Runtime.getRuntime().exec(new String[] {"START", "http://google.com"});
这是我收到的错误:
Exception in thread "main" java.io.IOException: Cannot run program "START": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Test2.main(Test2.java:78)
我运行的是 Windows Vista。
When I run this in my Windows command prompt it opens the default browser and loads the respective page:
START: http://google.com
I am trying to do the same thing with the Java code below but get an error message.
Runtime.getRuntime().exec(new String[] {"START", "http://google.com"});
This is the error I am getting:
Exception in thread "main" java.io.IOException: Cannot run program "START": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Test2.main(Test2.java:78)
I am running Windows Vista.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 START 是内置的命令提示符而不是可执行文件,因此您无法从 Java 调用它。如果要使用系统默认浏览器查看网页,请使用 java.awt.Desktop.browse()。
I believe that START is a command prompt builtin rather than an executable file, so you can't call it from Java. If you want to view a webpage with the system's default browser, use java.awt.Desktop.browse().