简体   繁体   English

Java-如何在没有任何GUI或命令提示符弹出窗口的情况下完全安静地运行我的应用程序?

[英]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. 我编写了此打印应用程序,该应用程序是从Windows 8.1 64位上的Python27启动的。 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) 我的问题是它在公共位置的信息亭,当Python27启动Java时,它有一个带有关闭图标的Windows命令提示符(某些用户在命令提示符处单击关闭图标,但作业未完成)

Question: How to run this following code silently without any command prompt being exposed/popup in Kiosk? 问题:如何在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". 更改您的Python代码以使用“ javaw”而不是“ java”启动Java应用程序。

Note that this is only necessary on Windows. 请注意,这仅在Windows上是必需的。 On other platforms the "java" command does not launch a command shell. 在其他平台上,“ java”命令不会启动命令外壳程序。

For more details, refer to: 有关更多详细信息,请参阅:


Given that you are invoking the Java app from Python like this: 假设您正在像这样从Python调用Java应用程序:

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 ; 直接的解决方法是将java更改为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; @eryksun指出,可以通过告诉Popen覆盖应用程序的默认启动标志来达到相同的目的。 eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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