Solved

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

Posted on 2001-07-25
4
455 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now