Link to home
Start Free TrialLog in
Avatar of old_type
old_type

asked on

Send input to exec process in windows

hi experts,

im working on a small programming project that will act as a front end to another written program, basically im making a GUI for it

i have comepleted most of the parts, except i would like to run everthing in the java window, so i would like to send user input directly to the dos program using java, instead of the user typing it into the dos window, after i have called the exec command, i have some code here of how i tried to do it, but it doesnt work, plz advise

and yes, im a newbie

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
     
// send process the password
// pass alrdy has the password
pass = pass.concat(System.getProperty("line.separator"));
PrintWriter writer = new PrintWriter(proc.getOutputStream());
writer.println(pass);
writer.flush();
int exitVal = proc.waitFor();

// done encryption
String done = "Decrypted ";
done = done.concat(arg1.getName());
JOptionPane.showMessageDialog(ivjJFrameContentPane, done);

thats it, thanks people
Avatar of Venci75
Venci75

what do you mean by - it is not working?

what is the program you are running?
Avatar of old_type

ASKER

i mean that no input seems to be entered into the program

it is a dos version of a pgp program, after typing in the decryption command, it will prompt for a password, this is what im trying to send in using java
try to print the content of the InputStream of the ptocess (for example - to the console) - may be there will be something interesting
Hi.
Can your dos program get command line parameters?
Avatar of CEHJ
>>so i would like to send user input directly to the dos program using java, instead of the user typing it into the dos window

More importantly, can your program get command line input *after* you have started it?
the program can get command line parameters, that part works

right now, it waits for input after i have started it because it is waiting for the password, so the DOS window right now prompts for user to enter the password, what i want to do is enter that in with java, so the user wont have to
What about this:
String cmd = "doc_program -pass " + pass;
and then
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
Does the process terminate?
What is the value of exitVal?

Also, you're sending the program two line feeds which may or may not be important.
what do you mean, does the process terminate?

u mean with my current code?

it would depend on what u mean by terminate, right now i must manually TYPE in the password IN THE DOS window, the above code i have does nothing, plz provide correct code of doing this
I can't see much wrong with your code right now. Comment out the following and try it:

pass = pass.concat(System.getProperty("line.separator"));
i had it commented out before, same results, i just had it in because i saw it in some post here and thought might give it a try

thx for the efforts ppl tho

i do appreciate it
does this code display the message dialog?
the code here, or u guys can suggest other solutions for this...just sending input to the dos window/program

Runtime rt = Runtime.getRuntime();     // works as planned
Process proc = rt.exec(cmd);     // works as planned
   
// send process the password
// pass alrdy has the password
pass = pass.concat(System.getProperty("line.separator"));
PrintWriter writer = new PrintWriter(proc.getOutputStream());
writer.println(pass);
writer.flush();
int exitVal = proc.waitFor();     // works as planned

// done encryption
String done = "Decrypted ";     // works as planned
done = done.concat(arg1.getName());     // works as planned
JOptionPane.showMessageDialog(ivjJFrameContentPane, done);     // works as planned
the code here, or u guys can suggest other solutions for this...just sending input to the dos window/program

Runtime rt = Runtime.getRuntime();     // works as planned
Process proc = rt.exec(cmd);     // works as planned
   
// send process the password
// pass alrdy has the password
pass = pass.concat(System.getProperty("line.separator"));
PrintWriter writer = new PrintWriter(proc.getOutputStream());
writer.println(pass);
writer.flush();
int exitVal = proc.waitFor();     // works as planned

// done encryption
String done = "Decrypted ";     // works as planned
done = done.concat(arg1.getName());     // works as planned
JOptionPane.showMessageDialog(ivjJFrameContentPane, done);     // works as planned
the code here, or u guys can suggest other solutions for this...just sending input to the dos window/program

Runtime rt = Runtime.getRuntime();     // works as planned
Process proc = rt.exec(cmd);     // works as planned
   
// send process the password
// pass alrdy has the password
pass = pass.concat(System.getProperty("line.separator"));
PrintWriter writer = new PrintWriter(proc.getOutputStream());
writer.println(pass);
writer.flush();
int exitVal = proc.waitFor();     // works as planned

// done encryption
String done = "Decrypted ";     // works as planned
done = done.concat(arg1.getName());     // works as planned
JOptionPane.showMessageDialog(ivjJFrameContentPane, done);     // works as planned
ok - add this:
...
writer.flush();

InputStream ind = proc.getInputStream();
int ri;
while ((ri = ind.read()) != -1) System.out.write(ri);

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

and post what is printed on the console
going into the debugger

i see that exitval is 0

and the ri thing is -1

but nothing shows up on console, might be a setting thing, im new to vaj
ok - can you execute a simple test :
save the input for your external application to a file, say 'in.txt'
and run your external program using
prog < in.txt

doesnt work, tried to do it...still prompts for the input???
so - the problem is not in the java code - if you manage to run the external application using 'prog < in.txt' - you will be able to do it with java
ohh really? let me try a few more similar tests and see

thx man
Are you sure about this Venci75? If the password is being passed from the file and only the password then this is equivalent to { COMMAND_NAME } password. This won't necessarily work for a program that wants to *prompt* for a password.

What output do you get at the command line from {COMMAND_NAME} /?
hold it guys, i have no idea what that was, just tell me what to do, and i'll try to do it, but in simpler terms please
Substitute your command (i.e. the name of your program) for {COMMAND_NAME}, taking the rest literally. If your program is called 'pgp', type:

pgp /?

at the command line and let us know what happens
i have to type

gpg -?

and then i get the listing of the commands....
Can you post the output? You could do gpg  -? >post-this.txt if its easier and post the file called  post-this.txt
why dont you explain to me what you are looking for and are trying to test?

cuz this is getting a bit farther from what im trying to do, and i know what command line arguments im using....

again im a newbie....i sometimes dont understand what is going on around me
>>and then i get the listing of the commands....

Just post this. I want to see what the program expects
c:\GnuPG>gpg -?
gpg (GnuPG) 1.2.1
Copyright (C) 2002 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Home: C:/GnuPG
Supported algorithms:
Pubkey: RSA, RSA-E, RSA-S, ELG-E, DSA, ELG
Cipher: 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH
Hash: MD5, SHA1, RIPEMD160
Compress: Uncompressed, ZIP, ZLIB

Syntax: gpg [options] [files]
sign, check, encrypt or decrypt
default operation depends on the input data

Commands:

 -s, --sign [file]              make a signature
     --clearsign [file]         make a clear text signature
 -b, --detach-sign              make a detached signature
 -e, --encrypt                  encrypt data
 -f, --encrypt-files [files]    encrypt files
 -c, --symmetric                encryption only with symmetric cipher
     --store                    store only
 -d, --decrypt                  decrypt data (default)
     --decrypt-files [files]    decrypt files
     --verify                   verify a signature
     --list-keys                list keys
     --list-sigs                list keys and signatures
     --check-sigs               check key signatures
     --fingerprint              list keys and fingerprints
     --list-secret-keys         list secret keys
     --gen-key                  generate a new key pair
     --delete-keys              remove keys from the public keyring
     --delete-secret-keys       remove keys from the secret keyring
     --sign-key                 sign a key
     --lsign-key                sign a key locally
     --nrsign-key               sign a key non-revocably
     --nrlsign-key              sign a key locally and non-revocably
     --edit-key                 sign or edit a key
     --gen-revoke               generate a revocation certificate
     --export                   export keys
     --send-keys                export keys to a key server
     --recv-keys                import keys from a key server
     --search-keys              search for keys on a key server
     --refresh-keys             update all keys from a keyserver
     --import                   import/merge keys
     --list-packets             list only the sequence of packets
     --export-ownertrust        export the ownertrust values
     --import-ownertrust        import ownertrust values
     --update-trustdb           update the trust database
     --check-trustdb            unattended trust database update
     --fix-trustdb              fix a corrupted trust database
     --dearmor                  De-Armor a file or stdin
     --enarmor                  En-Armor a file or stdin
     --print-md algo [files]    print message digests

Options:

 -a, --armor                    create ascii armored output
 -r, --recipient NAME           encrypt for NAME
     --default-recipient NAME   use NAME as default recipient
     --default-recipient-self   use the default key as default recipient
 -u, --local-user               use this user-id to sign or decrypt
 -z N                           set compress level N (0 disables)
     --textmode                 use canonical text mode
 -o, --output                   use as output file
 -v, --verbose                  verbose
 -q, --quiet                    be somewhat more quiet
     --no-tty                   don't use the terminal at all
     --force-v3-sigs            force v3 signatures
     --no-force-v3-sigs         do not force v3 signatures
     --force-v4-certs           force v4 key signatures
     --no-force-v4-certs        do not force v4 key signatures
     --force-mdc                always use a MDC for encryption
     --disable-mdc              never use a MDC for encryption
 -n, --dry-run                  do not make any changes
 -i, --interactive              prompt before overwriting
     --use-agent                use the gpg-agent
     --batch                    batch mode: never ask
     --yes                      assume yes on most questions
     --no                       assume no on most questions
     --keyring                  add this keyring to the list of keyrings
     --secret-keyring           add this secret keyring to the list
     --show-keyring             show which keyring a listed key is on
     --default-key NAME         use NAME as default secret key
     --keyserver HOST           use this keyserver to lookup keys
     --charset NAME             set terminal charset to NAME
     --options                  read options from file
     --status-fd FD             write status info to this FD
     --trusted-key KEYID        ultimately trust this key
     --load-extension FILE      load extension module FILE
     --rfc1991                  emulate the mode described in RFC1991
     --openpgp                  set all packet, cipher and digest options to Ope
nPGP behavior
     --pgp2                     set all packet, cipher and digest options to PGP
 2.x behavior
     --s2k-mode N               use passphrase mode N
     --s2k-digest-algo NAME     use message digest algorithm NAME for passphrase
s
     --s2k-cipher-algo NAME     use cipher algorithm NAME for passphrases
     --cipher-algo NAME         use cipher algorithm NAME
     --digest-algo NAME         use message digest algorithm NAME
     --compress-algo N          use compress algorithm N
     --throw-keyid              throw keyid field of encrypted packets
     --show-photos              Show Photo IDs
     --no-show-photos           Don't show Photo IDs
     --photo-viewer             Set command line to view Photo IDs

(See the man page for a complete listing of all commands and options)

Examples:

 -se -r Bob [file]          sign and encrypt for user Bob
 --clearsign [file]         make a clear text signature
 --detach-sign [file]       make a detached signature
 --list-keys [names]        show keys
 --fingerprint [names]      show fingerprints

Please report bugs to <gnupg-bugs@gnu.org>.
So which command do you want? Show us the command you'd enter at the command line.
the command is

gpg --decrypt-files <filename>

then it prompts,

You need a passphrase to unlock the secret key for user:...

Enter passphrase:

this is where it waits for the password
OK. Are you sure you're putting in

gpg --decrypt-files <filename>

fully into the command?
yes....the command works, im running th java app, and dos window pops up and prompts for the password, if i manually enter it in, it is okay, it works

the only thing my GUI does is lets the user use JFileChooser to find the file they want to decrypt and then i run it from there
I must confess i'm a bit stumped. objects will be online soon!
thx for all the help guys

maybe its just this program doesnt like the java input and wont work...i'll tyr to find a work around, but the output of the progarm i can get fine....

oh well, thx ppl
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's an article to read for some of the gotchas involved with exec:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
>> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

this is indeed a very good article about exec, but unfortunately, it again won't help here because the problem is not at the java side, rather pgp explicitly open a input from console (fopen("con:")).
The final pitfall:
"Runtime.exec() is not a command line"
you can easily make it command line:
cmd.exe /c for NT/2000/xp
command.exe /c for windows95/98/me
/bin/sh -c for for unix
......
but this is not the issue of this question.
a good article don't have to be a bible :)
>> Are you sure about this Venci75?

yes - I am sure, because

prog < in.txt

uses the content of the in.txt as input (replaces the standard input). Using the Process OutputStream is doing the same. If 'prog < in.txt' cannot be used, then the program is not using the standard input.
>>uses the content of the in.txt as input (replaces the standard input).

Yes, but what is required here happens *after* the program is started. Redirecting stdin is only supplying the program with parameters at startup. What is required here is more like an ftp script, where the script is actually replacing input that normally would be entered interactively *after* the start of the program
I said that if the program cannot do this by redirecting the stdin - it won't accept the input sent using process output stream
I don't think that this is the problem, but I wanted to be 100% sure before ignoring this
Yes, i must confess i've never used the ability to do this using the output stream of the process. So this is equivalent to:

command < some-input-file.txt

is it?
yes - you can try this for example with the commad prompt:

cmd < input.txt

where input.txt is a normal .bat file, but having 'exit' as last command
old_type:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
please remove

the question was not answered / solvable
I think I answered the question which is "not possible with command line exec", and an alternative way is also given:

Comment from kennethxu  Date: 11/05/2002 11:46AM PST  
old_type,

your pgp program doesn't read password from standard input, rather specifically read the password from console. java can only send data via stdin to it's child process, in your case, the pgp program. So I don't believe there is anyway you can pass your password.

You'll need to look for java interface to native pgp as an alternative.
http://www.iam.unibe.ch/~jampen/pgpjava/ 
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for kennethxu

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer