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)?
Asked
Active
Viewed 876 times
5
-
1Any execution starts from a classloaders which load the class files from a `URL` and further process it. First hand, what you wish to accomplish seems impossible but not sure if any classloader hack will do the trick. – Santosh Sep 04 '12 at 09:03
2 Answers
8
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.

Santosh
- 17,667
- 4
- 54
- 79
2
The easiest approach would be to write it to a ramdisk and avoid the in-memory idea completely.

Adam
- 5,215
- 5
- 51
- 90
-
+1 You can use custom class loaders to do this but thats 100x harder than writing to a file. – Peter Lawrey Sep 04 '12 at 09:11
-
You can't do it in Java unless you have a whole load of security permissions to do stuff like edit the system mount points. In linux you could possibly do it by executing an external script but ideally you would have it set up by your server's boot scripts. I used this link http://www.kevingunn.org/?p=210 to do that in ubuntu. – Adam Sep 04 '12 at 09:42