Solved

unable to run java's Runtime.exec(dos command) on windows NT

Posted on 2001-07-25
4
464 Views
Last Modified: 2008-02-01
Hi All,
Iam getting a problem when i tried to excute
Runtime.exec(dos command) on WindowsNT
The problem came when i tried to execute the following simple code..
import java.lang.Process;
import java.lang.Runtime;
public class TestFile
{
static Process p;
static Runtime rt=Runtime.getRuntime();
public static void main(String args[])
{
try{
p=rt.exec("ren c:\\test test.txt");
}catch(Exception e){
     //System.out.println(e);
     e.printStackTrace();
     }
}
}
and iam getting the following output
java.io.IOException: CreateProcess: ren c:\test test.txt error=2
        at java.lang.Win32Process.create(Native Method)
        at java.lang.Win32Process.<init>(Unknown Source)
        at java.lang.Runtime.execInternal(Native Method)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at TestFile.main(TestFile.java:10)
So i request all of u to give me a solution if u know..
my mailing address is sdivvela@in.ibm.com

thanks and regards
Srinivas
0
Comment
Question by:divvela
  • 2
4 Comments
 

Author Comment

by:divvela
ID: 6317067
I request all of u to send me a clear solution
0
 
LVL 16

Accepted Solution

by:
imladris earned 75 total points
ID: 6317597
There doesn't appear to be anything wrong with the Java code perse. One possibility is that c:\test doesn't exist and so the rename fails. But I believe the likely solution is that "ren" is implemented in DOS by the command processor. In that case, there is no program "ren" for the runtime exec to execute. You would need to specify the command processor. Something like:

p=rt.exec("command /c ren c:\\test test.txt");
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6317626
In order to run a dos command on NT you need to start the command processor (CMD). Runtime.exec() is looking for an executable file to run and when you simply pass it "ren C:\test test.txt" its looking for an executable called ren.exe in your path. Since REN is a command built into DOS, there is no executable called ren.exe on the machine so the Runtime.exec() call fails. The errorcode 2 says that the command file was not found.

Try the following:

----------------------------------------------------------

import java.lang.Process;
import java.lang.Runtime;

public class TestFile
{
  static Process p;
  static Runtime rt=Runtime.getRuntime();
  public static void main(String args[])
  {
    try
    {
      p=rt.exec("cmd /c ren c:\\test test.txt");
    }
    catch(Exception e)
    {
      //System.out.println(e);
      e.printStackTrace();
    }
  }
}

-----------------------------------------------------------

Hope it helps...

--Steve
0
 

Author Comment

by:divvela
ID: 6317803
Thank you very very....much imladris ......
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 36
null output 3 42
Android development question 2 75
spring maven example issues 3 37
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

679 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