How to get a list of installed Printers in Java

In this example, we show you how to get a list of installed Printers in Java. Then write the services to txt file.

How to get a list of installed Printers in Java

Video Example

Lookup Print Services

private static String getPrinterNames() {
 String content = "";
 PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
 
 content += "Number of print services: " + services.length;
 content += "\n";
 
 if(services.length != 0 || services != null) {
  int i = 1;
  for(PrintService service : services) {
   String name = service.getName();
   
   content += "Printer " + i + " name: " + name;
   content += "\n";
   i++;
  }
 }
 
 System.out.println(content);
 
 return content;
}

Write the services to printer.txt file

public static void main(String[] args) {
 BufferedWriter writer = null;
 
 String printerNames = "";
 printerNames += getPrinterNames();
 try {
  writer = new BufferedWriter(new FileWriter("printer.txt"));
  writer.write(printerNames);
  
  System.out.println("Done!");
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  try {
   if(writer != null) {
    writer.close();
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
 
}

Full source code

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.jackrutorial</groupId>
 <artifactId>DetectPrinter</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <properties>
  <jdk.version>1.8</jdk.version>
  <jodatime.version>2.5</jodatime.version>
  <junit.version>4.11</junit.version>
  <log4j.version>1.2.17</log4j.version>
 </properties>

 <build>
  <finalName>detectPrinter</finalName>
  <plugins>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.9</version>
    <configuration>
     <downloadSources>true</downloadSources>
     <downloadJavadocs>false</downloadJavadocs>
    </configuration>
   </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>${jdk.version}</source>
     <target>${jdk.version}</target>
    </configuration>
   </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
     <archive>
      <manifest>
       <addClasspath>true</addClasspath>
       <mainClass>com.jackrutorial.App</mainClass>
       <classpathPrefix>dependency-jars/</classpathPrefix>
      </manifest>
     </archive>
    </configuration>
   </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
     <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
       <goal>copy-dependencies</goal>
      </goals>
      <configuration>
       <includeScope>runtime</includeScope>
       <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
      </configuration>
     </execution>
    </executions>
   </plugin>

  </plugins>
 </build>
</project>

Main.java

package com.jackrutorial;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

public class Main {

 public static void main(String[] args) {
  BufferedWriter writer = null;
  
  String printerNames = "";
  printerNames += getPrinterNames();
  try {
   writer = new BufferedWriter(new FileWriter("printer.txt"));
   writer.write(printerNames);
   
   System.out.println("Done!");
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if(writer != null) {
     writer.close();
    }
   } catch (Exception ex) {
    ex.printStackTrace();
   }
  }
  
 }
 
 private static String getPrinterNames() {
  String content = "";
  PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
  
  content += "Number of print services: " + services.length;
  content += "\n";
  
  if(services.length != 0 || services != null) {
   int i = 1;
   for(PrintService service : services) {
    String name = service.getName();
    
    content += "Printer " + i + " name: " + name;
    content += "\n";
    i++;
   }
  }
  
  System.out.println(content);
  
  return content;
 }
}

Output

Number of print services: 5
Printer 1 name: Send To OneNote 2013
Printer 2 name: Microsoft XPS Document Writer
Printer 3 name: Microsoft Print to PDF
Printer 4 name: Foxit Reader PDF Printer
Printer 5 name: Fax

Done!
Latest
Next Post

post written by: