Hi;
This is a code that I got from the examples in Oracle in using the javax.print
When i try to compile and run a get an error saying
javax.print.PrintException: java.lang.IllegalArgumentException: URI is not hierarchical
package printtry;
/*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of
* Sun Microsystems, Inc.
* Use is subject to license terms.
*
*/
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.MediaSizeName;
public class PrintPS {
public static void main(String args[]) {
PrintPS ps = new PrintPS();
}
public PrintPS() {
/* Construct the print request specification.
* The print data is Postscript which will be
* supplied as a stream. The media size
* required is A4, and 2 copies are to be printed
*/
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;//DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII
PrintRequestAttributeSet aset
= new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
try {
aset.add(new Destination(new URI("file:c:/sss.xps")));
} catch (URISyntaxException ex) {
Logger.getLogger(PrintPS.class.getName()).log(Level.SEVERE, null, ex);
}
// aset.add(new Copies(2));
// aset.add(Sides.ONE_SIDED);
// aset.add(Finishings.STAPLE);
/* locate a print service that can handle it
*/
PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
// PrinterJob printerJob = PrinterJob.getPrinterJob();
if (pservices.length > 0) {
System.out.println("selected printer" + pservices[0].getName());
//Appendix A Example: PrintPS.java 43
/* create a print job for the chosen service
*/
DocPrintJob pj = pservices[0].createPrintJob();
try {
/* * Create a Doc object to hold the print data.
* Since the data is postscript located in a disk file,
* an input stream needs to be obtained
* BasicDoc is a useful implementation that will if
* requested close the stream when printing is completed.
*/
FileInputStream fis = new FileInputStream("test/example.jpeg");
Doc doc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.JPEG, null);
/* print the doc as specified
*/
// DocFlavor[] d= pj.getPrintService().getSupportedDocFlavors();
// for (int i = 0; i < d.length; i++) {
// DocFlavor docFlavor = d[i];
// System.out.println(docFlavor.toString());
//
// }
pj.print(doc, aset);
} catch (IOException ie) {
System.err.println(ie);
} catch (PrintException e) {
System.err.println(e);
}
}
}
}