简体   繁体   中英

Java - how to run my application completely silently without any GUI or Command prompt popup?

I wrote this printing application, which is launched from Python27 on Windows 8.1 64-bit. My problem is that its a Kiosk in public location, when Python27 launch the Java then it has a windows command prompt with close icon (some users click the close icon on command prompt and job does not get completed)

Question: How to run this following code silently without any command prompt being exposed/popup in Kiosk?

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class JPrint {

  public static boolean saveFile(URL url, String file) throws IOException {
    boolean download_status = false;

    System.out.println("[OK] - 1");
    InputStream in = url.openStream();
    FileOutputStream fos = new FileOutputStream(new File(file));
    System.out.println("[OK] - 2");
    int length = -1;
    byte[] buffer = new byte[1024];

    while ((length = in.read(buffer)) > -1) {
        fos.write(buffer, 0, length);
    }
    fos.close();
    in.close();

    download_status = true;
    System.out.println("[OK] - 3");
    return download_status;
  }

  public static void main(String[] args) throws IOException, PrinterException {  
    String downloaded_filename = "C:/pdf.pdf";
    String download_pdf_from = "http://www.example.com/index/print?kiosk=1";
    String downloaded_filename_open_as_pdf = "C:\\pdf.pdf";
    String printerNameDesired = "HP Photosmart 5520 series"; 

    // Get printers
    PrintService[] services = PrinterJob.lookupPrintServices();
    DocPrintJob docPrintJob = null;
          for (int i = 0; i < services.length; i++) {
            System.out.println(services[i]);
          }   

    try{
      URL url = new URL(download_pdf_from);

      if(saveFile(url, downloaded_filename)) {
        try {
          PDDocument pdf = PDDocument.load(new File(downloaded_filename_open_as_pdf));
          PrinterJob job = PrinterJob.getPrinterJob();
          for (int i = 0; i < services.length; i++) {
           if (services[i].getName().equalsIgnoreCase(printerNameDesired)) {
             docPrintJob = services[i].createPrintJob();
           }
          }

          job.setPrintService(docPrintJob.getPrintService());
          job.setPageable(new PDFPageable(pdf));
          //docPrintJob = service[i].createPrintJob();
          job.print();

        } catch (Exception e) {
          System.out.println("[FAIL]" + e);
        }      
      } else {
        System.out.println("[FAIL] - download fail");
      }      
    } catch (Exception ae) {
      System.out.println("[FAIL]" + ae);
    }


  }
}

Change your Python code to launch the Java app using "javaw" instead of "java".

Note that this is only necessary on Windows. On other platforms the "java" command does not launch a command shell.

For more details, refer to:


Given that you are invoking the Java app from Python like this:

Popen(['java', 
       '-cp', 
       'C:/Python27/pdfbox-app-2.0.0-RC3.jar;C:/Python27/jprint.jar',
       'JPrint']) 

the straight-forward fix is to change java to javaw ; ie

Popen(['javaw', 
       '-cp', 
       'C:/Python27/pdfbox-app-2.0.0-RC3.jar;C:/Python27/jprint.jar',
       'JPrint']) 

@eryksun points out that you can achieve the same ends by telling Popen to override the application's default launch flags; eg

DETACHED_PROCESS = 8
Popen(['java', 
       '-cp', 
       'C:/Python27/pdfbox-app-2.0.0-RC3.jar;C:/Python27/jprint.jar',
       'JPrint'],
      creationflags=DETACHED_PROCESS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM