shawn
shawn

Reputation: 4223

Executing an in memory jar

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)?

Upvotes: 5

Views: 920

Answers (2)

Santosh
Santosh

Reputation: 17923

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.

Upvotes: 8

Adam
Adam

Reputation: 5445

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

Upvotes: 2

Related Questions