Solved

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

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 53
Oracle SQL syntax check  without executing 6 56
jboss wildfly 10.1 10 96
jsp insert to database example 2 24
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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