简体   繁体   English

Java执行Linux命令

[英]Java executing Linux commands

I want to write a java code that executes some Linux command: 我想编写一个执行一些Linux命令的Java代码:

String cmd = "cd /home/arps/FBI" ;

Process p=Runtime.getRuntime().exec(cmd);


String [] arr = new String [9] ;
 arr[0] = "cd /home/arps/FBI" ;
 for(int n = 1 ; n < 9 ; n++){
 String command = "mv" + "  " +  "/home/arps/FBI/hr" + n + ".txt" + "    " + "/home/arps/FBI/hrs" + n +".txt" ;
 arr[n] = command ;
}


 Process pp=Runtime.getRuntime().exec(arr);

In above code: I try to rename 8 files named hr1, hr2 .... to hrs1 , hrs2 ... etc. In cd command I try to enter the required directory. 在上面的代码中:我尝试将名为hr1,hr2 ....的8个文件重命名为hrs1,hrs2 ...等。在cd命令中,我尝试输入所需的目录。 However, I have used absolute path also. 但是,我也使用了绝对路径。 But the code is giving error: 但是代码给出了错误:

java.io.IOException: Cannot run program "cd": java.io.IOException: error=2, No such file or directory

java.io.IOException: Cannot run program "mv  /home/arps/FBI/hr1.txt    /home/arps/FBI/hrs1.txt": java.io.IOException: error=2, No such file or directory

Can anybody help me why is this happening though I manually execute those command means "mv /home/arps/FBI/hr1.txt /home/arps/FBI/hrs1.txt" and executes properly? 有人可以帮我为什么会发生这种情况,尽管我手动执行这些命令意味着"mv /home/arps/FBI/hr1.txt /home/arps/FBI/hrs1.txt"并正确执行?

cd is a built-in command to the current shell - you can't execute it - it's a shell built-in, as the cwd is a process-level setting, so a new process has it's own value. cd是当前shell的内置命令-您无法执行-cd是内置的shell,因为cwd是进程级别的设置,因此新进程具有其自己的价值。 There is no way to change the cwd from within the java process. 无法从Java进程中更改cwd。

The array argument version of exec is for executing a single command, where you have split the arguments yourself, not for executing multiple commands. exec数组参数版本用于执行单个命令,在此您自己分割了参数,而不用于执行多个命令。

So you either need to give full paths, or implement the copy yourself in Java. 因此,您要么需要给出完整的路径,要么自己用Java实现副本。

Change the final line of your program from 更改程序的最后一行

Process pp=Runtime.getRuntime().exec(arr);

to: 至:

 for (String cmdLine: arr) {
    Process pp=Runtime.getRuntime().exec(cmdLine);

and you will execute each line separately, according to RunTime documentation . 然后您将根据RunTime文档分别执行每一行。

您最好编写一个满足您需要的shell脚本,然后从Java调用它。

arr array must store the arguments of command. arr数组必须存储命令的参数。 Not seperated commands. 不分开的命令。 refer to my question. 请参阅我的问题。 run shell command from java 从Java运行Shell命令

If ls -l /home/arps/FBI/hrs1.txt outputs nothing as you said in the comments, then the file you're trying to rename simply does not exist, so the exception is right about this. 如果ls -l /home/arps/FBI/hrs1.txt如您在注释中所说的没有输出任何内容,则您尝试重命名的文件根本不存在,因此例外情况是正确的。

PS: IMHO this is not to be done in Java. PS:恕我直言,这不是用Java完成的。 Use scripting languages for such things. 使用脚本语言进行此类操作。 Way easier and way smaller code. 更容易,更小的代码。 For each problem, try to use the right tool, not one tool for all problems. 对于每个问题,请尝试使用正确的工具,而不是针对所有问题的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM