Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Changing directory using exec

Thread view: 
festo - 24 Oct 2006 23:19 GMT
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
Thomas Kellerer - 24 Oct 2006 23:24 GMT
festo wrote on 25.10.2006 00:19:
> I am wondering why I cannot change the directory using my java program
> like so:
[quoted text clipped - 4 lines]
> 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
Thomas Fritsch - 24 Oct 2006 23:31 GMT
>I am wondering why I cannot change the directory using my java program
> like so:
[quoted text clipped - 4 lines]
> 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"));

Signature

Thomas

festo - 25 Oct 2006 02:22 GMT
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.

> >I am wondering why I cannot change the directory using my java program
> > like so:
[quoted text clipped - 10 lines]
> Process proc = runtime.exec("ls" + ..., new String[0], new
> File("/home/user"));
hiwa - 25 Oct 2006 02:59 GMT
> 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.
Thomas Kellerer - 25 Oct 2006 12:21 GMT
> 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
Martin Gregorie - 25 Oct 2006 22:01 GMT
> 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.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.