Hi ppl,

i am trying print a txt file from within my java code, here is the code i have done so far
but i am getting some runtime errors,

also i am trying to keep the coding to a minimum...any help would be appreciated,

package AddRFSwitchPac;
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;

public class Printing {
public static void main(String args[]) throws Exception {
String filename = ("D:/report"); // THIS IS THE FILE I WANT TO PRINT
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; // MY FILE IS .txt TYPE
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
As i said any help would be appreciated

thanks for your time, guys..


Recommended Answers

Post the runtime errors that you are getting.

Jump to Post

All 4 Replies

Post the runtime errors that you are getting.

ok i have screen dumped the errors from dos window
and attached it to this reply...

See attachment...

cheers buddy..

You are getting the error because you defined the class to be in the "AddRFSwitchPac" and you are trying to run it with "java Printing". Use "java AddRFSwitchPac.Printing" instead. That is assuming you have the Printing class in a folder named "AddRFSwitchPac" and that you are trying to run it from the directory above "AddRFSwitchPac".

thank you matey,, that was great,,,, why did'nt I remember that..lol

thanks a bunch.. that really helped


Be a part of the DaniWeb community

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