Solved

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

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal Reports Licensing Questions 4 88
swing controls 2 38
activeMQ Queue Messages Enqueued and Messages Dequeued 1 120
Java Inheritance super keyword use 8 74
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

710 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