system command exacuting

Ok, so im not sure how to use the exec thing at all.

part of the code where im having my problem is here

rt.exec(new String []{"mkdir","bob1"});//should make a directory bob w/e this script is

rt.exec(new String []{"cd","~"});//changes the directory to the home directory

rt.exec(new String []{"mkdir","bob2"});//shuld make folder bob2 in home directory but makes it where the script is

The problem is stated in the code.

bob1 and bob2 end up in the same directory but bob2 should be in the home do to the cd command.

Any ideas?

[1082 byte] By [krrose27a] at [2007-11-27 11:18:49]
# 1

I have also tried such things as:

rt.exec(new String [] { "cd","~","&&","mkdir","bob2"});

rt.exec(new String [] { "cd","~",";","mkdir","bob2"});

No succes, yet with my terminal thoes do work.

krrose27a at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 2

It is because these are 3 separate processes so the second one just changes directory and then exits.

Take a look the exec method which lets you specify a start directory

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String,%20java.lang.String[],%20java.io.File)

_helloWorld_a at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 3

Also there's good general discussion about using exec() here:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

pbrockway2a at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 4

> It is because these are 3 separate processes so the

> second one just changes directory and then exits.

>

> Take a look the exec method which lets you specify a

> start directory

>

> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runt

> ime.html#exec(java.lang.String,%20java.lang.String[],%

> 20java.io.File)

ty that helped.

Now im trying to seup the directory it should run from, the path is as follows.

/Users/kevinrose/Desktop/Bot\ Folder/

the only problem when i make the file varaiable which is:

File home = new File("/Users/kevinrose/Desktop/Bot\ Folder/");

eclipse tells me its an invalid escape sequence.

krrose27a at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 5

You need to escape you backslash like so

"\\"

_helloWorld_a at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 6

> rt.exec(new String [] { "cd","~"});//changes the

Special chars like ~, &, |, >, etc. are interpreted by the shell (e.g. bash) which doesn't exist when you do runtime.exec. The "~" here will be taken as a literal directory name. If you rane "cd ~" in a shell in a terminal window (which is identical to just "cd", by the way), the "cd" command would never see the ~, because the shell would expand it out to the full path to your home directory (assuming that shell supports that).

If you want to have those chars interpreted by a shell, exec the shell and pass the rest as args to it. For example exect("/bin/bash -c 'cd ~'");

jverda at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 7

> File home = new File("/Users/kevinrose/Desktop/Bot\

> Folder/");

new File("/Usres/blah/Bot Folder")

is fine. You don't need to escape the space. The File c'tor will handle that.

jverda at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 8

> You need to escape you backslash like so

>

> > "\\"

>

No, just get rid of it completely.

jverda at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 9

Apart from all those answers, using Runtime.exec() to create directories is grossly over-complicated given that the java.io.File class has a mkdir() method that you could use.

DrClapa at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 10

Spot on to the heart of the matter as always, Doctor.

jverda at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...
# 11

my name is yasser .

iam new friend.

thank you

Message was edited by:

yaserabdulah@yahoo.co.in

yaserabdulah@yahoo.co.ina at 2007-7-29 14:33:13 > top of Java-index,Java Essentials,New To Java...