Runtime.getRuntime().exec() 平台独立吗?
是
Runtime.getRuntime().exec("cmd /c");
平台独立的还是我必须在参数中传递特定于平台的命令?
谢谢
linux 中相当于命令 cmd /c start /b java -jar
的命令是什么
Is
Runtime.getRuntime().exec("cmd /c");
platform independent or I have to pass platform specific command in the parameter?
Thanks
What is the linux equivalent of the command cmd /c start /b java -jar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Runtime.getRuntime().exec() 与平台无关,它将在每个平台上启动外部可执行文件。当然,可执行文件本身必须存在,因此在 Linux 上启动 notepad.exe 很可能不起作用(除非您安装了 Wine 和 Notepad,但这是另一个故事)。
在 Windows 上,输入您要在 cmd.exe 中输入的任何内容(详细信息如下)。但要在 Windows 上进行自动路径查找,您需要执行类似
或 的
操作,使用关联的查看器打开 pdf 文件。
在 Linux 上,您可以执行在 Bash 等 shell 中执行的任何操作,但不能使用 bash 内置命令(如管道运算符)。您只需启动程序并传递参数即可。
要在 Linux 上启动另一个 Java 实例,您可以使用:
如果 java 可执行文件不在 PATH 上,则使用它的绝对路径。
Runtime.getRuntime().exec() is platform independant in the way, that it will start an external executable on every platform. The executable itself has to be there of course, so starting notepad.exe on Linux will very likely not work (except when you have Wine and Notepad installed, but this is another story).
On Windows, enter anything you would enter into cmd.exe (details follow). But to do the automatic path lookup on Windows, you need to do something like
or
which opens the pdf file with the associated viewer.
On linux, you can do anything you would do in a shell like Bash, but you cannot use bash builtins like the pipe operator. You can just start programs and pass arguments.
To start another Java instance on linux you could use:
Use the absolute path to the java executable if it is not on the PATH.
它不是独立于平台的。您需要针对您的目标操作环境的外部命令版本。
It is not platform independent. You need a version of the external command for each operating environment you're targeting.
Runtime.getRuntime().exec()
与平台无关。cmd /c
是。您希望在 Unix 上运行cmd /c
得到什么?因此,如果您想制作运行外部命令的跨平台程序,则必须持有特定于平台的命令。
The
Runtime.getRuntime().exec()
is platform independent.cmd /c
is. What are you expecting to get runningcmd /c
on Unix?So, you have to hold platform specific commands if you want to make cross-platform program that runs external commands.
它依赖于平台。引用 相关 javadoc(强调我的):
It's platform dependent. Quoting a section of the relevant javadoc(emphasis mine):
当然不是,你正在执行一个shell命令。我不建议您使用 Java 这样做,除非您没有其他更简洁的方法来完成您想做的事情。它违背了 Java 的可移植性目的。
Of course not, you're executing a shell command. I don't suggest you doing this like that with Java unless you have no other cleaner way of doing what you want to do. It defeats Java's purpose of being portable.