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?

7
  • did u remove the file by executing manually and then ran the program ???? your log says ' No such file or directory' Commented Jun 26, 2012 at 12:17
  • @AkhilDev, NO. I just checked. After that I return it to its previous condition. Commented Jun 26, 2012 at 12:18
  • @AkhilDev, I checked it through ls also. Commented Jun 26, 2012 at 12:21
  • What does ls -l /home/arps/FBI/hrs1.txt says? Commented Jun 26, 2012 at 12:26
  • To rename files in a Java program, I would prefer to use java.io.File.renameTo. Commented Jun 26, 2012 at 12:34

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

2

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

1 Comment

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

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.