Results 1 to 8 of 8
  1. #1
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default Need Help to Execute the commands from Java Program

    Can anybody help me how to execute the following one
    import java.io.*;
    class RunCommandDemo
    {
    public static void main(String args[])
    {
    try
    {
    String s=null;
    //System.out.println(args[0]);
    String arg[]={"type","D:/Anji/JavaExamples/MyFile.txt"};
    Process p=Runtime.getRuntime().exec(arg);
    BufferedReader bf=new BufferedReader(new InputStreamReader(p.getInputStream()));
    while((s=bf.readLine())!=null)
    System.out.println(s);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    on execution i'm getting the following error:-
    ------------------------------------------
    D:\Anji\JavaExamples>java RunCommandDemo
    java.io.IOException: Cannot run program "type": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at RunCommandDemo.main(RunCommandDemo.java:11)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 4 more
    Note:-
    ----
    I tried it by reading from the command line also But i couldn't get the result.

  2. #2
    Hussain Ali is offline Member
    Join Date
    Feb 2010
    Posts
    37
    Rep Power
    0

    Default

    which command are you using for compile and run?

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You are passing the command
    "type D:/Anji/JavaExamples/MyFile.txt"
    and your system is telling you that there is no program called type. What did you want the program to do instead?

  4. #4
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Actually i want to execute the MS-DOS "type" command by passing
    "<file-name>" as an argument.For this in docs i found a function
    java.lang.Process exec(String cmdarray[]) here,the first String reference in the cmdarray[] is a "Command" that we want to execute;subsequent String references presented in the cmdarray are the respective commands arguments.
    So,I created a String array with first reference as "type" (command),Second reference as " d:\anji\JavaExamples\MyFile.txt" (which is an argument to "type" command).Hence it should execute type d:\anji\JavaExamples\MyFile.txt na?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Run the command on your command line first before writing the Java code for it.
    Don't those forward slashes need to be backslashes (\) instead for Windows?

  6. #6
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    I already executed the command "type <file-name>" before posting the thread.
    Now,I checked it with backslashes is well but same problem.
    I changed the file-path to "d:\\anji\\JavaExamples\\MyFile.txt" here "\\" to escape the backslash in quotes.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Which command did you run successfully on your DOS prompt?

  8. #8
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    I ran the following command in DOS prompt
    d:\anji\JavaExamples>type MyFile.txt
    [and]
    d:\anji\JavaExamples>type d:\anji\JavaExamples\MyFile.txt

Similar Threads

  1. Execute the commands in Jsp Program
    By swetha_2008 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-17-2008, 07:04 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. Using Runtime to execute external commands
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 10:14 AM
  4. Replies: 2
    Last Post: 08-07-2007, 07:22 PM
  5. Replies: 1
    Last Post: 07-27-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •