1

我需要你的帮助。我有一个名为“generator.out”的 C 可执行文件,它是一个接收 int argc 和 char * argv[] 的 main() 函数。这个主函数的参数是一个文件(我们称之为 sample.da)和一个目标文件(我们称之为 out.bn)。我需要创建一个可以读取这些名称(sample.da 和 out.bn)并运行我的函数的 java 接口。我到目前为止的代码是:

package swingapps;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.lang.*;


public class Swingapps {
private JButton button = new JButton("Generate Bayesian Network");
private JTextField path=new JTextField(40);
private JTextField name=new JTextField(40);
public Swingapps(JPanel jp) {
    jp.add(button);
    jp.add(path);
    jp.add(name);
    button.addActionListener(new Handler());
    path.addActionListener(new Read());
    name.addActionListener(new Call());
}
String text;
private class Read implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
    text = path.getText();
    path.selectAll();
}
}

String namet;
private class Call implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
    namet = name.getText();
    path.selectAll();
}
}
File filep=new File("text"+File.separator+"text");
File filen=new File("namet"+File.separator+"namet");

private class Handler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        try {
        Process p= Runtime.getRuntime().exec("/home/user/workspace/proj2/./generator.out");
        }
        catch(IOException ioex)
        {
            ioex.printStackTrace();
        }
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Contador               ");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    frame.setContentPane(p);
    p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
    Swingapps app = new Swingapps(p);
    frame.pack();
    frame.setVisible(true);
    }

}

请注意,我是java新手,所以我不太了解这一点。我只需要一个简单的界面来运行这个程序!

非常感谢!

4

2 回答 2

2

看一下ProcessBuilder类,这是一个适合您情况的示例:

String command = "generator.out";
String arg1    = "sample.da";
String arg2    = "out.bn";
java.io.File workinDir = new java.io.File("/tmp");
ProcessBuilder pb = new ProcessBuilder(command, arg1, arg2);
pb.directory(workinDir);
Process p = pb.start();
于 2012-05-25T20:25:07.320 回答
0

Just add the arguments as you would do it on the commandline:

Runtime.getRuntime().exec(
    "/home/user/workspace/proj2/./generator.out sample.da out.bn");

Sometimes it works better using the method taking the different parts as an array:

Runtime.getRuntime().exec(
    new String[] {"/home/user/workspace/proj2/./generator.out",
        "sample.da", "out.bn"}):
于 2012-05-25T20:24:55.483 回答