Not able to execute an exe file from java using ProcessBuilder

匆匆过客 提交于 2019-12-13 14:47:09

问题


I am making a project to run C, C++ and Java, from within Java code itself. It works absolutely fine for Java, and the problem is faced when compiling and executing C and C++ files.

I got my compilation right with this code and I can get the executable file generated in my specified path. But now when I run the executable binary from ProcessBuilder I get an error saying that 'file was not found'. Please see to the code and tell me what is going wrong and where??

public void processCode(String path,String lang)throws IOException
    {
        String cmd="",s=null,out=null,file="";
        totalTime=0;
        ProcessBuilder process=new ProcessBuilder();
        process.directory(new File(path));
        if(lang.equals("c")||lang.equals("cpp"))
        {
            cmd=threadNum+".exe";
            process.command(cmd);
        }
        else if(lang.equals("java"))
        {
            cmd="java";
            file="Main"+threadNum;
            process.command(new String[]{cmd,file});
        }
        process.redirectInput(new File(PATH+"Input\\" + prob + ".txt"));
        process.redirectOutput(new File(PATH+"Output.txt"));
        Process p=process.start();
        long start=System.currentTimeMillis();
        while (true)
        {
            try{
                    if(p.exitValue()==0)
                    {
                        totalTime=(int)(System.currentTimeMillis()-start);
                        break;
                    }
                }
                catch (Exception e)
                {

                }
                if(System.currentTimeMillis()-start>2000)
                {
                    res=1;
                    p.destroy();
                    break;
                }
        }
        if(res!=1)
        {
            compareFile();
        }
    }

The method is called from here And the error generated is :

Exception in thread "main" java.io.IOException: Cannot run program "19.exe" (in directory "C:\wamp\www\usercodes\lokesh"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at Contest.processCode(Main.java:202)
    at Contest.compileCode(Main.java:180)
    at Contest.makeFile(Main.java:157)
    at Contest.main(Main.java:53)
    at Main.main(Main.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
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:188)
    at java.lang.ProcessImpl.start(ProcessImpl.java:132)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
    ... 10 more

回答1:


Setting the directory of a ProcessBuilder does not have any effect on where the system will look for the executable when it tries to start a process. It merely sets the current working directory of the newly-created process to this directory, should it be able to launch a process successfully. Your program 19.exe may well exist in C:\wamp\www\usercodes\lokesh, but unless this folder is on the PATH, the system will not be able to start your process.

Try running the process using the full path of the executable instead of just 19.exe.

It does have to be said that the error message is somewhat misleading. It says that it couldn't find your executable, and then it says 'in directory ...', which implies that that was where it was looking for it.



来源:https://stackoverflow.com/questions/11276421/not-able-to-execute-an-exe-file-from-java-using-processbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!