Java 运行时或 Processbuilder 或其他 [英] Java Runtime OR Processbuilder OR Other

查看:73
本文介绍了Java 运行时或 Processbuilder 或其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道从 Java 运行命令行可执行文件的最佳选择是什么.命令的目标平台是 Windows 7(+) 和 Unix/Linux.

I'd like to know what the best alternative is for running command line executables from Java. The Target platforms for the commands are Windows 7(+) and Unix/Linux.

我有一个当前使用 Runtime.exec() 以及来自 JavaWorld StreamGobbler 文章的增强的类.它在 Windows 和 Unix 上大约 90% 的时间都可以工作.另外 10% 的时间我需要扩展类,然后摆弄将/bin/sh 的 cmd.exe 放在命令前面.有时我还不得不在使用具有命令和参数的单个字符串之间摆弄以将命令和参数拆分为 String[] 数组.

I have a class that currently uses Runtime.exec() along with the enhancements from the JavaWorld StreamGobbler article. It works about 90% of the time on both Windows and Unix. The other 10% of the time I need to extend the class and then fiddle with putting cmd.exe of /bin/sh in front of the command. I've also had to fiddle sometimes between using a single String that has command and arguments to splitting the command and args into a String[] array.

我最新的是一个新的错误/异常java.lang.IllegalArgumentException:可执行文件名称已嵌入引号,拆分参数."我当前的 Runtime.exec() 类在作为 Java 应用程序运行的 Eclipse 中运行良好,但是一旦我构建它并从实际命令提示符运行,它就会因上述异常而失败.

My latest is a new error/exception "java.lang.IllegalArgumentException: Executable name has embedded quote, split the arguments." My current Runtime.exec() class works fine in Eclipse running as a Java application, but once I build it and run from an actually command prompt, it fails with the above exception.

所以现在我读到我们应该使用 ProcessBuilder 对操作系统平台执行命令行可执行文件.

So now I'm reading that we should be using ProcessBuilder to do command line executables to the OS platform.

我的问题是,最好的选择是什么?Runtime.exec()、ProcessBuilder 或其他一些选项?是否有一种选项可以同时为 Windows 和 Unix/Linux 提供服务?如果不是,哪一种最适合 Windows?哪一种最适合 Unix/Linux?

My question is, what is the best alternative? Runtime.exec(), ProcessBuilder, or some other option? Is there one option that will service both Windows and Unix/Linux? If not, which one works best with Windows? Which one works best with Unix/Linux?

蒂亚,阿迪姆

推荐答案

不确定如何给予 Bohemian 信用,但我使用了 ProcessBuilder...解决方案位于 :

Not sure how to give Bohemian credit, but I used ProcessBuilder...Solution is at :

Java - 带有空格和双引号的 ProcessBuilder 命令参数-引用失败

这篇关于Java 运行时或 Processbuilder 或其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆