39 lines
1.7 KiB
Java
39 lines
1.7 KiB
Java
import javax.print.PrintService;
|
|
import javax.print.PrintServiceLookup;
|
|
import javax.print.attribute.HashPrintRequestAttributeSet;
|
|
import javax.print.attribute.PrintRequestAttributeSet;
|
|
import javax.print.attribute.standard.PrinterName;
|
|
import java.net.URI;
|
|
|
|
public class ManualPrinterDiscovery {
|
|
public static void main(String[] args) {
|
|
// Create attribute set to search for specific printer types
|
|
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
|
|
|
|
// Try to find services with different attributes
|
|
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributes);
|
|
|
|
if (services.length == 0) {
|
|
System.out.println("No printers found via standard lookup.");
|
|
System.out.println("\nTrying alternative approach...");
|
|
|
|
// Sometimes the service cache is empty - try to force refresh
|
|
try {
|
|
Class<?> psLookup = Class.forName("sun.print.PrintServiceLookup");
|
|
java.lang.reflect.Method method = psLookup.getDeclaredMethod("refreshServices");
|
|
method.setAccessible(true);
|
|
method.invoke(null);
|
|
|
|
// Try lookup again
|
|
services = PrintServiceLookup.lookupPrintServices(null, attributes);
|
|
System.out.println("After refresh: " + services.length + " printers");
|
|
} catch (Exception e) {
|
|
System.out.println("Refresh failed: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
for (PrintService service : services) {
|
|
System.out.println("Printer: " + service.getName());
|
|
}
|
|
}
|
|
} |