Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 860
shawn Avatar asked Sep 04 '12 08:09

shawn


People also ask

Can I run an executable JAR file?

Most JAR files are simply containers for data that another program needs to run with Java; therefore you cannot run these files and nothing will happen when you double-click them. Similarly, most executable JAR files are downloaded as installation files to install applications or programs.

How do I make a JAR file executable?

Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.

Why are my JAR files not executing?

If you can't run jar files on your Windows 10 PC, you might need to reinstall Java Runtime Environment. When JAR files are not opening, another great method is to simply use a universal file opener. Alternatively, some reported that turning your JAR file into a BAT file worked perfectly.


2 Answers

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.
like image 137
Santosh Avatar answered Oct 05 '22 17:10

Santosh


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

like image 26
Adam Avatar answered Oct 05 '22 17:10

Adam