Welcome to WebmasterWorld Guest from 184.73.13.66

Forum Moderators: bakedjake

Message Too Old, No Replies

Runtime.getRuntime().exec("cd") returns java.io.IOException, error 2

when calling "cd" shell command from java app

   
10:52 pm on Mar 28, 2008 (gmt 0)

10+ Year Member



Hi,

I'm trying to execute Linux Shell command from java application and get a runtime error after executing the above line:

Exception in thread "main" java.io.IOException: Cannot run program "cd": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at LauncherWin.main(LauncherWin.java:137)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 4 more

It works for other commands like "pwd", "ls -l" etc. but not "cd".

Any idea why?

thanks in advance,
tatarin

11:10 pm on Mar 28, 2008 (gmt 0)

5+ Year Member



Hi,

pwd and ls are external programs whereas cd is an internal shell command. Try using absolute path names instead.

Andrew

[edited by: Little_G at 11:11 pm (utc) on Mar. 28, 2008]

11:46 pm on Mar 28, 2008 (gmt 0)

10+ Year Member



absolute path does not work ... any other alternatives to setting the current directory from java app?
12:05 am on Mar 29, 2008 (gmt 0)

5+ Year Member



Hi,

You could try the exec method that accepts a working directory argument, [java.sun.com...]

Andrew

4:49 am on Mar 29, 2008 (gmt 0)

10+ Year Member



do you have an example for this case? I have tried this command as well before posting to this forum, but it returns the same error.
What I'm trying to do is to execute a build command from java app like "mach/blaze/javac" etc. which can only be executed from a particular directory and won't work with absolute path. Basically java app triggers a shell command that triggers another java app to run using build command.
12:00 pm on Mar 29, 2008 (gmt 0)

5+ Year Member



Hi,

OK, you can run an instance of the shell of your choice and send it commands via the Process's OutputStream, this allows you to simulate typeing commands into the terminal. Here I've used bash with an initial working directory of /bin the program should print out "/bin" followed by "/" as the cd command moves to the parent directory.

File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd ..");
out.println("pwd");
out.println("exit");
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}

Andrew

[edited by: Little_G at 12:02 pm (utc) on Mar. 29, 2008]

12:11 am on Apr 8, 2008 (gmt 0)

10+ Year Member



what does "/bin/bash" command do?
12:27 am on Apr 8, 2008 (gmt 0)

10+ Year Member



Thanks so much Little_G! it worked like a charm :-)!
10:09 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



I have another problem now :-(.
I'm trying to set up a crontab job to run this and as as soon as it gets to execute the out.println("java AppClass") it exists. It works fine when I run the perl script responsible for it manually from command line, but chokes with exit value = 127 when I run it through crontab. Any idea? I tried putting thread.sleep before exit but it does not work.
10:35 pm on Apr 8, 2008 (gmt 0)

5+ Year Member



Hi,

127 means 'command not found' so it's attemping to run a program that it can't find.
Is there a specific reason that you need to do this in java? You may be able to do what you want with a shell script.

Andrew

10:54 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



yea I have to do it in java since our whole framework is in java. The java class works fine and does what is intended manually, but does chokes with cron job.
11:00 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



The perl script that I crontab executes is
#!/usr/bin/perl
system("java -classpath /usr/local/username folder1.folder2.AppLnx");

when i run this script manually from command line it works fine. I know crontab starts in the root directory but the script you provided should have moved to the directory where i need to execute that cause it does work manually.

11:22 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



i seems like the code you suggested is ineffective with crontab since it executes everything from root and ignores the cd commands and any other attempts to change the directory. My question is how do I change the directory in perl script so crontab does not ignore it?
4:28 am on Apr 9, 2008 (gmt 0)

10+ Year Member



I was able to run the code with crontab but now it chokes on finding firefox :-(. Again it works fine when I run it from the command line but running via crontab throws and error saying that firefox-bin can't be located. Seems like crontab is looking for absolute path but can't find it. Any suggestions are welcome.

thanks,
tatarin

 

Featured Threads

Hot Threads This Week

Hot Threads This Month