1

I want to write a java code that executes some Linux command:

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. 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?

Arpssss
  • 3,850
  • 6
  • 36
  • 80
  • did u remove the file by executing manually and then ran the program ???? your log says ' No such file or directory' – Akhi Jun 26 '12 at 12:17
  • @AkhilDev, NO. I just checked. After that I return it to its previous condition. – Arpssss Jun 26 '12 at 12:18
  • @AkhilDev, I checked it through ls also. – Arpssss Jun 26 '12 at 12:21
  • What does `ls -l /home/arps/FBI/hrs1.txt` says? – m0skit0 Jun 26 '12 at 12:26
  • To rename files in a Java program, I would prefer to use [java.io.File.renameTo](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#renameTo%28java.io.File%29). – ewan.chalmers Jun 26 '12 at 12:34
  • If it outputs nothing, then the file does not exist. You can check manually doing `ls -l /home/arps/FBI/` to list all the files in the directory – m0skit0 Jun 26 '12 at 12:41
  • I would recommend using the ProcessBuilder instead Runtime.exec: http://www.javabeat.net/2007/08/using-the-new-process-builder-class/ It has some advantages, e.g. merging StdOut and StdErr into StdOut. – jayeff Jun 26 '12 at 12:52

5 Answers5

9

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. There is no way to change the cwd from within the java process.

The array argument version of exec is for executing a single command, where you have split the arguments yourself, not for executing multiple commands.

So you either need to give full paths, or implement the copy yourself in Java.

Community
  • 1
  • 1
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • He's already providing full paths, correct me if I'm wrong here. The problem here is simply that the file does not exist (check the comments on the question). – m0skit0 Jun 26 '12 at 12:44
2

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.

rlinden
  • 2,053
  • 1
  • 12
  • 13
2

You might be better off writing a shell script that does what you need and invoking that from Java.

user888379
  • 1,343
  • 12
  • 30
  • ya writing the script is good,but add it to the system path like this sudo cp path-to-your-shell-file /usr/bin/ ....thats where its gonna look when you'll run it. – cafebabe1991 Sep 15 '13 at 11:10
0

arr array must store the arguments of command. Not seperated commands. refer to my question. run shell command from java

Community
  • 1
  • 1
masay
  • 923
  • 2
  • 17
  • 34
0

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.

PS: IMHO this is not to be done in 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.

m0skit0
  • 25,268
  • 11
  • 79
  • 127