-2

I have an executable of c++ that opens a file and writes a line to it. Works fine by itself.

#include <iostream>
#include <fstream>

using namespace std;

int main(){
    ofstream fout;
    fout.open("test.txt");
    if(fout.is_open()){
        cout<<"test"<<endl;
        fout<<"Hello World!" << endl;
    }
    fout.close();
    return 0;
}

And I made a main() in java to call it from there like so:

public static void main(String args[]){
        File f = new Resources().getFile("test.exe");
        System.out.println(f.exists());
        String path = f.getAbsolutePath();


        try{
            Process p = new ProcessBuilder(path).start();
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while(line != null){
                line = br.readLine();
                System.out.println(line);
            }

            p.destroy();

        }catch(Exception e){
            System.out.println(e.getMessage());
        }

    }

The first line in main is from my project, it finds the file, it works too.

The thing is when the exe is run alone it creates the file and writes to it. When I run the java main() the file is not created even though I get the output from the stdout. So the executable runs but doesn't create the file.

I am going nuts peaople... what do?

Vasilis
  • 107
  • 1
  • 4
  • 2
    Where do you expect it to write file? It should create it in java execution directory. Have you tried to write in absolute path in C++ program? – default locale Dec 13 '12 at 08:07
  • 1
    And why do you call `destroy` (not `waitFor`) after processing standard output? – default locale Dec 13 '12 at 08:12
  • I suggest calling [`ProcessBuilder.redirectErrorStream(true)`](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#redirectErrorStream%28%29) – Andrew Thompson Dec 13 '12 at 08:30

2 Answers2

2

Use Process.waitFor() method instead of Process.destroy() method.

0

This would be the easiest that you can use but mind that it will work in your specific easy case. If your exe prints to stderr first it will block. In other case you need 2 threads that will read stdout and stderr of your process not to block.

    try
    {
        Process proc = Runtime.getRuntime().exec( "your exec here" );

        // handle process' stdout stream
        InputStream out = proc.getInputStream();
        //Thread err = proc.getErrorStream();
        BufferedReader br = new BufferedReader( new InputStreamReader( out ) );


        int exitVal = proc.waitFor();
        String line;

        while ( ( line = br.readLine() ) != null )
        {
            System.out.println( line );
        }
    }
    catch( Exception e )
    {
        System.out.println( "oops: " + e.getMessage() );
    }
Artur
  • 7,038
  • 2
  • 25
  • 39
  • Changed my code to be like yours but still for some reason the c++ doesnt create the file and doesnt write to it ... – Vasilis Dec 13 '12 at 18:17