Search code examples
javahadoopjarexecutableelastic-map-reduce

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


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).

The why, is that I would like to use a Java wrapper around the ImageMagick executable as component of an image processing Elastic Map Reduce job. EMR only expects to take a jar file, so I don't think there's any room to install software on the data nodes that spin up.


Solution

  • 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();