集成命令行软件与Java程序

发布于 2024-10-06 17:46:50 字数 706 浏览 0 评论 0原文

我正在将开源 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许一世地老天荒 2024-10-13 17:46:51

ProcessBuilder 正是您所需要的。我刚刚为您编写了一些代码框架。接受并根据您的需要进行修改。

public Process exec(        
        String programName,
        Integer maxSize,
        Integer maxSizeO,
        String[] inFiles,
        String outFile) throws IOException {


    ProcessBuilder pb = new ProcessBuilder();

    List<String> cmd = new ArrayList<String>();
    cmd.add(programName);
    if (maxSize != null) {
        cmd.add("-a");
        cmd.add("" + maxSize);
    }
    if (maxSizeO != null) {
        cmd.add("-o");
        cmd.add("" + maxSizeO);
    }
    if (inFiles != null) {
        cmd.addAll(Arrays.asList(inFiles));
    }
    if (outFile != null) {
        cmd.add(outFile);
    }

    Process p = pb.start();

    return p;
}

ProcessBuilder is exactly what you need. I have just written some code skeleton for you. Take it and modify according to your needs.

public Process exec(        
        String programName,
        Integer maxSize,
        Integer maxSizeO,
        String[] inFiles,
        String outFile) throws IOException {


    ProcessBuilder pb = new ProcessBuilder();

    List<String> cmd = new ArrayList<String>();
    cmd.add(programName);
    if (maxSize != null) {
        cmd.add("-a");
        cmd.add("" + maxSize);
    }
    if (maxSizeO != null) {
        cmd.add("-o");
        cmd.add("" + maxSizeO);
    }
    if (inFiles != null) {
        cmd.addAll(Arrays.asList(inFiles));
    }
    if (outFile != null) {
        cmd.add(outFile);
    }

    Process p = pb.start();

    return p;
}
神回复 2024-10-13 17:46:51

我建议您首先为您的功能定义一个接口。该接口定义了设置参数(强制/可选)和执行实际工作的方法。首先,您添加一个使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文