Executing an in memory jar

2019-06-23 03:18发布

问题:

Suppose I have a java process that is receiving a runnable jar file from a trusted process in the form of a byte [], is there a way to invoke it without having to write the jar file to disk and then invoke it(start a new process that is running the jar)?

回答1:

Here is one way you can accomplish it:

  1. Create a ByteArrayInputStream from the byte [] received.
  2. Now use JarInputStream to create in-memory representation of the jar file.

    ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); JarInputStream jis = new JarInputStream(bis);

  3. This way you have the jar loaded in the memory.

  4. Now you can use custom classloaders to further process it. Here is one example you can refer to.


回答2:

The easiest approach would be to write it to a ramdisk and avoid the in-memory idea completely.