Is it possible to jar up an executable so that it can be run from Java?

后端 未结 1 1619
孤街浪徒
孤街浪徒 2021-01-14 08:16

Simply put, I need to be able to stick a compiled executable inside a Java jar file and then be able to run it from Java (probably via ProcessBuilder).

1条回答
  •  無奈伤痛
    2021-01-14 08:39

    The executable into the jar is a resource, you may access it via a Stream and expand the executable to the TEMP directory, then execute it with ProcessBuilder.

    File target = new File( System.getProperty( "java.io.tmpdir" ),  );
    InputStream  is =
       getClass().getClassLoader().getResourceAsStream(  );
    OutputStream os = new FileOutPutStream( target );
    
    Process p = new ProcessBuilder( target ).start();
    

    0 讨论(0)
提交回复
热议问题