3

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
2
  • 1
    Dept. of the bleedin' obvious here, but you haven't actually said anywhere that you've checked and "C:\wamp\www\usercodes\lokesh\19.exe" definitely exists. Does it? Commented Jun 30, 2012 at 19:01
  • Yes it does..I mentioned it that "I can get the .exe file generated in my specified path" and u can also see the link to confirm it Commented Jun 30, 2012 at 19:14

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.