running unix command from java
Hi All,
I have been struggling for a week trying to run a unix command from my java program.
the unix command is = ssh dmdev3@tsapid01-zt3d01 ':> /t3/envs/dmdev3/test/file_list.txt'
when i try to run this command directly on my unix console, it works perfectly.
but when i try it form my JAVA program, i get the below mentioned error
ksh: :> /t3/envs/dmdev3/test/file_list.txt: not found
:(:(:(
below is my java code snippet that i use,
procTemp=runEnv.exec(strShellCmd);
where strShellCmd contains the entire unix command as a single string
requiring your help at the earliest cause i'm in such a desperate situation.
Thanks in advance,
Madhu.
Re: running unix command from java
Have you tried using this line of code? :
Code:
Process child = Runtime.getRuntime().exec(command);
If you do not know how to use this properly, a quick google search will provide you with a bunch of examples of how to use this.
This site had a good one as well.
Running system commands in Java applications | java exec example | devdaily.com
I would try to execute you command, but I would not want to access your ssh server.
Re: running unix command from java
Quote:
Originally Posted by
kammce
Have you tried using this line of code? :
Code:
Process child = Runtime.getRuntime().exec(command);
If you do not know how to use this properly, a quick google search will provide you with a bunch of examples of how to use this.
This site had a good one as well.
Running system commands in Java applications | java exec example | devdaily.com
I would try to execute you command, but I would not want to access your ssh server.
I found out the solution.
initially i used Code:
ProcessBuilder procBuild=new ProcessBuilder(strShellCmd);
but when i used Code:
ProcessBuilder procBuild=new ProcessBuilder("bash","-c",strShellCmd);
it worked :)
thanks for your help,
Madhu.
Re: running unix command from java
Quote:
Originally Posted by
kammce
Have you tried using this line of code? :
Code:
Process child = Runtime.getRuntime().exec(command);
I'm guessing that is the equivalent of the line the OP has given, where runEnv is the Runtime and procTemp is a Process.
For the OP all I can suggest is going through this article at JavaWorld which covers a lot of things you need to know about how Runtime exec() works (and sometimes doesn't).