Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-28-2008, 04:54 PM   #1
LQ Newbie
Registered: Mar 2008
Posts: 10

Rep: Reputation: 0
Runtime.getRuntime().exec("cd") returns, error 2 in java


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" Cannot run program "cd": error=2, No such file or directory
at java.lang.ProcessBuilder.start(
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(
at App.main(
Caused by: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(
at java.lang.ProcessImpl.start(
at java.lang.ProcessBuilder.start(
... 4 more

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

Any idea why?

thanks in advance,

Last edited by tatarin; 04-07-2008 at 06:49 PM.
Old 03-28-2008, 05:21 PM   #2
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
guess: 'cd' is a built-in shell command, and getRuntime is expecting an actual program?
Old 03-28-2008, 05:48 PM   #3
LQ Newbie
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
If it does not understand "cd" what would be the alternative to set a working directory to say "usr/local/username"?
Old 03-28-2008, 05:52 PM   #4
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
Old 03-28-2008, 07:42 PM   #5
LQ Guru
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
You can string a path together using System.getProperty(some_dir).

For example,

File file = new File(System.getProperty("user.home")); //we start from the user's home dir
File parentFile = file.getParentFile().getParentFile(); //climb up two levels i.e. all the way up to /
if (parentFile.getAbsolutePath().equals("/")){ // check whether we really have /
String [] files = parentFile.list(); //list the contents ...
for (String s : files) //and print them out

This will list all the files directly under /.

You can then move from / to /usr/local with:
File usrLocal = new File(parentFile + "usr" + File.separator + "local")

or all in one go, like this, much shorter:
String path = "/usr/local";
File usrLocal = new File(path);
String [] usrLocalFiles = usrLocal.list();
for (String s : usrLocalFiles)

Last edited by jay73; 03-28-2008 at 07:50 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
java runtime() exec() program mcshen Programming 4 11-25-2004 11:29 AM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 03:47 AM
Runtime.getRuntime().exec("javac ") ihasircioglu Programming 1 05-27-2004 06:54 AM
I cannot use "java chat". Browser says plugin required "x-java-vm". jdruin Linux - Software 4 04-18-2004 05:44 PM
Java "throws IOException" problem Nylex Programming 5 01-30-2004 08:49 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:28 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration