Changing directory using exec

F

festo

I am wondering why I cannot change the directory using my java program
like so:

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("cd" + " " + directory);


Is there an easy way to accomplish this because using ("ls" + .....)
works but not the "cd".

Please help.

fezto
 
T

Thomas Kellerer

festo wrote on 25.10.2006 00:19:
I am wondering why I cannot change the directory using my java program
like so:

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("cd" + " " + directory);


Is there an easy way to accomplish this because using ("ls" + .....)
works but not the "cd".

You are starting a new process that will change *its* working directory. But
this will not change the working directory for the process that runs your Java
program.

But why do you want to do that?

Thomas
 
T

Thomas Fritsch

festo said:
I am wondering why I cannot change the directory using my java program
like so:

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("cd" + " " + directory);


Is there an easy way to accomplish this because using ("ls" + .....)
works but not the "cd".
See the API doc of Runtime.
There are two exec() methods, which take a File parameter (the working
directory in which the process will run). Use for example:
Process proc = runtime.exec("ls" + ..., new String[0], new
File("/home/user"));
 
F

festo

I am doing a file transfer client server program and I need to be able
to change to a directory on the server to do an "ls" to list the
directory files then transfer file(s) from server to client.

Thomas said:
festo said:
I am wondering why I cannot change the directory using my java program
like so:

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("cd" + " " + directory);


Is there an easy way to accomplish this because using ("ls" + .....)
works but not the "cd".
See the API doc of Runtime.
There are two exec() methods, which take a File parameter (the working
directory in which the process will run). Use for example:
Process proc = runtime.exec("ls" + ..., new String[0], new
File("/home/user"));
 
H

hiwa

festo said:
I am doing a file transfer client server program and I need to be able
to change to a directory on the server to do an "ls" to list the
directory files then transfer file(s) from server to client.
You don't need to change directory to do that -- listing files in a
directory and send one of them.
 
T

Thomas Kellerer

I am doing a file transfer client server program and I need to be able
to change to a directory on the server to do an "ls" to list the
directory files then transfer file(s) from server to client.
Why do you start a new process to list the files in a directory?

Have a look at File.listFile():

File dir = new File("/path/to/directory");
File[] filesInDir = dir.listFile();

Thomas
 
M

Martin Gregorie

festo said:
I am doing a file transfer client server program and I need to be able
to change to a directory on the server to do an "ls" to list the
directory files then transfer file(s) from server to client.
Have you considered using a Java SSH client class to talk to a standard
ftpd or sshd server?

Java class libraries exist to implement the client end of an FTP
connection which would connect to a standard ftpd server and should do
everything you need, though its relatively insecure. See:
http://jakarta.apache.org/commons/net/api/org/apache/commons/net/ftp/FTPClient.html

If you need higher security, e.g. encrypted passwords and files in
transit you should look at SSH. A full SSH implementation supports the
scp facilities for file transfer as well as the sftp facilities which
are a close approximation to FTP in terms of directory listing, moving
around the directory tree and moving files to or from the server, but
encrypt files during transfer. See http://linuxmafia.com/ssh/java.html
for information on available class libraries.

Using either FTP or SSH will save you a shed load of work because the
servers are standard, tested off the shelf code and have been ported to
almost every OS capable of supporting them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top