问题
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:
- Create a
ByteArrayInputStream
from thebyte []
received. Now use JarInputStream to create in-memory representation of the jar file.
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); JarInputStream jis = new JarInputStream(bis);
This way you have the jar loaded in the memory.
- 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.
来源:https://stackoverflow.com/questions/12260016/executing-an-in-memory-jar