Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-07-25
4
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

704 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