• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can' t lookup printService by system call

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello JGuru,
I implemented a printing program in Java. It takes file name and printer name as it command line argument. If the program finds the print name matches, it prints to that printer. It prints to default printer if no match was found.

If I run this program on a DOS command line, it works as expected but when invoke from a Cpp code using system call to command line, the java program could not find any printer.

In my java program, I created a class call PrinterInfo, using PrintJob.lookupPrintServices() to look up printers that are available. This method call return no element if invoked the entire program invoked by system call from Cpp. Any hint or idea is greatly appreciated.


public static int lookupPrinter(String requestPrinterName) //throws PrinterException
{
PrintService desiredService = null;
PrintService printServices[] = PrinterJob.lookupPrintServices();
Trace.log("requestPrinterName[ " + requestPrinterName + "]");
Trace.log("printServices [" + printServices.length + "]");
if (printServices.length == 0)
{
return PrinterState.NO_PRINTER;
// throw new PrinterException(PrinterReason.NO_PRINTER_AVAILABLE);
}
// Search desired printer is in the list
String poolPrinter = "";
for (int i = 0 ; i < printServices.length; i++)
{
poolPrinter = printServices[i].getName();
Trace.log("service found " + poolPrinter + "\n");
// System.out.println();
if (poolPrinter.equalsIgnoreCase(requestPrinterName)||
poolPrinter.endsWith(requestPrinterName))
{

//System.out.println("I want this one: " + printServices[i].getName());
desiredService = printServices[i];
currentService = desiredService;
printerName = requestPrinterName;
state = PrinterState.REQUEST_PRINTER;
break;
}
}

if (desiredService == null)
{
//System.out.println("currentService [" + currentService + "]");
currentService = PrintServiceLookup.lookupDefaultPrintService();
printerName = currentService.getName();
state = PrinterState.DEFAULT_PRINTER;
}
return state;
}
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic