Is there a way of sending directly an "ESC" coded string to an Epson Matrix Printer connected on a USB using Java. I've tried this one using the LPT1 port successfully. However, majority of our clients are now using USB cables instead of the old parellel cables. The main problem I have is how to open the particulary USB where the printer is connected as an outputstream in Java. Thanks in advance for any tips and suggesions.
3 Answers
This article on JSR-80 might help you. Looks like it's not trivial at all and OS dependent...

- 113,398
- 19
- 180
- 268
Since you say LPT1, I assume this is for Windows.
I don't see any 100% Java solution to this, you want to deal with hardware
JNI
You could use JNI to call some windows DLL written in a native language (C/C++/Delphi) that exports one function SendData. You could prepare the data in a temp file (random filename), and the DLL would send it to the printer then delete it.
In Java, you could use load and loadlibrary to load the DLL.
Use some readymade solutions to convert it to USB/LPT
- A driver for your printer.
- NET USE Command
- DOS2USB or DOS2PRN

- 1
- 1

- 5,654
- 5
- 28
- 48
This is how it is done in Java:
PrintService pservice = ... // acquire print service of your printer
DocPrintJob job = pservice.createPrintJob();
String commands = "";
commands += "\u001B\u0045\u000A"; // plain
commands += "Hello ";
commands += "\u001B\u0045\u000D"; // bold
commands += "ESCP!";
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(commands.getBytes(), flavor, null);
job.print(doc, null);
Borrowed from: https://stackoverflow.com/a/9309845/3196753
Note on some OSs, the printer needs to be configured as a raw/generic print device. For USB printers, this often involves installing the USB driver and then adding a second printer with the same port, but a raw or generic driver.