• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reg:Executing Commands  RSS feed

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have developed an application in swings in which a user will schedule a command. So to make it user friendly the user will enter only the command, like the "del d:\exp.txt". I am successful in all other parts of the program but when i try to run the command from java i get the error. The code is below some one guide me through it please.


When i try to run the commands like the java or the task list i get the out put. But the command which i have written in the exec method gives error why so. the error is

java.io.IOException: Cannot run program "del": CreateProcess error=2, The system
cannot find the file specified
java.io.IOException: Cannot run program "del": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at comm.main(comm.java:12)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 4 more
Press any key to continue...


Thanks & Regards
alexander
 
Rob Spoor
Sheriff
Posts: 20898
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"del" is not an application, it's a built-in command of the "cmd.exe" application. Try "cmd /C del d:/exp.txt". There might be problems when the file has spaces in its absolute name though, but that's an issue with cmd.exe.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Prime. I have done what you said but still i get the error. The error is.

Exited with error code 1

 
Rob Spoor
Sheriff
Posts: 20898
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the file exist? What happens if you call the command directly, without Java?
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the file exist. The command works when i use it directly from command prompt.
when i use the command dir from java its working. But like del and copy are not Working.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its giving exited with error code 0, when i run the code below. But the file is not deleted.
 
Rob Spoor
Sheriff
Posts: 20898
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code works just fine on my PC.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!