集成命令行软件与Java程序
我正在将开源 C 程序与 Java 程序集成。
我尝试过使用 JNI,但这对于我想要做的事情来说不是合适的方式。
c程序的用法如下。
program_name argument1 [-a maxSize] [-o maxSizeO] ...... [-P] \
in.txt in1.dat in2.dat in1.tmp in2.tmp out.txt
我可以像这样使用 ProcessBuilder 类运行这个程序。
ProcessBuilder pb = new ProcessBuilder("program_name",
"argument1",
"-a", maxSize,
"-o", maxSizeO.........,
int2.tmp, out.txt);
Process p = pb.start();
但是有很多可选参数,因此维护 Java 程序的方式看起来是错误的。
有什么想法可以通过软件设计的好方法来解决这个问题吗?
提前致谢。
I'm integrating open source c program with Java program.
I'd tried to use JNI but it was not appropriate way from what I want to do.
The usage of c program is same as below.
program_name argument1 [-a maxSize] [-o maxSizeO] ...... [-P] \
in.txt in1.dat in2.dat in1.tmp in2.tmp out.txt
I could run this program using ProcessBuilder class like this way.
ProcessBuilder pb = new ProcessBuilder("program_name",
"argument1",
"-a", maxSize,
"-o", maxSizeO.........,
int2.tmp, out.txt);
Process p = pb.start();
But There are lots of optional arguments, so it looks like a wrong way to maintain my Java program.
Is there any idea to solve this problem in good way of software design?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ProcessBuilder
正是您所需要的。我刚刚为您编写了一些代码框架。接受并根据您的需要进行修改。ProcessBuilder
is exactly what you need. I have just written some code skeleton for you. Take it and modify according to your needs.我建议您首先为您的功能定义一个接口。该接口定义了设置参数(强制/可选)和执行实际工作的方法。首先,您添加一个使用 ProcessBuilder 来完成工作的实现。这样,如果您有更好的解决方案,您可以透明地切换到另一个实现。
I propose you first define an interface for you functionality. This interface defines methods to set the parameters (mandatory / optional) and to do the actual work. To start with, you then add an implementation which uses
ProcessBuilder
to do the work. This way you can transparently switch to another implementation if you have better solution.