I tried to send a file with print data (data from a Zebra printer) from the Linux machine to a shared printer on the Windows machine, but it did not work, I tried everything. My last idea was to first try to work by command line on the Linux machine and then do the same solution in Java, and the result was: it works by command line but not in Java.
My command line solution on Linux has:
smbclient \\\\host\\printer_share -U 'domain/user%pass' -c "put file_name"
The solution with smbclient works perfectly, so I thought about using jCIFS in Java, but it does not work in the printer. In a shared folder of the same host it works, but in the printer share no, however by command line with smbclient works both work. Anyone have any ideas where I'm going wrong?
My java code:
public static void sendFileToPrinter(String commandsToPrinter) {
String user = "user";
String pass = "pass";
String domain = "domain";
String path = "smb://host/printer_share/file_to_print";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain, user, pass);
SmbFile smbFile = new SmbFile(path, auth);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
smbfos.write(commandsToPrinter.getBytes());
System.out.println("Work");
}
Java error:
Indifferently of the operational system i was able to solve the problem with the help of @HieryNomus, who has one library perfect to implement SMB. Git link: https://github.com/hierynomus/smbj/
For my need, I achieved through the following implementation (this is only my test code):
public static void sendCommandToZebraPrinter(String command) throws MalformedURLException, SmbException, IOException {
String username = "username";
String password = "password";
String domain = "mydomain";
String sharedDirectory = "PRINTER_SHARE";
String computerName = "MYCOMPUTER";
SMBClient client = new SMBClient();
try (Connection connection = client.connect(computerName)) {
AuthenticationContext ac = new AuthenticationContext(username, password.toCharArray(), domain);
Session session = connection.authenticate(ac);
try (PrinterShare share = (PrinterShare) session.connectShare(sharedDirectory)) {
InputStream stream = new ByteArrayInputStream(command.concat("\n").getBytes(StandardCharsets.UTF_8));
share.print(stream);
}
}
}
Command variable is the EPL command to Zebra printer (GC420t), such as:
I8,A,001
Q104,024
q863
rN
S2
D11
ZT
JF
OD
R172,0
f100
N
75,33,D,h3,"1"
b363,39,D,h2,"TEST"
b198,33,D,h3,"TEST"
LO154,4,1,73
LO280,4,1,73
A149,27,2,2,1,1,N,"1"
A272,26,2,3,1,1,N,"TEST"
A425,26,2,3,1,1,N,"TEST"
P1
IF THE COMMAND DOES NOT WORK: Add \n
at the end of the command.