Sotiris
Sotiris

Reputation: 1

C++ to Java(openmp)

I'd appreciate it if you could help on this

Basically what I need to do is to find the best way to load a cpp file through java.

Explaining more, I have a program written in C++ (openmp) and I need to write a Java Gui that will run this file along with some other tasks.

What is the most efficient and easier way to do this? Do you have any online books , or recommendations for it?

Also could this be done through xml? I mean have the xml structure and load the java's gui file and then the .cpp? How could this work?

Thanks

Upvotes: 0

Views: 280

Answers (2)

AudioDroid
AudioDroid

Reputation: 2322

I think what you are looking for is the JNI: http://en.wikipedia.org/wiki/Java_Native_Interface

Search the web for "JNI getting started" or "JNI tutorial" or "JNI Hello World".

Upvotes: 1

Martijn Courteaux
Martijn Courteaux

Reputation: 68907

Take a look at Runtime.getRuntime().exec(String);. This method invokes another application. Here is some example usage:

public void runBtnActionPerformed(ActionEvent evt)
{
    try {
        Process p = Runtime.getRuntime().exec("./mycppapp"); // or for Windows "mycppapp.exe"
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = br.readLine()) != null)
        {
            System.out.println(line);
        }
    } catch (Exception e)
    {
        // handle here
    }
}

Be sure you compiled your C++ application. It is impossible to run the code without compilation. You can try to compile the C++ through Java, using the same method:

int success = Runtime.getRuntime().exec(new String[]{"g++", "mycode.cpp"}).waitFor();

Upvotes: 0

Related Questions