Santhucool
Santhucool

Reputation: 1706

Calling Compiled C++ exe file from java not working

I am trying to call a C++ program from java and my C++ program as follows:

// A hello world program in C++
// hello.cpp

    #include<iostream>
    using namespace std;

    int main()
    {
        cout << "Hello World!";
        return 0;
    }

What I did was I am using minGW compiler for compiling C++ program to hello.exe and when I working it, it is working:

C:\Users\admin\Desktop>g++ -o hello hello.cpp

C:\Users\admin\Desktop>hello.exe
Hello World!

I have created a java program in such a way that it should call the C++compiled program(hello.exe), but my java program is note calling the exe, my program as follows:

//Hello.java

public class Hello {
    public static void main(String []args) {

        String filePath = "hello.exe";
        try {

            Process p = Runtime.getRuntime().exec(filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

Check the output of java program:

C:\Users\admin\Desktop>javac Hello.java

C:\Users\admin\Desktop>java Hello

C:\Users\admin\Desktop>

Why it is not working, Please help me?

Upvotes: 2

Views: 1670

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347214

Very simply, you need to read the output of the process, via the Processs InputStream, for example...

String filePath = "hello.exe";
if (new File(filePath).exists()) {
    try {

        ProcessBuilder pb = new ProcessBuilder(filePath);
        pb.redirectError();
        Process p = pb.start();
        InputStream is = p.getInputStream();
        int value = -1;
        while ((value = is.read()) != -1) {
            System.out.print((char) value);
        }

        int exitCode = p.waitFor();

        System.out.println(filePath + " exited with " + exitCode);
    } catch (Exception e) {
        e.printStackTrace();
    }
} else {
    System.err.println(filePath + " does not exist");
}

Generally speaking, you should use ProcessBuilder over Process, it gives you far more options

Upvotes: 5

Santhucool
Santhucool

Reputation: 1706

Worked !! Thank you Guys for your support!!

import java.io.File;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Hello {
    public static void main(String []args) {
        String filePath = "hello.exe";
        try {
                ProcessBuilder builder = new ProcessBuilder("hello.exe");
                Process process = builder.start();
                InputStream inputStream = process.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 1);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
                inputStream.close();
                bufferedReader.close();
            } catch (Exception ioe) {
                //ioe.printStackTrace();
            }
    }
} 

Upvotes: 0

Related Questions