private static File extractFromJar ( String resource , String fileName , String suffix ) throws IOException { final URL res = Main . class . getResource ( resource ) ; // put this jar in a file system so that we can load jars from there final File tmp ; try { tmp = File . createTempFile ( fileName , suffix ) ; } catch ( final IOException e ) { final String tmpdir = System . getProperty ( "java.io.tmpdir" ) ; throw new IllegalStateException ( "JavaMelody has failed to create a temporary file in " + tmpdir , e ) ; } final InputStream is = res . openStream ( ) ; try { final OutputStream os = new FileOutputStream ( tmp ) ; try { copyStream ( is , os ) ; } finally { os . close ( ) ; } } finally { is . close ( ) ; } tmp . deleteOnExit ( ) ; return tmp ; }
Extract a resource from jar mark it for deletion upon exit and return its location .