我的问题是,当我执行我的代码时, AcroRd32.exe 进程会打开,因此它似乎可以工作,但是没有打印任何内容,代码永远不会结束。它似乎阻挡了p.waitFor()
。
这是我的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Map.Entry;
class AfficheurFlux implements Runnable {
private final InputStream inputStream;
AfficheurFlux(InputStream inputStream) {
this.inputStream = inputStream;
}
private BufferedReader getBufferedReader(InputStream is) {
return new BufferedReader(new InputStreamReader(is));
}
@Override
public void run() {
BufferedReader br = getBufferedReader(inputStream);
String ligne = "";
try {
while ((ligne = br.readLine()) != null) {
System.out.println(ligne);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class CustomPrintReport extends BaseAction {
private PropertyList props;
private String location;
@Override
public void processAction(PropertyList properties) throws SapphireException {
Logger.logInfo("Method CustomPrintReport START");
props = properties;
// Generate the pdf file to print
String file = generateReportToFile();
printFileToPrinter(file);
cleanTemporaryFile(file);
Logger.logInfo("Method CustomPrintReport END");
}
private void printFileToPrinter(String file) throws SapphireException {
Logger.logInfo("Method printFileToPrinter START");
String printer = props.getProperty("addressid");
try
{
/* Remark : location = C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe */
List<String> params = java.util.Arrays.asList(location, "\\t", file, printer);
ProcessBuilder pb = new ProcessBuilder(params);
Process p = pb.start();
AfficheurFlux fluxSortie = new AfficheurFlux (p.getInputStream());
AfficheurFlux fluxErreur = new AfficheurFlux (p.getErrorStream());
new Thread(fluxSortie).start();
new Thread(fluxErreur).start();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Logger.logInfo("Method printFileToPrinter END");
}
}
编辑:
我也试过这个:
location = C:\ Program Files(x86)\ Adobe \ Acrobat Reader DC \ Reader and command = AcroRd32.exe。但有了这个,我得到错误&#34;无法运行程序&#34; AcroRd32.exe&#34; (在目录&#34; C:\ Program Files(x86)\ Adobe \ Acrobat Reader DC \ Reader&#34;):CreateProcess error = 2,找不到文件;&#34;。
List<String> params = java.util.Arrays.asList(command, "/t", file, printer);
ProcessBuilder pb = new ProcessBuilder(params);
pb.directory(new File(location));
Process p = pb.start();
EDIT2:我也试过这个
String[] params = {location+"\\"+command, "/t", file, printer};
Process p = Runtime.getRuntime().exec(params);
或
Process p = Runtime.getRuntime().exec(command);
with command =&#34; C:\ Program Files(x86)\ Adobe \ Acrobat Reader DC \ Reader \ AcroRd32.exe&#34; / t&#34; C:\ Windows \ TEMP \ 1528790402183.pdf&#34; &#34; NPI10F21A(HP LaserJet M203dn)&#34;。 如果我直接在cmd提示符中执行此命令,则会打印pdf。