Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

PHP exec() java jar doesn't detect default printer, jar works OK and send to printer in windows 10

I am working on a project developing a kiosk web app, the project is a demo of kiosk devices, so i need to send to print a receipt when user clicks a button. I made a java jar that already print the receipt but when it is called from php exec('print.jar', $output) PrintServiceLookup.lookupDefaultPrintService() throws nullpointerexception. I have tried also with system() and shell_exec() and so on. I hope somebody can help cause I have looking for days.

This is the jar code public class PrintText {

public static void main(String[] args) throws PrintException, IOException {

String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService().getName();
System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    
  
  PrinterOptions p=new PrinterOptions();

  p.resetAll();
  p.initialize();
  p.feedBack((byte)2);
  p.color(0);
  p.alignCenter();
  p.setText("PANTALLAS LINCE");
  p.newLine();
  p.setText("MUPI INTERACTIVO");
  p.newLine();
  p.addLineSeperator();
  p.newLine();
  p.alignCenter();
  p.setText("Tu Cuenta");
  p.newLine();
  p.addLineSeperator();
  p.newLine();

  p.alignLeft();
  p.setText("Folio No : 2001 Venta directa : E511");
  p.newLine();              

  p.setText("Fecha : " +  "01/01/1801 22:59");

  p.newLine();
  p.setText("Metodo de Pago : Tarjeta");
  p.newLine();
  p.setText("Suc : Altabrisa Mérida");
  p.newLine();
  p.addLineSeperator();
  p.newLine();
  p.alignCenter();
  p.setText(" - -Recibo de Compra - ");
  p.newLine();
  p.alignLeft();
  p.addLineSeperator();

  p.newLine();

  p.setText("Id DescPrecioQty");
  p.newLine();
  p.addLineSeperator();
  p.newLine();
  p.setText("1" + "" + "Aliens Everywhere" + "" +  "100" + "" + "5"); p.newLine();
  p.setText("1" + "" + "Aliens Everywhere" + "" +  "100" + "" + "5"); p.newLine();
  p.setText("1" + "" + "Aliens Everywhere" + "" +  "100" + "" + "5"); p.newLine();
  p.setText("1" + "" + "Aliens Everywhere" + "" +  "100" + "" + "5"); p.newLine();
  p.setText("1" + "" + "Aliens Everywhere" + "" +  "100" + "" + "5"); p.newLine();
  p.setText("1" + "" + "Aliens Everywhere" + "" +  "100" + "" + "5"); p.newLine();

  p.addLineSeperator();
  p.feed((byte)3);
  p.finit();
  p.feed((byte)15);
  
  

// InputStream is = new ByteArrayInputStream(Header.getBytes("UTF8")); InputStream is = new ByteArrayInputStream(p.finalCommandSet().getBytes());

PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);
DocPrintJob job = service.createPrintJob();

PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();
is.close();

}

}

this is php

$output = exec('print.jar 2>&1'); var_dump($output);

the problem is that PrintServiceLookup.lookupDefaultPrintService() throws null when run with php exec() but works ok if it is called from windows explorer double clicking

question from:https://stackoverflow.com/questions/65601171/php-exec-java-jar-doesnt-detect-default-printer-jar-works-ok-and-send-to-pri

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...