direct printing the text file fto the printer from Java

Hi, I am using the below code to directly print the text file to the printer in java and it's work fine.
but I need to send the file as parameter here in this code it is harcode. Because I need to use this code to create a webservice in Jdeveloper.
can help me in this issue as I am new to Java.

package printtxt;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class Class3 {


public static void main(String[] args) {

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

/* locate a print service that can handle it */

PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

/* create a print job for the chosen service */

int printnbr = 0;
DocPrintJob pj = pservices[printnbr].createPrintJob();

try {
    
FileInputStream fis = new FileInputStream("D:\\cback\\menu_security.txt");
Doc doc = new SimpleDoc(fis, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(pj);

/* print the doc as specified */
pj.print(doc, aset);

}
catch (Exception ex){

ex.printStackTrace();
}
}
}

Open in new window

irsbenzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>but I need to send the file as parameter here in this code it is harcode.

So, what's the problem? You'd just write a method with a path as the parameter. Of course, if you want to run that on the server side, then the path will have to exist on the server
for_yanCommented:
just use args[0] as the name of the file and type ioit on command line
irsbenzAuthor Commented:
Hi CEHJ,
Thanks for the reply and your solution,can you just explain me where to write method in that code as I am purely a PL/SQL developer.
Please help me to resolve this.

Thanks in advance
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

CEHJCommented:
If you want to use that server-side, you can just change

>>public static void main(String[] args) {

to the below and use the parameter 'path' in the code instead of what's been hard-coded
public static void print(String path) {

Open in new window

ElbasselCommented:
As CEHJ said, you want to read the file in the server, and you can't get a stream of this file because it is not on the server.
So, you can pass the the data of the file( The array of the bytes that has the data), and then in the server create the file and do what ever you want with this file.
Did I miss something?
Good luck
irsbenzAuthor Commented:
Thanks CEHJ for your reply it's really help me alot, I greatly appriciate it.

One more solution I like from you that if want to print on the particular printe not to the default printer by passing the printer name along with the file name then what we have to do,

Thanks for support and help in advance.
for_yanCommented:

Look here for an example on how to communicate printer name:

http://www.daniweb.com/software-development/java/threads/75474



PrintService[] services3 = PrintServiceLookup.lookupPrintServices(null, null);
AttributeSet aset3 = new HashAttributeSet();
aset3.add(new PrinterName("my printer name", null));
aset3.add(MediaTray.BOTTOM);
services3 = PrintServiceLookup.lookupPrintServices(null, aset3);
PrinterJob pjob = PrinterJob.getPrinterJob();

services3.createPrintJob();



pjob.print();


for_yanCommented:
So, I guess in your case it should be siomethuing like this:

public static void print(String filePath, String printerName){

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

aset.add(new PrinterName(printerName, null));

/* locate a print service that can handle it */

PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

/* create a print job for the chosen service */

int printnbr = 0;
DocPrintJob pj = pservices[printnbr].createPrintJob();

try {
    
FileInputStream fis = new FileInputStream(filePath);
Doc doc = new SimpleDoc(fis, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(pj);

/* print the doc as specified */
pj.print(doc, aset);

}
catch (Exception ex){

ex.printStackTrace();
}
}

Open in new window

CEHJCommented:
>>One more solution I like from you that if want to print on the particular printe not to the default printer by passing the printer name along with the file name

Make the method i gave you take TWO arguments, one for the file path and one for the printer name (call it 'printerName'). Use the following class to add a PrinterName attribute to the set of attributes you create

http://docs.oracle.com/javase/6/docs/api/javax/print/attribute/standard/PrinterName.html

e.g.
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterName(printerName, null));

Open in new window

irsbenzAuthor Commented:
Hi CEHJ,
After i modified as per your instruction but there is no printing take place. The  updated code is shown below.

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class printtxt{


//public static void main(String[] args) {
 public static void print(String path,String printername)

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

//PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterName(printerName, null)); 


/* locate a print service that can handle it */

PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

/* create a print job for the chosen service */

int printnbr = 0;
DocPrintJob pj = pservices[printnbr].createPrintJob();

try {
    
FileInputStream fis = new FileInputStream(path);
Doc doc = new SimpleDoc(fis, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(pj);

/* print the doc as specified */
pj.print(doc, aset);

}
catch (Exception ex){

ex.printStackTrace();
}
}
} 

Open in new window

for_yanCommented:
Java is case senstivie:

>public static void print(String path,String printername)


>aset.add(new PrinterName(printerName, null));



printername  is not the same as printerName
for_yanCommented:
It should be like that:
(it should not even have compiled with wrong case)
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class printtxt{


//public static void main(String[] args) {
 public static void print(String path,String printername)

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

//PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterName(printername, null)); 


/* locate a print service that can handle it */

PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

/* create a print job for the chosen service */

int printnbr = 0;
DocPrintJob pj = pservices[printnbr].createPrintJob();

try {
    
FileInputStream fis = new FileInputStream(path);
Doc doc = new SimpleDoc(fis, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(pj);

/* print the doc as specified */
pj.print(doc, aset);

}
catch (Exception ex){

ex.printStackTrace();
}
}
} 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Make sure you're using the right name for the printer - you'll probably need to do a sample iteration of them to find out their format
irsbenzAuthor Commented:
Thanks for the reply, but still I can't able to able the print from the printer. I am passing the arg0 c:\test.txt and arg1  \\T5565-555HJ\MN-456-01-FN-GCT-HP400-PS.
Please show me where I am doing worng the code is given below.
 
 import javax.print.DocFlavor;
  import java.io.ByteArrayInputStream;
  import java.io.FileInputStream;
  import javax.print.Doc;
  import javax.print.DocFlavor;
  import javax.print.DocPrintJob;
  import javax.print.PrintService;
  import javax.print.PrintServiceLookup;
  import javax.print.SimpleDoc;
  import javax.print.attribute.HashPrintRequestAttributeSet;
  import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PrinterName;
import javax.print.event.PrintJobAdapter;
  import javax.print.event.PrintJobEvent;

public class Class1{
public void print(String path,String printername){

  DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

  //PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  aset.add(new PrinterName(printername, null)); 


  /* locate a print service that can handle it */

  PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

  /* create a print job for the chosen service */

  int printnbr = 0;
  DocPrintJob pj = pservices[printnbr].createPrintJob();

  try {
      
  FileInputStream fis = new FileInputStream(path);
  Doc doc = new SimpleDoc(fis, flavor, null);
  PrintJobWatcher pjDone = new PrintJobWatcher(pj);

  /* print the doc as specified */
  pj.print(doc, aset);

  }
  catch (Exception ex){

  ex.printStackTrace();
  }
  }
  }  

Open in new window

for_yanCommented:
What is the error message?
CEHJCommented:
>>I am passing the arg0 c:\test.txt

Does that exist on the server?

Does the name you're using come up when you iterate the available names as i suggested?
irsbenzAuthor Commented:
yes that file are on the server.
Yes it's come up.
for_yanCommented:
Please, explain what it is you observe when it is not printing - just waiting ? Or showing any error?
Did you try to priont to the same printer from the same host manually ?
irsbenzAuthor Commented:
Yes I can able to print it know.
for_yanCommented:
can you explain what happens when you use the above method?
does it report any error?
irsbenzAuthor Commented:
that time there is no error and also it does not show any error. but later when I reconnect the printer and try its print.
thanks all of you for support.
irsbenzAuthor Commented:
Hi Guru's,
I am facing two more problem in printing text file first one is fonts size is not correct it's prints the own font size, style and secound is problem is it's not printing the line longer then 80 character.
for_yanCommented:
irsbenzAuthor Commented:
But I want to print the default fonts which is there in the document.
for_yanCommented:
But your document is text.txt file, correct?

This file does not specify any fonts.
CEHJCommented:
>>But I want to print the default fonts which is there in the document.

A text file cannot contain any style information, and that includes the specification of fonts
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.