Executing an in memory jar

邮差的信 提交于 2019-12-10 14:41:11

问题


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.



来源:https://stackoverflow.com/questions/12260016/executing-an-in-memory-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!