-1

I am trying to see whether or not the installed java version is good to run from c++.

I looked everywhere, yet neither pipes nor redirecting the system() call using "cmd + > txtfile.txt" seem to work with the output of "java -version".

I have no idea why, maybe the output stream can't be redirected?

All help is very much appreciated!

  • 2
    What code have you tried? Can you [edit your question](https://stackoverflow.com/posts/58422474/edit) and show your best attempt? –  Oct 16 '19 at 22:35
  • 2
    Show a [mcve] of your current code. Hint: when you do `java -version`, Java responds on stderr (file descriptor 2), not on stdout (file descriptor 1). (which you can verify on the command line with a `>` redirect for stdout and a `2>` redirect for stderr) – Erwin Bolwidt Oct 16 '19 at 22:50

1 Answers1

1

The problem is that "java -version" responds on stderr, which must be redirected using 2> not >.

In conclusion

Write output to file using '2> filename.txt'

system("java -version &> temp.txt");

std::ifstream fin("temp.txt");

std::string Result = "";
std::string Line;

while(getline(fin, Line))
    Result+=Line;

fin.close();

(Thanks to Erwin Bolwidt)