Problem with Runtim.exec()

Am trying to use Runtime.exec() to run system commands but my program aint working. The code and the StackTrace are as under. can anybody provide me with a solution.'

package test;

public class Main1 {
  public static void main(String[] args) {

    try {
      Runtime.getRuntime().exec("mkdir c:\temp");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

Execution result:

java.io.IOException: CreateProcess: mkdir c:\temp error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:63)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:566)
at java.lang.Runtime.exec(Runtime.java:428)
at java.lang.Runtime.exec(Runtime.java:364)
at java.lang.Runtime.exec(Runtime.java:326)
at test.Main1.main(Main1.java:7)
engineer007Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
zzynxConnect With a Mentor Software engineerCommented:
Runtime.getRuntime().exec("cmd /c mkdir c:\temp");
0
 
girionisCommented:
Try: Runtime.getRuntime().exec("cmd /C start mkdir c:\temp");
0
 
girionisCommented:
Sorry

Try: Runtime.getRuntime().exec("cmd /C mkdir c:\temp");
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
girionisCommented:
bah... too late
0
 
zzynxSoftware engineerCommented:
But if it is to create a directory, the use of File.makedir() would be better:

File dir = new File("C:/temp");
dir.mkdir();
0
 
MogalManicCommented:
mkdir is an internal command to the Windows shell, so all of the above comments are correct.

BTW, You can do the same thing nativly using Java:
   java.io.File dir=new File("c:/temp");
   dir.mkdirs();  //This will create ALL of the directories specified by the path of 'dir'
0
 
zzynxSoftware engineerCommented:
>> all of the above comments are correct.
even the last one was ;°)
0
 
engineer007Author Commented:
well infact I have to run other system commands as well.

 Runtime.getRuntime().exec("cmd /c mkdir c:\temp"); ain't working as well. Now I aint getting the exception but still c:\temp directory is not getting created.
0
 
girionisCommented:
> Now I aint getting the exception but still c:\temp directory is not
> getting created.

What is it doing now? Is it crashing? You might have to read the input and erro in different threads.
0
 
engineer007Author Commented:
Process.exitValue() returns a value of 1
0
 
zzynxSoftware engineerCommented:
>> Runtime.getRuntime().exec("cmd /c mkdir c:\temp"); ain't working as well

Try adding an extra \ :

          Runtime.getRuntime().exec("cmd /c mkdir c:\\temp");
0
 
zzynxSoftware engineerCommented:
This works for me (while the previous didn't even compile)
0
 
armoghanCommented:
or use it with the other slash :)
Runtime.getRuntime().exec("cmd /c mkdir c:/temp");
0
 
NaeemgCommented:
If your objective is to create director on disk then y don't u use File object.? is so then use the followin code.

    File thisDir = new File("c:\\myDir");
    thisDir.mkdir();


//Naeem Shehzad Ghuman
0
 
zzynxSoftware engineerCommented:
No, that last one doesn't work, since in a Dos session you can't type:  mkdir c:/temp
(You get: "The syntax of the command is incorrect")
0
 
zzynxSoftware engineerCommented:
Remarks:
1) My previous comment was about armoghan's comment.
2) Naeemg, why do you simple repeat what is already said before? Please first read the previous comments.
0
 
armoghanCommented:
zzynx you r right :)
0
 
nadhuviCommented:
first u can not pass as c:\temp <\t is tab delimiter>
try this c:/temp

0
 
zzynxSoftware engineerCommented:
nadhuvi,
You'd better read the previous comments
0
 
zzynxSoftware engineerCommented:
Hi engineering007, any questions left?
0
 
engineer007Author Commented:
Ohkay I tried the code on a freinds machine and its working there. But i wont work on mine, Shall be thankful if you could gimme a hint.
0
 
nadhuviCommented:
ok....
0
 
zzynxSoftware engineerCommented:
>>I tried the code on a freinds machine and its working there
Running what OS?
>> But i wont work on mine
Running what OS?
0
 
engineer007Author Commented:
What was that ok fro nadhuvi??
0
 
zzynxSoftware engineerCommented:
>> Ohkay I tried the code
You mean the one with the double back slash?
         >>Runtime.getRuntime().exec("cmd /c mkdir c:\\temp");
0
 
zzynxSoftware engineerCommented:
>> What was that ok fro nadhuvi??
That was meant for my remark
       >>nadhuvi,
       >>You'd better read the previous comments
0
 
engineer007Author Commented:
Yes the same one
0
 
zzynxSoftware engineerCommented:
Then please answer my questions above...
0
 
engineer007Author Commented:
We both are running windows2000 professional.
0
 
zzynxSoftware engineerCommented:
Sure you have enough rights to create a temp directory on drive C ?
0
 
engineer007Author Commented:
Yeah quite sure - Infact its not just about making of directories. Whtever command I am attempting isn't running.
0
 
zzynxSoftware engineerCommented:
>>But i wont work on mine,

So,

   Runtime.getRuntime().exec("cmd /c dir > c:\\dir.txt");

doesn't create you a dir.txt file on the C drive?

Do you get any errors/exceptions?
0
 
engineer007Author Commented:
Hey well - I just happened to restart my pc after 5 days and things have started to work ;)
Thx zzynx
0
 
zzynxSoftware engineerCommented:
Thanks for accepting.

PS.
1) For the people reading this thread afterwards it is good to pay attention to what comment you mark as accepted answer. You had better marked the one with the double quotes as accepted answer since that's the right one.
2) Again, why did you gave a B-grade? It's simply *the* solution. ???
0
All Courses

From novice to tech pro — start learning today.