Solved

Problem with Runtim.exec()

Posted on 2004-10-15
34
274 Views
Last Modified: 2008-02-01
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)
0
Comment
Question by:engineer007
  • 16
  • 8
  • 4
  • +4
34 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 25 total points
ID: 12317848
Runtime.getRuntime().exec("cmd /c mkdir c:\temp");
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317849
Try: Runtime.getRuntime().exec("cmd /C start mkdir c:\temp");
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317853
Sorry

Try: Runtime.getRuntime().exec("cmd /C mkdir c:\temp");
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 35

Expert Comment

by:girionis
ID: 12317855
bah... too late
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317864
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 12317878
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12317886
>> all of the above comments are correct.
even the last one was ;°)
0
 

Author Comment

by:engineer007
ID: 12317904
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
 
LVL 35

Expert Comment

by:girionis
ID: 12317940
> 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
 

Author Comment

by:engineer007
ID: 12317999
Process.exitValue() returns a value of 1
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12318042
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12318046
This works for me (while the previous didn't even compile)
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12318559
or use it with the other slash :)
Runtime.getRuntime().exec("cmd /c mkdir c:/temp");
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12318617
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12318636
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12318653
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
 
LVL 18

Expert Comment

by:armoghan
ID: 12318659
zzynx you r right :)
0
 
LVL 1

Expert Comment

by:nadhuvi
ID: 12336539
first u can not pass as c:\temp <\t is tab delimiter>
try this c:/temp

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336564
nadhuvi,
You'd better read the previous comments
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336863
Hi engineering007, any questions left?
0
 

Author Comment

by:engineer007
ID: 12336873
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
 
LVL 1

Expert Comment

by:nadhuvi
ID: 12336874
ok....
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336893
>>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
 

Author Comment

by:engineer007
ID: 12336894
What was that ok fro nadhuvi??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336898
>> Ohkay I tried the code
You mean the one with the double back slash?
         >>Runtime.getRuntime().exec("cmd /c mkdir c:\\temp");
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336914
>> What was that ok fro nadhuvi??
That was meant for my remark
       >>nadhuvi,
       >>You'd better read the previous comments
0
 

Author Comment

by:engineer007
ID: 12336952
Yes the same one
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336975
Then please answer my questions above...
0
 

Author Comment

by:engineer007
ID: 12355250
We both are running windows2000 professional.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12355429
Sure you have enough rights to create a temp directory on drive C ?
0
 

Author Comment

by:engineer007
ID: 12355542
Yeah quite sure - Infact its not just about making of directories. Whtever command I am attempting isn't running.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12355582
>>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
 

Author Comment

by:engineer007
ID: 12355641
Hey well - I just happened to restart my pc after 5 days and things have started to work ;)
Thx zzynx
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12355677
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

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 77
servlet doXXX methods 3 49
spring jars download 1 27
JavaFX TableView not displaying correctly 3 18
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question