homepage Welcome to WebmasterWorld Guest from 23.20.63.27
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
Forum Library, Charter, Moderators: bakedjake

Linux, Unix, and *nix like Operating Systems Forum

    
Runtime.getRuntime().exec("cd") returns java.io.IOException, error 2
when calling "cd" shell command from java app
rus3439




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

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

 

Little_G




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

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]

rus3439




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

absolute path does not work ... any other alternatives to setting the current directory from java app?

Little_G




msg:3613870
 12:05 am on Mar 29, 2008 (gmt 0)

Hi,

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

Andrew

rus3439




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

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.

Little_G




msg:3614088
 12:00 pm on Mar 29, 2008 (gmt 0)

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]

rus3439




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

what does "/bin/bash" command do?

rus3439




msg:3621380
 12:27 am on Apr 8, 2008 (gmt 0)

Thanks so much Little_G! it worked like a charm :-)!

rus3439




msg:3622174
 10:09 pm on Apr 8, 2008 (gmt 0)

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.

Little_G




msg:3622184
 10:35 pm on Apr 8, 2008 (gmt 0)

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

rus3439




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

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.

rus3439




msg:3622191
 11:00 pm on Apr 8, 2008 (gmt 0)

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.

rus3439




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

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?

rus3439




msg:3622353
 4:28 am on Apr 9, 2008 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved