engineer007
asked on
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.cre ate(Native Method)
at java.lang.Win32Process.<in it>(Win32P rocess.jav a:63)
at java.lang.Runtime.execInte rnal(Nativ e Method)
at java.lang.Runtime.exec(Run time.java: 566)
at java.lang.Runtime.exec(Run time.java: 428)
at java.lang.Runtime.exec(Run time.java: 364)
at java.lang.Runtime.exec(Run time.java: 326)
at test.Main1.main(Main1.java :7)
package test;
public class Main1 {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec(
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Execution result:
java.io.IOException: CreateProcess: mkdir c:\temp error=2
at java.lang.Win32Process.cre
at java.lang.Win32Process.<in
at java.lang.Runtime.execInte
at java.lang.Runtime.exec(Run
at java.lang.Runtime.exec(Run
at java.lang.Runtime.exec(Run
at java.lang.Runtime.exec(Run
at test.Main1.main(Main1.java
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try: Runtime.getRuntime().exec( "cmd /C start mkdir c:\temp");
Sorry
Try: Runtime.getRuntime().exec( "cmd /C mkdir c:\temp");
Try: Runtime.getRuntime().exec(
bah... too late
But if it is to create a directory, the use of File.makedir() would be better:
File dir = new File("C:/temp");
dir.mkdir();
File dir = new File("C:/temp");
dir.mkdir();
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'
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'
>> all of the above comments are correct.
even the last one was ;°)
even the last one was ;°)
ASKER
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.
Runtime.getRuntime().exec(
> 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.
> getting created.
What is it doing now? Is it crashing? You might have to read the input and erro in different threads.
ASKER
Process.exitValue() returns a value of 1
>> 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");
Try adding an extra \ :
Runtime.getRuntime().exec(
This works for me (while the previous didn't even compile)
or use it with the other slash :)
Runtime.getRuntime().exec( "cmd /c mkdir c:/temp");
Runtime.getRuntime().exec(
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
File thisDir = new File("c:\\myDir");
thisDir.mkdir();
//Naeem Shehzad Ghuman
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")
(You get: "The syntax of the command is incorrect")
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.
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.
zzynx you r right :)
first u can not pass as c:\temp <\t is tab delimiter>
try this c:/temp
try this c:/temp
nadhuvi,
You'd better read the previous comments
You'd better read the previous comments
Hi engineering007, any questions left?
ASKER
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.
ok....
>>I tried the code on a freinds machine and its working there
Running what OS?
>> But i wont work on mine
Running what OS?
Running what OS?
>> But i wont work on mine
Running what OS?
ASKER
What was that ok fro nadhuvi??
>> Ohkay I tried the code
You mean the one with the double back slash?
>>Runtime.getRuntime().exe c("cmd /c mkdir c:\\temp");
You mean the one with the double back slash?
>>Runtime.getRuntime().exe
>> What was that ok fro nadhuvi??
That was meant for my remark
>>nadhuvi,
>>You'd better read the previous comments
That was meant for my remark
>>nadhuvi,
>>You'd better read the previous comments
ASKER
Yes the same one
Then please answer my questions above...
ASKER
We both are running windows2000 professional.
Sure you have enough rights to create a temp directory on drive C ?
ASKER
Yeah quite sure - Infact its not just about making of directories. Whtever command I am attempting isn't running.
>>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?
So,
Runtime.getRuntime().exec(
doesn't create you a dir.txt file on the C drive?
Do you get any errors/exceptions?
ASKER
Hey well - I just happened to restart my pc after 5 days and things have started to work ;)
Thx zzynx
Thx zzynx
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. ???
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. ???