Please stop spamming this group with this "problem" of yours.
I've asked you many questions and I gave you lots of hints
as to what you could do to try to find what's wrong, but
obviously you refuse to apply them and you keep posting
basically the exact same message to this group.
The problem is that you don't have the mindset needed
to solve the many problems you'll encounter during your
programming career.
Either you change your mindset or you look for another
job...
Make a script (like a Windows batch script [a .bat file])
that wraps your PDF Creator call. Make that script take
no input at all. Use it with a default file to try at
first.
1. Does it work when you call the *script* manually ?
2. Does it work when you call that same script from
a standalone Java app using Runtime.exec ?
3. Does it work when you call that same script from
your JSP/Servlet ?
> please help me, I have been searching for the
> solution for a long time.
No you haven't. You're putting your head in the
sand, you're not listening to advice, you're not
answering questions.
All you've done is posting this same pathetic
message to this newsgroup hoping that someone
would come with a magical fix.
There are no stupid jobs in this world.
I suggest flipping burgers.
Yes I have made a batch file with default file.
> 1. Does it work when you call the *script* manually ?
It works perfectly when called manually.
> 2. Does it work when you call that same script from
> a standalone Java app using Runtime.exec ?
gives correct output when called through a standalone Java app using
Runtime.exec.
> 3. Does it work when you call that same script from
> your JSP/Servlet ?
PDF creator is called but does not give output... nor writes anything
in the output file.
I have tried all the things. ikts not that I haven't. just that I dont
have anyone to guide me here, no ones from programming background that
I'm get stuck.
Now, If I'm doing the same thing using JPS a blank file is being
created.
My revised code:
package pack;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.DocAttribute;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.OrientationRequested ;
public class Printjob {
public static void main(String[] args)
{
String filename = "C:\\1.doc" ;
new Printjob().printPDFPages(filename);
}
public void printPDFPages(String fileLocation) {
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet
();
pras.add(OrientationRequested.PORTRAIT);
pras.add(new Copies(1));
pras.add(new JobName(fileLocation, null));
PrintService ps1 = null;
PrintService pss[] = PrintServiceLookup.lookupPrintServices(null,
null);
String printerName = "PDFCreator";
for (int i = 0; i < pss.length; i++) {
if (pss[i].getName().equalsIgnoreCase(printerName)) {
ps1 = pss[i];
break; }
}
System.out.println("Default printer: "+ps1.getName());
System.out.println("Printing to " + ps1);
System.out.println("Filename " + fileLocation);
DocAttributeSet attributeSet = new HashDocAttributeSet ();
FileInputStream fin = new FileInputStream(fileLocation);
Doc doc = new SimpleDoc(fin,
DocFlavor.INPUT_STREAM.AUTOSENSE,null);
ps1.createPrintJob().print(doc, pras);
fin.close();
Thread.sleep(30000);
System.out.println("All Done !!");
} catch (Exception ie) {ie.printStackTrace();}
}
}
A blank pdf file is created with name as .pdf and not 1.pdf as
required when I execute the program.
Try *not* closing the FileInputStream.
> fin.close();
"Print service implementors should close any print data streams (ie
Reader or InputStream implementations) that they obtain from the
client doc. ..."
--
Andrew T.
pscode.org
Tried it still a blank file is created.
p = Runtime.getRuntime().exec("cmd /c C:\\test.bat");
int iwaitfor = 240; //2 mins
while(iwaitfor > 0) //wait till thread dead or "iwaitfor" in time.
iwaitfor--;
System.out.println("Sleep started");
Thread.sleep(3000);
System.out.println("Sleep ended");
p.destroy();
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream
()));
Even then the PDFCreator is just seen running without giving output
when called through servlet.
I don't know what you mean by "mins" in the comment, but that loop might take
literally no time at all, depending on what the optimizer does with it. At
most, on modern systems I'd expect it to take about a fraction of a
microsecond to execute.
FWIW, "iwaitfor" does not conform to the Java convention for variable names.
> System.out.println("Sleep started");
> Thread.sleep(3000);
> System.out.println("Sleep ended");
> p.destroy();
Perhaps three seconds isn't long enough for the execution of the batch file to
complete?
You'd be better off actually waiting for the execution than guessing about the
timing.
<http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>
> stdInput = new BufferedReader(new InputStreamReader(p.getInputStream
> ()));
>
> Even then the PDFCreator is just seen running without giving output
> when called through servlet.
Interestingly, you kill the subprocess before you try to read its output.
I've not used 'Process', but that seems suspect to me.
--
Lew
[...]
> Perhaps three seconds isn't long enough for the execution of the
> batch file to complete?
>
> You'd be better off actually waiting for the execution than guessing
> about the timing.
>
> <http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>
>
> > stdInput = new BufferedReader(
> > new InputStreamReader(p.getInputStream ()));
> >
> > Even then the PDFCreator is just seen running without giving output
> > when called through servlet.
>
> Interestingly, you kill the subprocess before you try to read its
> output. I've not used 'Process', but that seems suspect to me.
Same here. I've used Process, although never from a servlet. Out of
curiosity, I tried it. No problem, as long as one resists the temptation
to make "cmd" a request parameter:
<code>
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** @author John B. Matthews */
public class Test extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Exec";
out.println("<html>");
out.println("<head>");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h2>" + title + ": " + new Date() + "</h2>");
doExec("ls", out);
out.println("</body>");
out.println("</html>");
}
private void doExec(String cmd, PrintWriter out) {
String s;
try {
Process p = Runtime.getRuntime().exec(cmd);
// read from the process's stdout
BufferedReader stdout = new BufferedReader (
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null) {
out.println(s + "<br>");
}
// read from the process's stderr
BufferedReader stderr = new BufferedReader (
new InputStreamReader(p.getErrorStream()));
while ((s = stderr.readLine()) != null) {
out.println(s + "<br>");
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
out.println("Exit value: " + p.waitFor() + "<br>");
}
catch (Exception e) {
e.printStackTrace(out);
}
}
}
</code>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
John B. Matthews wrote:
> Same here. I've used Process, although never from a servlet. Out of
> curiosity, I tried it. No problem, as long as one resists the temptation
> to make "cmd" a request parameter:
I note that you do not kill the subprocess prior to attempting to read its
output, that you do not use a timed wait, and that you do use
'Process#waitFor()', the opposite of the three points in the OP's code that
worried me.
--
Lew
> John B. Matthews wrote:
> > Same here. I've used Process, although never from a servlet. Out of
> > curiosity, I tried it. No problem, as long as one resists the
> > temptation to make "cmd" a request parameter:
>
> I note that you do not kill the subprocess prior to attempting to
> read its output, that you do not use a timed wait, and that you do
> use 'Process#waitFor()', the opposite of the three points in the OP's
> code that worried me.
Good points. Misusing destroy() is one I actually tried. I got an
informative stack trace showing a java.io.IOException, the method name,
and the line number where things went awry:
java.io.IOException: ... at Test.doExec(Test.java:35) at ...
The OP might want to verify that no diagnostic output has been discarded
in testing. In addition, the ant <reload/> task can ensure that current
classes are being used. As you and others have suggested, only a
methodical approach will prevail.
Do you mean 'p.waitFor()'?
> I.e [sic] I still see the PDFCreator running for a long time with no output
> being generated.
Is the task waiting for input?
--
Lew
This is because a spawned program directs its System.out output back
to mother. see http://mindprod.com/jgloss/exec.html
Mom has to do something with it. Alternatively spawn a command
interpreter and have it redirect the output and invoke pdfcreator.exe
--
Roedy Green Canadian Mind Products
http://mindprod.com
"Species evolve exactly as if they were adapting as best they could to a changing world, and not at all as if they were moving toward a set goal."
~ George Gaylord Simpson
To Roedy Green:
> This is because a spawned program directs its System.out output back
> to mother. seehttp://mindprod.com/jgloss/exec.html
>
> Mom has to do something with it. Alternatively spawn a command
> interpreter and have it redirect the output and invoke pdfcreator.exe
Can you explain in more detail please?
I'm calling the command interpreter which in turns calls the
pdfcreator.exe.
p = Runtime.getRuntime().exec("cmd /c C:\\test.bat");
My test.bat code is:
@echo off
start C:\\"Program Files"\\PDFCreator\\PDFCreator.exe /NOSTART /PFC:\
\1.doc
echo File created.
Why the extra backslashes in the .bat file?
--
Lew
The extra backslashes are used since single backslash is considered as
special character and not as separator.
That is required when *Java* is parsing the
String. In this case, it is being parsed by
the Windows CLI.
--
Andrew T.
pscode.org
ruds wrote:
> The extra backslashes are used since single backslash is considered as
> special character and not as separator.
In a .bat file? Are you quite certain?
--
Lew
Oops, sorry.
Ok I removed that and now I'm getting the proper output for the sample
file.