简体   繁体   中英

Getting an error while running Runtime.exec method in java

I am using java program to run priyam.bat file which is i placed in D:/priyam.bat directory but while running this java program i am getting the below error .

java.io.IOException: Cannot run program "priyam.bat" (in directory "D:\"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at Test.main(Test.java:12)
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>(ProcessImpl.java:177)
    at java.lang.ProcessImpl.start(ProcessImpl.java:28)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
    ... 3 more

Snippet of code

import java.io.File;
import java.io.IOException;


public class Test {

public static void main(String args[])
{
    Runtime r = Runtime.getRuntime();
    int arg = 5;
    try {
        Process p = r.exec("priyam.bat",null,new File("D://"));
        System.out.println("executed");


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

As i am new to the java world,your help and suggestions will be highly lauded and appreciated. Thanks in advance

Try with this code:

Process p = r.exec("D:\\priyam.bat"));

In windows the file separator is "\\" or you can use File.separator value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM