Java – Print Zebra ZM400 using Java

javazebra-printerszpl-ii

my printer is Zebra ZM400 label printer and it's connected to one of the pc (connected with USB) in the network.

I want to send the command to the label printer from my pc via network and print label.

How to connect that printer from network and print label from java application?

I know I've to use ZPL langauage but I don't know how to make connection and send command to label printer.

Is it possible? I surfed in the google but i can't find any sample code yet.

EDIT

I used norbi771's method.. but when it sent the command, just blank come out..

my label's dimension is 3.25" x 3.75"..

This is my sample code for label .. but nothing comes out..

public class TestLabelPrinter {

    /**
     * @param args
     */
    public static void printLabel(String label, String company, String docDate)  {
        try {
            FileOutputStream os = new FileOutputStream("\\\\192.168.42.57\\zd");
            PrintStream ps = new PrintStream(os); 
            String commands = "^XA" +
                              "^LH30,30" +
                              "^F020,10^AD^FDZEBRA^FS" + 
                              "F020,60^B3^FDAAA001^FS" + 
                              "^XZ";     

            ps.println(commands);
            ps.print("\f");
            ps.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printLabel("label 12345", "Company name", "2013-05-10 12:45");
        System.out.println("Successful..");
    }

Best Solution

Maybe not the best answer, but I recently did it like that. I connected the printer to the PC with Windows. Then I shared the printer. Then this shared printer I mapped into LPT1 via simple command (all of this can be done one one PC):

net use \\pcname\sharedprinter LPT1:

Since now this LPT1 port is aka file you can write to. Now I simply write data to that file in JAVA and it is working fine. I know it is not very elegant, but works for me and lets me use one label printer shared between a few PCs

    public class EplPrint1 {
            private final String port;
            public EplPrint1(String port) {
                    this.port = port;
            }
            public void printLabel(String label, String company, String docDate) throws FileNotFoundException {
                    FileOutputStream os = new FileOutputStream(port);
                    PrintStream ps = new PrintStream(os);
                    String commands = "N\n"
                            +  "A1,1,0,1,1,1,N,\""+asciiNormalize(company)+"\"\n"
                            + "A1,20,0,1,1,1,N,\""+asciiNormalize("Entry date")+": " + docDate+"\"\n"
                            + "B1,40,0,1,3,2,80,B,\""+label+"\"\n"
                            + "P1,1\n";     

                    ps.println(commands);
                    ps.print("\f");
                    ps.close();
            }

            public static void main(String[] argv) throws FileNotFoundException {
                    //EplPrint1 p = new EplPrint1("d:\\tmp\\eplcommands.txt");
                    EplPrint1 p = new EplPrint1("LPT1");
                    //p.printLabel("23535.A.33.B.233445");  
                    p.printLabel("label 12345", "Company name", "2013-05-10 12:45");
            }
    }

The example provided is for EPL printing, but ZPL should work the same way.