Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java runtime.exec无法正确执行_Java_Windows Server 2008 R2_Runtime.exec_Java 6 - Fatal编程技术网

Java runtime.exec无法正确执行

Java runtime.exec无法正确执行,java,windows-server-2008-r2,runtime.exec,java-6,Java,Windows Server 2008 R2,Runtime.exec,Java 6,我得到一个exe文件,我必须在WindowsServer2008R2上使用Java(版本6)执行。现在有一个问题我不太明白。使用命令行执行文件时 "C:\test.exe param1 param2" 它工作正常,但当我使用 Process proc = Runtime.getRuntime().exec("C:\\test.exe param1 param2"); proc.waitFor(); 我可以在windows任务管理器中看到test.exe,它开始运行(它创建了一个日志,其中说

我得到一个exe文件,我必须在WindowsServer2008R2上使用Java(版本6)执行。现在有一个问题我不太明白。使用命令行执行文件时

"C:\test.exe param1 param2" 
它工作正常,但当我使用

Process proc = Runtime.getRuntime().exec("C:\\test.exe param1 param2");
proc.waitFor();
我可以在windows任务管理器中看到test.exe,它开始运行(它创建了一个日志,其中说明了这一点),但是它不再做任何事情。test.exe以0%无休止地运行,我必须手动终止该进程。执行此操作后,java程序继续并

proc.exitValue()
是“1”,因此java认识到我已经终止了进程。我还尝试在批处理文件中编写命令行,并使用.exec()执行它,但它没有改变任何东西

真正让我困惑的是,它可以通过windows命令行完美运行,但不能通过.exec()运行。有人知道是什么导致了这样的问题吗?或者更有可能是test.exe导致了问题

致意

编辑:在.exec中写下错误的路径

"C:\test.exe param1 param2"
你有一张账单在里面。试试这个:

"C:\\test.exe param1 param2"

如果进程在stdout或stderr上生成任何输出,则需要使用它。否则它可能会阻塞。

由于您的程序处理大量输出,我的假设是,它无法写入标准输出(Linux下是一个管道,Windows不知道)

试试这个:

"C:\\test.exe param1 param2"
final byte[] devnull = new byte[1024];

final ProcessBuilder builder = new ProcessBuilder("C:\\test.exe", "param1", "param2")
    .redirectErrorStream(true);
final Process p = builder.start();

final InputStream stdout = process.getInputStream();

// Purge stdout
while (stdout.read[devnull] != -1);

// Grab the process' exit code here
正如fge在中所指出的,重要的是使用进程生成的所有输出—不仅在Linux上,而且在Windows上,不仅是标准输出,还有可能的错误

这方面的一般模式可能如下所示:

private static void runCommand(String command) throws IOException
{
    Process process = Runtime.getRuntime().exec(command);
    String errorMessage = 
        new String(toByteArray(process.getErrorStream()));
    String outputMessage = 
        new String(toByteArray(process.getInputStream()));
    int exitValue = 0;
    try
    {
        exitValue = process.waitFor();
    }
    catch (InterruptedException e)
    {
        Thread.currentThread().interrupt();
    }
    System.out.println("Output message: "+outputMessage);
    System.out.println("Error message: "+errorMessage);
    System.out.println("Exit value: "+exitValue);
}

private static byte[] toByteArray(
    InputStream inputStream) throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buffer[] = new byte[8192];
    while (true)
    {
        int read = inputStream.read(buffer);
        if (read == -1)
        {
            break;
        }
        baos.write(buffer, 0, read);
    }
    return baos.toByteArray();
}

也许可以帮助你,似乎是一个类似的问题。当你在控制台上运行这个程序时,它会在控制台中产生输出吗?或者它是从标准输入读取的?在继续之前,请使用
ProcessBuilder
test.exe
是否输出大量文本?可能是它在写入输出时被阻止了,而您似乎没有从中读取。是的,它确实在控制台和log.txt中生成了大量输出。减少控制台中的输出是否有帮助(不过,我必须联系test.exe-program的程序员才能这样做)?对不起,我尝试将问题分解为最少的代码,并为问题复制了错误的exec()-字符串。在java程序中,路径就像您编写的一样