Skip to content
Advertisement

Some terminal commands run via Java don’t display output in Linux

I’m trying to write a Java program to run terminal command. Googling and SO got me to here:

JavaScript

So far, so good. The problem is if I try to run a command like “python -V” via

String[] cmd = {"python", "-V"};

The program will run, but no output is actually printed out. Any ideas?

Advertisement

Answer

The output you see on your command line when running python -V is actually being printed to standard error. To capture this, you need to use a different InputStream, such as this:

JavaScript

The rest of your code is fine.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement