Exec and command

U

Unite

I have the following code :

Runtime rt = Runtime.getRuntime();
String commandLine = "\""+path+application+"\" /t "+"\""+PDFStore+"\
\"+item.toString()+"\" \""+currentPrinter+"\"";
Process p = rt.exec(commandLine);
p.destroy() ;
System.out.println(commandLine);

My problem is that if I use the output from the Eclipse Console window
below :
"F:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe" /t "F:
\Documents and Settings\UnitePC\My Documents\46-200805.pdf " "EPSON
Stylus CX3200"

I can copy paste it and run from anywhere on the computer through
windows command and it works 100% but the Process p =
rt.exec(commandLine); does not do anything.

Am I missing a step somewhere? As said the value from
System.out.println(commandLine); works from within windows command.

Thanks
 
A

Andrew Thompson

I have the following code :

(snippets snipped)
Am I missing a step somewhere?

The usual advice is to make sure you account for
the output streams of the Process, both standard
and error.

If that does not help, and nobody else spots an
obvious error, it might help to post an SSCCE*.

* <http://sscce.org/>
 
G

Gordon Beaton

I have the following code :

Runtime rt = Runtime.getRuntime();
String commandLine = "\""+path+application+"\" /t "+"\""+PDFStore+"\
\"+item.toString()+"\" \""+currentPrinter+"\"";
Process p = rt.exec(commandLine);
p.destroy() ;
System.out.println(commandLine); [...]
but the Process p = rt.exec(commandLine); does not do anything.

No exception?

Make sure you read the output of Process.getInputStream() and
Process.getErrorStream() while the program is running or it may hang.

Also, runtime.exec(String) ignores the quoting in your command line.
If you need to use quotes to keep arguments together, use
Runtime.exec(String[]) instead.

With Runtime.exec(String[]) put each argument into a single element of
the array. The array keeps the arguments together implicitly.

Your command should look something like this:

String[] commandLine = {
"F:\\Program Files\\Adobe\\Reader 8.0\\Reader\\AcroRd32.exe",
"/t",
"F:\\Documents and Settings\\UnitePC\\My Documents\\46-200805.pdf",
"EPSON Stylus CX3200"
};

(either double every backslash in string literals, or use forward
slashes instead)

If it still doesn't work, display the exception stacktrace or the
result of Process.exitValue().

/gordon

--
 
U

Unite

Got it working even with the quotes thanks to including
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

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

Another question I got is should I create a new thread for each
execution?
Basically my program takes PDF's in a directory and prints the ones
checked. This can amount to 1000's of PDF's.
So should I create 1 thread and run the for loop in this thread or
have a new thread for each loop?
I would use a PDF JAR to print but the free ones like itext are
unreliable for all PDF types so the installed exe just makes sense to
use.
 
R

Roedy Green

I can copy paste it and run from anywhere on the computer through
windows command and it works 100% but the Process p =
rt.exec(commandLine); does not do anything.

Try echoing the commandline just before you exec it to make sure it
is what you think you composed.

System.out.println( "[" + commandLine + "]" );


You may find you forgot to double your \ in your string literals. It
may be a stray lead or trail space. The echo should show only single
\. Often I use / instead of \, since it bypasses the \\ problem, and
it is compatible with URLs.


There are other overloaded variants where you hand over each parm
without surrounding quotes, so you are not counting so much on how the
command is parsed.

See http://mindprod.com/jgloss/exec.html
 
U

Unite

I've never seen it be unreliable for any PDF usage I've observed of
it.
What exactly do you mean by "unreliable"?


To you.

From what I read online it will open and print PDF's 100% created by
itself but has problems with PDF's created with other programs some of
the times like alignments, fonts and pictures. These are invoices,
statements etc so when they printed have to be 100% correct according
to original PDF.
 
U

Unite

I've never seen it be unreliable for any PDF usage I've observed of
it.
What exactly do you mean by "unreliable"?


To you.

From what I read online it will open and print PDF's 100% created by
itself but has problems with PDF's created with other programs some of
the times like alignments, fonts and pictures. These are invoices,
statements etc so when they printed have to be 100% correct according
to original PDF.
 
A

Andrew Thompson

I have the following code :

(snippets snipped)
Am I missing a step somewhere?

The insightful advice is to make troublesome you account for
the output streams of the Process, both standard
and pan.

If that does not misfire, and nobody else spots an
outrageous algorithm, it might consider to post a SSCCE*.

* <http://sscce.org/>

--
Jessica T.
PhySci.org


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, degenerate, Skull and Bones, propaganda, brainwash,
mind control, fanatic, deranged, idiot, lunatic, retarded]

"I think anybody who doesn't think I'm smart enough
to handle the job is underestimating."

--- Adolph Bush,
U.S. News & World Report, April 3, 2000
(Thanks to Alfred Stanley, Austin, Texas.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top