LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

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


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 App.main(App.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

Last edited by tatarin; 04-07-2008 at 07:49 PM.
 
Old 03-28-2008, 06:21 PM   #2
abolishtheun
Member
 
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, 06:48 PM   #3
tatarin
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, 06:52 PM   #4
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
http://www.devx.com/tips/Tip/13804
 
Old 03-28-2008, 08:42 PM   #5
jay73
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
System.out.println(s);
}

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)
System.out.println(s);

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


Reply


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 12:29 PM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 04:47 AM
Runtime.getRuntime().exec("javac ") ihasircioglu Programming 1 05-27-2004 07:54 AM
I cannot use "java chat". Browser says plugin required "x-java-vm". jdruin Linux - Software 4 04-18-2004 06:44 PM
Java "throws IOException" problem Nylex Programming 5 01-30-2004 09:49 PM


All times are GMT -5. The time now is 11:45 PM.

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