无法使用 ProcessBuilder 从 java 执行 exe 文件 [英] Not able to execute an exe file from java using ProcessBuilder

查看:87
本文介绍了无法使用 ProcessBuilder 从 java 执行 exe 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个从 Java 代码本身运行 C、C++ 和 Java 的项目.对Java完全没问题,编译执行C、C++文件时会遇到这个问题.

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.

我用这个代码正确编译,我可以在我指定的路径中生成可执行文件.但是现在当我从 ProcessBuilder 运行可执行二进制文件时,我收到一条错误消息,指出找不到文件".请查看代码并告诉我出了什么问题,在哪里??

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

推荐答案

设置 ProcessBuilder 的 directory 不会影响系统在尝试启动程序时将在哪里查找可执行文件过程.它只是将新创建的进程的当前工作目录设置为该目录,如果它能够成功启动进程.你的程序 19.exe 很可能存在于 C:\wamp\www\usercodes\lokesh 中,但是除非这个文件夹在 PATH 上,否则系统将无法开始您的流程.

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.

尝试使用可执行文件的完整路径运行该进程,而不仅仅是 19.exe.

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.

这篇关于无法使用 ProcessBuilder 从 java 执行 exe 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆