Java Execute External Program

  

Active1 month ago

Compile and Run Java in Command Line with External Jars. Compile & Run Java Program With External Jar. Now let's download a third-party library and use some method from the library. In this case, I downloaded apache.commons.lang from here, and use the StringUtils.capitalize method. The jar file is downloaded to 'jars' directory which is. Apr 04, 2016  Friends its very simple. Import os module and run os.system(command) 2. Import subprocess and run subprocess.Popen.

Java Execute External Program

I'm trying to execute an external program with Java using ProcessBuilder, but it expects input from the user.

To be more specific, the program is PGSQL (Postgres SQL), when it's executed the program prompts the user for a password. The only way to bypass that is to save a file in the user home containing the passwords, I'm trying to avoid that, so I want to execute the program from Java and send the password using the process' output stream.

The code works fine when the program doesn't expect any user input, but when I delete the password file from the user home, the program hangs. I see that it's being executed, but nothing happens. If I debug it, it reaches the while and then nothing happens until I kill the process.

Java Execute External Program

This is the code, any help will be greatly appreciated.

Thanks a lot.

Harry PotelHarry Potel

2 Answers

I believe the prompt is going to STDERR, not STDOUT, so you'll have to open a stream connected to that and read there. When you try to read from STDOUT your code hangs waiting for output that will never arrive.

EDIT: I see you have redirected the error stream in the ProcessBuilder.

One other possibility is that the BufferedReader is waiting for a newline to finish reading, and the prompt does not end with a newline.

Jim GarrisonJim Garrison

Java Execute External Program With Arguments

74.9k14 gold badges131 silver badges170 bronze badges

Java Execute External Program Review

Been a long time, but got exactly the same problem. Here is a SSCCE that should work:

remi

Java Exec External Program

remi
3,2971 gold badge14 silver badges32 bronze badges

Not the answer you're looking for? Browse other questions tagged java or ask your own question.