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

Recommended Answers

All 4 Replies

can you show the entire error message?

maybe this can help you?

Come on now Zaad! You have been here long enough to know that you should always include the line number when you post an error message! But presumably it's line 35/6 - is that URI valid? Does the file exist?

This excerpt from, the URI API doc mau help?

A hierarchical URI is either an absolute URI whose scheme-specific part begins with a slash character, or a relative URI, that is, a URI that does not specify a scheme. Some examples of hierarchical URIs are:

http://java.sun.com/j2se/1.3/
docs/guide/collections/designfaq.html#28
../../../demo/jfc/SwingSet2/src/SwingSet2.java
file:///~/calendar

Looks to me like you may need new URI("file:///c:/sss.xps")

commented: Solutino was provided +0

Come on now Zaad! You have been here long enough to know that you should always include the line number when you post an error message! But presumably it's line 35/6 - is that URI valid? Does the file exist?

Sorry about that line was 70

Because the console only gives :
avax.print.PrintException: java.lang.IllegalArgumentException: URI is not hierarchical

and the exception is thrown from line 70

This excerpt from, the URI API doc mau help?


Looks to me like you may need new URI("file:///c:/sss.xps")

Thanks an lot for this... This helped and is working...........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.