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;
}