By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

exec(cmdArray) to bash shell does not work

P: n/a
I have a java (2.0) program with the following lines:

String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
System.out.println(Runtime.getRuntime().exec(cmdAr ray1));

It compliles properly, but does not print the file to the printer. It
displays the following as the return from Runtime...:

java.lang.UNIXProcess@1034bb5

It apparently is sending something out to bash and getting that result back.

This in within SuSE 8.2 Linux running bash.
The following line typed into the shell works properly:
lp -d hp4m MyFile

Anyone have any ideas?
Thanks, John
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
First -- try using the full path for lp, i.e. "/usr/bin/lp" or similar
("which lp" should give you the full path). Interactive logins will set
a path potentially different from the path your "Runtime" call receives,
so Runtime may not have the path necessary to call just "lp".
-Matt

John Bowling wrote:
I have a java (2.0) program with the following lines:

String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
System.out.println(Runtime.getRuntime().exec(cmdAr ray1));

It compliles properly, but does not print the file to the printer. It
displays the following as the return from Runtime...:

java.lang.UNIXProcess@1034bb5

It apparently is sending something out to bash and getting that result back.

This in within SuSE 8.2 Linux running bash.
The following line typed into the shell works properly:
lp -d hp4m MyFile

Anyone have any ideas?
Thanks, John


Jul 17 '05 #2

P: n/a
That solved the problem of printing the file.
If I specify the filename as "MyFile*", which could be handy when you have
multiple files that are similar but with a count, it will not find the file.
Currently I don't need to do that, but I probably will at some point.
Thanks, John

"Matt Smith" <ma********@uconn.edu> wrote in message
news:3F**************@uconn.edu...
First -- try using the full path for lp, i.e. "/usr/bin/lp" or similar
("which lp" should give you the full path). Interactive logins will set
a path potentially different from the path your "Runtime" call receives,
so Runtime may not have the path necessary to call just "lp".
-Matt

John Bowling wrote:
I have a java (2.0) program with the following lines:

String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
System.out.println(Runtime.getRuntime().exec(cmdAr ray1));

It compliles properly, but does not print the file to the printer. It
displays the following as the return from Runtime...:

java.lang.UNIXProcess@1034bb5

It apparently is sending something out to bash and getting that result back.
This in within SuSE 8.2 Linux running bash.
The following line typed into the shell works properly:
lp -d hp4m MyFile

Anyone have any ideas?
Thanks, John

Jul 17 '05 #3

P: n/a
Wildcards are a shell function. I am not overly familiar with
"Runtime.exec", but my guess would be that the command is executed
directly by the system, not issued through a shell. If you really want
the shell features (like wildcarding), you may want to issue something
like this (with adjusted paths as needed):

"/bin/bash -c \"/usr/bin/lp -d hp4m MyFile*\" "

This forces the BASH shell to execute the command, and since the shell
is handling the command, shell functions like wildcards should work.

Note the escaped (\") quotes -- I believe (I may be wrong) that the -c
option for bash needs quotes around it's command string. Escape the
inner quotes with a backslash to make sure Java does not interpret them
as closing your string.

Hope that helps,
-Matt
John Bowling wrote:
That solved the problem of printing the file.
If I specify the filename as "MyFile*", which could be handy when you have
multiple files that are similar but with a count, it will not find the file.
Currently I don't need to do that, but I probably will at some point.
Thanks, John

"Matt Smith" <ma********@uconn.edu> wrote in message
news:3F**************@uconn.edu...
First -- try using the full path for lp, i.e. "/usr/bin/lp" or similar
("which lp" should give you the full path). Interactive logins will set
a path potentially different from the path your "Runtime" call receives,
so Runtime may not have the path necessary to call just "lp".
-Matt

John Bowling wrote:
I have a java (2.0) program with the following lines:

String cmdArray1[] = {"lp", "-d", "hp4m", "MyFile"};
System.out.println(Runtime.getRuntime().exec(cm dArray1));

It compliles properly, but does not print the file to the printer. It
displays the following as the return from Runtime...:

java.lang.UNIXProcess@1034bb5

It apparently is sending something out to bash and getting that result
back.
This in within SuSE 8.2 Linux running bash.
The following line typed into the shell works properly:
lp -d hp4m MyFile

Anyone have any ideas?
Thanks, John



Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.