PL-RL
PL-RL

Reputation: 455

Java and C integration - running an app

I need your help. I have a C executable called "generator.out" that is a main() function that receives int argc and char * argv[]. The arguments for this main function are a file (let's call it sample.da) and a target file (let's call it out.bn). I need to create a java interface that can read those names (sample.da and out.bn) and run my function. The code I have so far is:

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);
    }

}

Please note that I'm a newbie in java so I don't understand very much of this. I just need a simple interface to run this program!

Thank you very much!

Upvotes: 1

Views: 136

Answers (2)

aleroot
aleroot

Reputation: 72636

Take a look at ProcessBuilder class, here's an example that could fit your situation :

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();

Upvotes: 2

Mattias Isegran Bergander
Mattias Isegran Bergander

Reputation: 11909

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"}):

Upvotes: 0

Related Questions