Help on running lp command from java
Hi
How should i be able to print a String from my java program to the printer in Unix Platform.
I am able to print a file using the follwing lp command
lp -d <printername> <filename>.
what should be the exact syntax to print a string ,I am using the following command but it doesnot work
lp -d <printername> - <string>
In my application I create a temporary file ,then print the file and delete the file as it contains some imp data,but this approach will not work always because if the file is deleted before the printing is completed then it won't be printed.So what should be a good approach for the same??.
Regards
Ravindra
Hi
How should i be able to print a String from my java
program to the printer in Unix Platform.
I am able to print a file using the follwing lp
command
lp -d <printername> <filename>.
what should be the exact syntax to print a string ,I
am using the following command but it doesnot work
lp -d <printername> - <string>
The right syntax for Unix is: echo "<string>" |lp -d <printername>
Regards.
Similar Messages
-
Running ls command from Java stroed procedure no output
Hi ,
I am trying to run ls command from java stored procedure in oracle
Process p = Runtime.getRuntime().exec("ls");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("output of the command run:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
from java stored procedure in oracle.
i get output of println statments but it does not go into while loop to print from stdInput.
Result of running Java stored procedure is -
output of the command run:
Call completed.
when i run the program on client side it works fine.
Has anybody tried this from java stroed procedure.
Thanks,
JagJag,
Actually, the question of whether it works for me seems to depend on the version of the OS (or Oracle). On RedHat Linux (Oracle 8.1.6) it didn't work at all, but on Solaris (Oracle 9.0.2) it did. Here's the output from that run:
SQL> /
output of the command run:
init.ora
initDBPart9i.DBPSun01.ora
initdw.ora
lkDBPART9I
orapw
orapwDBPart9i
spfileDBPart9i.ora
Done
PL/SQL procedure successfully completed.
But, I did need to change a line of your code to this:
Process p = Runtime.getRuntime().exec("/usr/bin/ls");
your original was:
Process p = Runtime.getRuntime().exec("ls");
You might consider, if possible, use of some of the Java File classes instead of ls, as this might make things more predictable for you. There were some examples in oramag.com a few months ago, but they were pretty simple (you might not need them).
Hope this helps,
-Dan
http://www.compuware.com/products/devpartner/db/oracle_debug.htm
Debug PL/SQL and Java in the Oracle Database -
Running ssh command from java and then answering password prompt
Hi,
I have a situation that has not solved yet. I am running ssh command from unix terminal without any problem, and then i enter password.
For example :
[oracle@fuata]:/export/home/oracle> ssh -N [email protected] -L 9901:127.0.0.1:9999
Password:
It is working. I have question that how can i perform this in java? I am thinking that i can run ssh command by using Runtime Class, it is ok. But how can i answer the password? I am a bit confused. Is there any example looks like this?
Thanks for responses.futi wrote:
Thanx. Firstly i insisted to do this without jsch but actually this is harder than jsch. I edit some of code pieces PortForwardingL.java and could run it. It works problem-free. Could you say why you "insisted" on this approach. It can't be for speed+ since jsch is very fast. It can't be for portability+ since jsch is portable but the use of Runtime.exec() requires the installation of ssh software. It can't be because of limitations+ since jsch is a fully featured library. It can't be for security+ since jsch is secure. It can't be for ease of use+ since jsch is much easier to use than ssh with Runtime.exec(). Unless it's a licensing issue, it can't be for commercial+ reasons since jsch is free. The only reason I can think of why one would "insisted" on this approach is if it is for some college project. -
How to run system commands from JAVA
Hi Friends,
How to run windows system commands from JAVA
Runtime r=Runtime.getRuntime();
r.exec("dir");
Throwing following Exception
CreateProcess :dir error=2
Thanks in advance
HamsaHi ,
in Windows NT this is not possible, you can use the following :
Runtime r=Runtime.getRuntime();
StringBuffer sbuf = new StringBuffer();
String dir = new String();
java.lang.Process proc = r.exec("cmd /c dir");
InputStream is = proc.getInputStream();
int ch ;
while((ch=is.read() ) != -1)
sbuf.append((char)ch);
is.close();
dir = sbuf.toString();
System.out.println(dir ); -
Problem while running dos command from java program
Dear friends,
I need to terminate a running jar file from my java program which is running in the windows os.
For that i have an dos command to find process id of java program and kill by using tskill command.
Command to find process id is,
wmic /output:ProcessList.txt process where "name='java.exe'" get commandline,processid
This command gives the ProcessList.txt file and it contains the processid. I have to read this file to find the processid.
when i execute this command in dos prompt, it gives the processid in the ProcessList.txt file. But when i execute the same command in java program it keeps running mode only.
Code to run this command is,
public class KillProcess {
public static void main(String args[]) {
KillProcess kProcess = new KillProcess();
kProcess.getRunningProcess();
kProcess = new KillProcess();
kProcess.readProcessFile();
public void getRunningProcess() {
String cmd = "wmic /output:ProcessList.txt process where \"name='java.exe'\" get commandline,processid";
try {
Runtime run = Runtime.getRuntime();
Process process = run.exec(cmd);
int i = process.waitFor();
String s = null;
if(i==0) {
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println("--> "+s);
} else {
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
while ((s = stdError.readLine()) != null) {
System.out.println("====> "+ s);
System.out.println("Running process End....");
} catch(Exception e) {
e.printStackTrace();
public String readProcessFile() {
System.out.println("Read Process File...");
File file = null;
FileInputStream fis = null;
BufferedReader br = null;
String pixieLoc = "";
try {
file = new File("ProcessList.txt");
if (file.exists() && file.length() > 0) {
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis, "UTF-16"));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
} else {
System.out.println("No such file");
} catch (Exception e) {
e.printStackTrace();
return pixieLoc;
} when i remove the process.waitFor(), then while reading the ProcessList.txt file, it says "No such file".
if i give process.waitFor(), then it's in running mode and program is not completed.
Colud anyone please tell me how to handle this situation?
or Is there anyother way to kill the one running process in windows from java program?
Thanks in advance,
SathishHi masijade,
The modified code is,
class StreamGobbler extends Thread
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
this.is = is;
this.type = type;
public void run()
try
InputStreamReader isr = new InputStreamReader(is, "UTF-16");
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
ioe.printStackTrace();
public class GoodWindowsExec
public static void main(String args[])
try
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows 95" ) )
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
} else {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
System.out.println("Executing.......");
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
t.printStackTrace();
}when i execute the above code, i got output as,
Execing cmd.exe /C wmic process where "name='java.exe'" get commandline,processid
and keeps in running mode only.
If i execute the same command in dos prompt,
CommandLine
ProcessId
java -classpath ./../lib/StartApp.jar;./../lib; com.abc.middle.startapp.StartAPP 2468
If i modify the command as,
cmd.exe /C wmic process where "name='java.exe'" get commandline,processid > 123.txt
and keeps in running mode only.
If i open the file when program in running mode, no contents in that file.
If i terminte the program and if i open the file, then i find the processid in that file.
Can you help me to solve this issue? -
Hi ,
I would appreciate if anyone could tell me how to run DOS command such as "del" using JAVA language .Thank you.<steps onto soapbox>
Surely for something like 'del' we should be advocating a non-OS specific method so we don't lose sight of Java's cross platform abilities.
If it has to run an OS specific thing fine, but please look for a non OS specific solution first.
<steps off soapbox> -
Running a command from java code
hi all,
There is a command
"java -jar selenium-server.jar -interactive"
which i am running through command prompt after going to D:\MyFolder\Examples .
i want to execute this command using java code .please helpsubratjyetki wrote:
please answer in detail or if posible can u give the code for thisOnce more -
The detail is given in the reference. Why don't you read it?
I could give you the code but it will cost you �100 per hour. -
May I ask how I should write if I want to execute a dos command in java? The command is :
"C:\Program Files\Real\realproducer\rmeditor -i abc.rm -d abc.txt". Should I use array?
Thanks!why an array ?
String command = "C:\\Program Files\\Real\\realproducer\\rmeditor -i abc.rm -d abc.txt";
Process p = Runtime.getRuntime().exec( command );what about the seach function on the left side ? ;-)
tobias -
Running shell command from Java, but from Windows Schedule Task?
I have a simple java program that will start a dos based program. The java program works great when I run it, but when I run it from the Windows Task Scheduler, the dox box that should pop up, does not. It is running because I can see it in the task manager but I can not see the command window.
Any ideas how to show the command window from a java application that is ran from Windows Task Scheduler?
Example program below. Runs fine by itself, should get a dos popup that issues the directory command, run it from the scheduler and it does not show the dos box. You can however see it in the Processes as CMD.exe.
import java.io.IOException;
public class Test {
* @param args
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd /k start dir");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}Now the my real program is quite a bit more complicated. It actually goes out and checks the status of an Oracle Database, if all the tablespaces are online, then the middle tier application is allowed to start. The middle tier application is started by my java application, and is comes up with a dos console which shows status and users connected to the database.Jag,
Actually, the question of whether it works for me seems to depend on the version of the OS (or Oracle). On RedHat Linux (Oracle 8.1.6) it didn't work at all, but on Solaris (Oracle 9.0.2) it did. Here's the output from that run:
SQL> /
output of the command run:
init.ora
initDBPart9i.DBPSun01.ora
initdw.ora
lkDBPART9I
orapw
orapwDBPart9i
spfileDBPart9i.ora
Done
PL/SQL procedure successfully completed.
But, I did need to change a line of your code to this:
Process p = Runtime.getRuntime().exec("/usr/bin/ls");
your original was:
Process p = Runtime.getRuntime().exec("ls");
You might consider, if possible, use of some of the Java File classes instead of ls, as this might make things more predictable for you. There were some examples in oramag.com a few months ago, but they were pretty simple (you might not need them).
Hope this helps,
-Dan
http://www.compuware.com/products/devpartner/db/oracle_debug.htm
Debug PL/SQL and Java in the Oracle Database -
Running UNIX command from Java
import java.lang.* ;
import java.io.* ;
public class TestRunTime
public static void main(String args[])
int rc = -1 ;
String yard = "psnsy" ;
String ifwList = "[email protected],[email protected]" ;
String cmd = "/usr/bin/mailx -r oracle -s \"PMC - Missing Interface Files from " + yard +
"\" " + ifwList + " < /interface/nwps/missingfiles.txt" ;
rc = RunThis(cmd) ;
System.out.println(rc) ;
private static int RunThis(String str)
Runtime rt = Runtime.getRuntime();
int rc = -1;
try
Process p = rt.exec(str);
p.waitFor() ;
BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = buf.readLine()) != null)
System.out.println(line) ;
rc = 0 ;
return rc ;
catch ( Throwable t )
t.printStackTrace();
rc = -1 ;
throw new RuntimeException() ;
}When I run java TestRunTime
all it does is hangs and never completes.
I can run the string cmd from the UNIX shell and it runs as expected - I receive an email.jschell wrote:
sabre150 wrote:
Whether the detail is as you say or as I say does not remove the need to process the exec()ed processes stdout and stderr each in their own thread. Since the OP is not writing to stdin he can handle one of stdout or stderr in the Thread that invokes the exec() but the other needs a separate thread.If the streams are stripped from the process then...
1. They should not be stripped until they are in their own thread.
2. Each requires their own thread.
But since the OP isn't stripping either, no other threads are needed. Nor does the OP need to strip them.I have to disagree. The following code is based on the traps article and sends output to stdout and to stderr from the 'sh' program. Run as is it deadlocks. Run by changing the 'false' to 'true' in the 'if' statement it does not deadlock.
If one changes the code to process only stdout or stderr but not both then it deadlocks.
Running the same code on Windows XP and Windows 2000 but using 'cmd.exe' instead of 'sh' and using 'dir' instead of 'ls' produces the same result.
Running similar code that just runs a perl script without any stdin but that writes to both stdout and stderr it deadlocks if one does not process both stdout and stderr in separate threads.
If one processes the Process stdout and stderr streams then one does not get a deadlock.
This is entirely consistent with what the 'traps' article says and I hope consistent with what I have written in this thread.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
class StreamGobbler extends Thread
private int count = 0;
private final InputStream is;
private final String type;
StreamGobbler(InputStream is, String type)
this.is = is;
this.type = type;
public void run()
try
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(++count + "\t " + type + "> " + line);
} catch (IOException ioe)
ioe.printStackTrace();
public class Sabre20091015_2
public static void main(String args[]) throws Exception
final Process proc = Runtime.getRuntime().exec("sh");
if (false)
new StreamGobbler(proc.getErrorStream(), "ERROR").start();
new StreamGobbler(proc.getInputStream(), "OUTPUT").start();
final Writer writer = new OutputStreamWriter(proc.getOutputStream());
for (int commandIndex = 0; commandIndex < 20000; commandIndex++)
writer.write("echo Index " + commandIndex + "\n");
writer.write("ls\n");
writer.flush();
writer.write("fnaskfdkdhflakjfljd\n");
writer.flush();
writer.close();
final int exitValue = proc.waitFor();
System.out.println("Exit value = " + exitValue);
} -
Unable to execute Linux command from Java
Hi,
I am currently working on a code wherein i need to execute Linux command from Java. Below are some of the query i have.
1) Is there any efficient method of running OS commands from Java, rather than using Runtime and Process method.
2) Below is details of my code which fails in execution
**-- Java Version**
java version "1.6.0"
OpenJDK Runtime Environment (build 1.6.0-b09)
OpenJDK Server VM (build 1.6.0-b09, mixed mode)
-- Program Code ----
Where <path> = Path i put myself
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.*;
public class GetInode{
* @param args
public static void main(String[] args) {
GetInode test = new GetInode();
test.getInode();
public void getInode(){
String command = "/usr/bin/stat -Lt <path>;
System.out.println(command);
Process process;
Runtime rt;
try{
rt = Runtime.getRuntime();
process = rt.exec(command);
InputStreamReader isr = new InputStreamReader(process.getErrorStream());
BufferedReader bre = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream());
System.out.println(bre.readLine());
System.out.println(br.readLine().split(" ")[7]);
process.destroy();
}catch (Exception ex){
System.out.println("Error :- " + ex.getMessage());
------Output -------------
/usr/bin/stat -Lt "<path>"
/usr/bin/stat: cannot stat `"<path>"': No such file or directory
Error :- null
Can any one help me what is wrong and why i am unable to run the Linux command from Java.For clarity purpose............i m submitting actual code here
--- Code ---
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.*;
public class GetInode{
* @param args
public static void main(String[] args) {
GetInode test = new GetInode();
test.getInode();
public void getInode(){
String command = "/usr/bin/stat -Lt \"/afs/inf.ed.ac.uk/user/s08/s0898671/workspace/CASWESBLIN/TestFS/01_FIL_01.txt.txt\"";
System.out.println(command);
Process process;
Runtime rt;
try{
rt = Runtime.getRuntime();
process = rt.exec(command);
InputStreamReader isr = new InputStreamReader(process.getErrorStream());
BufferedReader bre = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println(bre.readLine());
System.out.println(br.readLine().split(" ")[7]);
process.destroy();
}catch (Exception ex){
System.out.println("Error :- " + ex.getMessage());
--- Output ---
[ratz]s0898671: java GetInode
/usr/bin/stat -Lt "/afs/inf.ed.ac.uk/user/s08/s0898671/workspace/CASWESBLIN/TestFS/01_FIL_01.txt.txt"
/usr/bin/stat: cannot stat `"/afs/inf.ed.ac.uk/user/s08/s0898671/workspace/CASWESBLIN/TestFS/01_FIL_01.txt.txt"': No such file or directory
Error :- null
-- Linux Terminal --
If i copy the first line from the output and execute on Linux terminal her is the output that i get
[ratz]s0898671: /usr/bin/stat -Lt "/afs/inf.ed.ac.uk/user/s08/s0898671/workspace/CASWESBLIN/TestFS/01_FIL_01.txt.txt"
/afs/inf.ed.ac.uk/user/s08/s0898671/workspace/CASWESBLIN/TestFS/01_FIL_01.txt.txt 12003 24 81a4 453166 10000 1c 360466554 2 0 1 1246638450 1246638450 1246638450 4096
Can you just assist me where am i really making mistake.......i was wondering if the command that i pass from Java....can be executed on Linux terminal why is it faling to run from java.........and when i print the Error Stream for process output........it show cannot Stat....... -
hi all
how do i run dos commands from java??????????Using Runtime#exec().
Also see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html -
I want run some command using java
hi all,
i want run command using java code.
from cell prompt when i run this command 'mysqldump test > /home/DBNAME.sql'
it will create DBNAME.sql file.
but i want to run this command using java code
i tried the following code but it did not work.
is any other way is their?
try {
Runtime.getRuntime().exec("setxkbmap nudi");
} catch(IOException ioe) {
ioe.printStackTrace();
thanks in advance
dayahello,
thanks for replay
i am sorry the above code should be like this.(the above code is working fine)
public class ExportTest{
public ExportTest(){
try {
Runtime.getRuntime().exec("mysqldump test > '/root/DBNAME.sql'");
} catch(IOException ioe) {
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
public static void main(String args[]){
new ExportTest();
}when run above class, it not creating DBNAME.sql file.
when run in command prompt it creating DBNAME.sql
($ mysqldump test > /root/DBNAME.sql)
but i want to run this command from java code, that's way tried to do using above
code, but it won't create DBNAME.sql
is it wrong what i am doing? or any other way?
thanks inadvace
daya -
Running OpenVMS DCL command from Java
Hi,
I want to execute an Alpha OpenVMS 7.2-2 command from Java. I use JDK 1.1.8-1.
I have to place the DCL command in an OpenVMS .COM file because passing a DCL
image like DIR doesn't work using Runtime.exec().
When I ran the Java program, I get the following error:
java.io.IOException: Child creation error: exec format error
at java.lang.UNIXProcess.<init>(Compiled Code)
at java.lang.Runtime.exec(Compiled Code)
at java.lang.Runtime.exec(Compiled Code)
at oscmd.main(Compiled Code)
here is my Java program:
import java.io.*;
public class oscmd {
public static void main(String[] args) throws Exception {
Process child = Runtime.getRuntime().exec(
"dra1:[oracle8.db_adamp.backup]RUNCMD.COM");
child.waitFor();
child.exitValue();
child.destroy();
Thanks for your help.Hi,
Did you get your exec format error problem?
I'm having exactly the same problem and would greatly appreciate any help you could offer.
Thanks
Tim -
Running a SQL*Plus command from Java
I have a requirement to run SQL* Plus commands from Java. The results as they would appear in SQL*Plus window or spool file should be captured and stored in a table.
For example,
SQL> select * from dual;
D
X
1 row selected;
SQL>
All the lines above should be stored for later use.
Could any one give pointers?
Thanks,
Ravi<p>
Hi,
</p>
<p>
<strong><font face="Courier New">
public class RuntimeExecApp {
public static void main(String args[]) throws IOException
Runtime r = Runtime.getRuntime();
r.exec("C:\\Oracle\\sqlplus.exe");
}</font></strong>
</p>
<p>
Kuba
</p>
Message was edited by:
KUBA
Maybe you are looking for
-
How do I download and print a pdf file in osx mountain lion
How do I download and print a pdf file?
-
Importing more than one at a time
when I try to use the option to import files that are on my computer already it only allows me to do one at a time I have tried both a shift and a control key to try to get more than one file at a time and it does not work so from what I have read i
-
Any Ideas for speeding up Macbook Pro?
My Mac has been acting really slow lately. I have had to get it wiped once already because it was stuck on the grey screen at boot. I know now that I can fix this with safe mode, but I have also been having some big issues using the mac itself. I occ
-
When I try to start iTunes, I get the message "program can't start because MSVCR80.dll is missing from your computper. Try reinstalling the program." When I try reinstalling, I get the message Error 7 (Windows Error 126) Can you help me?????
-
I am using OWB to read data from a bunch of oracle tables and putting the results in a flat file. I've defined is as a tab delimited file and everything works fine except for one thing. What I need is to include double quotes for only those fields th