在 Jar 文件中运行 exe 文件

时间:2021-05-11 04:27:23

标签: java

我在下面运行此代码,并尝试使用 stockfish exe。当使用文件路径运行时,它虽然不在 jar 文件中工作。当我使用 URL 时,它不起作用。我的其他文件路径工作只是这个没有。我认为这与它是一个 exe 并使用 processbuilder 有关。

public Engine()
{
     builder = new ProcessBuilder(getClass().getResource("/cpt_chess/sf.exe"));
     builder.redirectErrorStream(true);
}


public void start_process()
{
    try 
    {
        process = builder.start();
        
    } 
    catch (IOException e) 
    {System.out.print("error");
    }
}



public void read () 
{
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
    new Thread ()
    {
        public void run ()
        {
            try {
                String line =  stdInput.readLine();
             
                while (line != null)
                {
                    if (line.contains("bestmove"))
                    {
                        test_chess.engine_move = line;
                    }
                    line =  stdInput.readLine();
                }
            } catch (IOException ex) {
                Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }.start();
}

1 个答案:

答案 0 :(得分:2)

你不能。 Windows 不允许您直接从 ZIP/JAR 文件执行文件。

您可以做的是解压缩这些文件。使用 System.getProperty("java.io.tmpdir") 获取临时文件文件夹的路径,然后将这些文件复制到那里。您可以使用 Class.getResourceAsStream("/qemu.exe") 等来检索 InputStreams 给它们。

相关问题