2

I am trying to execute an external program (.exe) out of a java file. This is my code so far:

try{
                Process process = new ProcessBuilder(path).start();
                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;




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

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

The initial output of the program is displayed on the console window of my java program but unfortunately it requires a user password and hitting enter. I already tried to implement a lot of possible solutions I found on the internet like .inheritIO() or forwarding the commands within process-arguments i.e. Process process = new ProcessBuilder(path,"pw","/c").start(); but that doesn't work. I don't understand why the input stream works perfectly fine, but any kind of output stream does not seem to work.

FlowRyan
  • 205
  • 1
  • 3
  • 7
  • 1
    Well, you didn't mention trying to open the process's output stream (actually it's its input stream, it's an output stream from the Java point of view). Did you try that? Then again, remember that many programs have special interaction for passwords that doesn't involve streams but direct key reading or direct device access. – RealSkeptic Sep 12 '16 at 12:01
  • I'm not sure but I think the external program is written either in java or in c/c++. Is that crucial for my java code? – FlowRyan Sep 12 '16 at 12:05
  • If `inheritIO()` doesn’t work, you can stop. `inheritIO()` is already providing a direct connection. If that doesn’t work, the sub process simply isn’t using that channel, but directly accessing the console—in a way that apparently stops working when being a sub-process. Note that some things that work on a native console won’t work, when your application is launched from an IDE. – Holger Sep 20 '16 at 10:10

1 Answers1

0

You can follow this recipe:

  1. Leave out Java for a moment: see if you can find a way to call that tool using cmd/shell and giving it the password as command line argument (meaning: make such experiments without adding another layer of complexity). If that works, just take those arguments into your Java program to use them there.
  2. If that didn't work, you have to figure if you can "write" the passwords to the process; there are many examples out there how to do that.
  3. If that doesn't work either, than chances are that your .exe isn't reading the password using stdin. In that case, the only thing you can do is: change the way your .exe is working. Example: if your .exe is actually doing some "remote action" on a different system (for which you need the password for); then you could try to establish a password-less communication; based on private/public key pairs.
Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248