PDA

View Full Version : [Java] execute cat in java code for print



giuseppe_84
August 26th, 2010, 11:17 AM
Hello all

I apologize for my English, I would run the command

cat / tmp / test.txt > / dev/lp0 in Java code
I have tried in various ways:


Process p = Runtime.getRuntime().exec("cat /tmp/test.txt > /dev/lp0");
p.destroy();

but nothing happens

or


String[] arr = new String[5];
arr[0] = "/bin/bash ";
arr[1] = "cat ";
arr[2]= "/tmp/test.txt ";
arr[3] = " > ";
arr[4]= "/dev/lp0";
Process p = Runtime.getRuntime().exec(arr);
or


String[] arr = new String[4];
arr[0] = "cat ";
arr[1]= "/tmp/test.txt ";
arr[2] = " > ";
arr[3]= "/dev/lp0";
Process p = Runtime.getRuntime().exec(arr);

with the last two attempts gave me this kind of Exception


java.io.IOException: Cannot run program "/bin/bash ": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java :460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:466)
at provacomm.Main.main(Main.java:40)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java :453)
... 3 more
can someone help me thanks

Zugzwang
August 26th, 2010, 11:25 AM
[/COLOR]I have tried in various ways:


Process p = Runtime.getRuntime().exec("cat /tmp/test.txt > /dev/lp0");
p.destroy();


Huh? You are starting the process and immediately destroy it (which is likely, but not guaranteed to happen before it has finished)? Don't you rather want to wait until it has finished first? Call "waitFor()" instead of "destroy()".

At a side note, it probably makes more sense to open "/dev/lp0" for writing using a "BufferedOutputStream" or something like that to write your stuff there and simply forget about doing it in a subprocess. If you want the whole thing to be portable, you might want to look into AWT/Swing's printing functionalities.

Some Penguin
August 26th, 2010, 11:34 AM
1. Don't append extra spaces. Read the exception again -- you don't actually HAVE a command named "/bin/bash ", do you? You have "/bin/bash".

2. Pipes. Get the output stream from the process and redirect it. ">" is not an argument.

3. Don't use two extra processes -- 'bash' and 'cat' -- when Java is perfectly capable of opening files itself.

giuseppe_84
August 26th, 2010, 11:35 AM
Huh? You are starting the process and immediately destroy it (which is likely, but not guaranteed to happen before it has finished)? Don't you rather want to wait until it has finished first? Call "waitFor()" instead of "destroy()".

At a side note, it probably makes more sense to open "/dev/lp0" for writing using a "BufferedOutputStream" or something like that to write your stuff there and simply forget about doing it in a subprocess. If you want the whole thing to be portable, you might want to look into AWT/Swing's printing functionalities.

I put p.destroy () because if after launching the application I tried to print again from the terminal told me "device is busy", but if I delete it still does not work, I tried to open the door and write but I wanted to know if it was possible to directly execute the command

giuseppe_84
August 26th, 2010, 11:41 AM
1. Don't append extra spaces. Read the exception again -- you don't actually HAVE a command named "/bin/bash ", do you? You have "/bin/bash".

2. Pipes. Get the output stream from the process and redirect it. ">" is not an argument.

3. Don't use two extra processes -- 'bash' and 'cat' -- when Java is perfectly capable of opening files itself.


I tried this way



String[] arr = new String[3];
arr[0] = "cat";
arr[1]= "/tmp/test.txt";
arr[2]= "/dev/lp0";
Process p = Runtime.getRuntime().exec(arr);

but does not work

giuseppe_84
August 26th, 2010, 11:50 AM
Pipes. Get the output stream from the process and redirect it. ">" is not an argument.



you can show me how please

Zugzwang
August 26th, 2010, 04:17 PM
I put p.destroy () because if after launching the application I tried to print again from the terminal told me "device is busy", but if I delete it still does not work, I tried to open the door and write but I wanted to know if it was possible to directly execute the command

Ok, so then first wait for the task to finish and *then* destroy it. By the way: Why don't you let the "lpr" command do the printing if you really want to do it the hard way?

And with respect to input/output streaming of processes in Java, have a look at the following example: http://www.java2s.com/Code/JavaAPI/java.lang/ProcessgetOutputStream.htm

And by the way. The following code:


String[] arr = new String[3];
arr[0] = "cat";
arr[1]= "/tmp/test.txt";
arr[2]= "/dev/lp0";
Process p = Runtime.getRuntime().exec(arr);



does not work because "cat" is not a program but a shell command. So you will need to invoke the shell to run the "cat" command. But as already stated in this post, this is not the right way to solve your problem. We do understand that you rather like to stick to "solutions" which you understand, but I must say that calling "cat" to write to "/dev/lpr" using the Java process interface reminds me of this comic: http://www.xkcd.com/763/

giuseppe_84
August 26th, 2010, 04:41 PM
Ok, so then first wait for the task to finish and *then* destroy it. By the way: Why don't you let the "lpr" command do the printing if you really want to do it the hard way?

And with respect to input/output streaming of processes in Java, have a look at the following example: http://www.java2s.com/Code/JavaAPI/java.lang/ProcessgetOutputStream.htm

And by the way. The following code:


String[] arr = new String[3];
arr[0] = "cat";
arr[1]= "/tmp/test.txt";
arr[2]= "/dev/lp0";
Process p = Runtime.getRuntime().exec(arr);

does not work because "cat" is not a program but a shell command. So you will need to invoke the shell to run the "cat" command. But as already stated in this post, this is not the right way to solve your problem. We do understand that you rather like to stick to "solutions" which you understand, but I must say that calling "cat" to write to "/dev/lpr" using the Java process interface reminds me of this comic: http://www.xkcd.com/763/

I have to print documents with special printer, I opened the door / dev/lp0 write, and I could write about it, but some printers give me problems, what do you advise me to do and if you can show me some examples, I thought about using the command 'cat' in the code because that way I could always print

spjackson
August 26th, 2010, 05:57 PM
String[] arr = new String[5];
arr[0] = "/bin/bash ";
arr[1] = "cat ";
arr[2]= "/tmp/test.txt ";
arr[3] = " > ";
arr[4]= "/dev/lp0";
Process p = Runtime.getRuntime().exec(arr);
If you want to go down the road of calling 'cat' in a separate process, I think that the following should work for you.



String[] arr = new String[5];
arr[0] = "/bin/bash"; // N.B. No trailing space
arr[1] = "-c";
arr[2]= "cat /tmp/test.txt > /dev/lp0";
Process p = Runtime.getRuntime().exec(arr);