In java, how does one call the linux if command? -- Linux general technology-Linux technology and application information. For details, refer to the following section. In linux, I wrote a shell script moveDirectory. sh with the code:
If [-d $ {moveDirectory}/$ {batchDate}]; then
Mv-f $ {batchNumDir}/* $ {moveDirectory}/$ {batchDate }/
Echo "aa"
Else
Echo "bb"
Mv-f $ {batchNumDir }$ {moveDirectory}
Fi
This code can be executed normally on the linux terminal. However, if I call this shell script in a java program, an error occurs. The called code is:
Process proc = runtime.getruntime(cmd.exe c ("moveDirectory. sh ");
String line;
BufferedReader br = new BufferedReader (new InputStreamReader (proc. getInputStream ()));
// Record the output stream
While (line = br. readLine ())! = Null ){
Logger. debug (line );
}
// Wait until the process ends
Proc. waitFor ();
// Print the error message when the process ends abnormally.
If (proc. exitValue ()! = 0 ){
Br = new BufferedReader (new InputStreamReader (proc. getErrorStream ()));
While (line = br. readLine ())! = Null ){
Logger. error (line );
}
}
The ERROR message displayed is [ERROR-10: 37: 21] ShellTask. shell script execution failed (140)-shell script execution failed
Java. io. IOException: Cannot run program "if": java. io. IOException: error = 2, No such file or directory
At java. lang. ProcessBuilder. start (ProcessBuilder. java: 459)
At java.lang.Runtime.exe c (Runtime. java: 593)
At java.lang.Runtime.exe c (Runtime. java: 466)
At com.huateng.topafs.frame.ShellTask.exe cCmds (ShellTask. java: 120)
At com.huateng.topafs.frame.ShellTask.exe cProcess (ShellTask. java: 103)
At com. huateng. topafs. frame. ShellTask. run (ShellTask. java: 79)
At com. huateng. topafs. frame. BatchRunner. runTask (BatchRunner. java: 201)
At com. huateng. topafs. frame. BatchRunner. run (BatchRunner. java: 55)
At com. huateng. topafs. frame. BatchRunner. main (BatchRunner. java: 257)
Caused by: java. io. IOException: java. io. IOException: error = 2, No such file or directory
At java. lang. UNIXProcess. (UNIXProcess. java: 148)
At java. lang. ProcessImpl. start (ProcessImpl. java: 65)
At java. lang. ProcessBuilder. start (ProcessBuilder. java: 452)
... 8 more:
After querying related error information on the Internet, we can conclude that java can call linux external program commands, such as cp and mv, but cannot call linux system commands, such as cd, if, and passwd.
On the linux terminal, I used the which command to query the path of the cp and cd commands. The result is:
[Devafs:/home/devafs/batch/sbin] which cp
/Bin/cp
[Devafs:/home/devafs/batch/sbin] which cd
/Usr/bin/which: no cd in (/usr/java/jdk1.6.0 _ 14/bin:/usr/java/jdk1.6.0 _ 14/jre/bin: /usr/kerberos/bin:/usr/local/bin:/usr/bin:/home/devafs/bin:/oracle/product/11.1.0/db_1/bin: /home/devafs/bin:/home/afsadm/sqllib/adm:/home/afsadm/sqllib/misc: /home/afsadm/sqllib/lib:/home/afsadm/sqllib/bin:/home/devafs/apache-ant-1.7.1/bin: /home/devafs/apache-maven-2.2.1/bin :.)
As I have just been in touch with the linux system and cannot solve this problem, I can only locate the error here, so please give me some advice. Thank you very much!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service