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

后端 未结 1 1607
孤街浪徒
孤街浪徒 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" ), <filename> );
    InputStream  is =
       getClass().getClassLoader().getResourceAsStream( <path to rc> );
    OutputStream os = new FileOutPutStream( target );
    <copy is to os>
    Process p = new ProcessBuilder( target ).start();
    
    0 讨论(0)
提交回复
热议问题