简体   繁体   中英

How do I execute a method from a library jar on a different JRE than my current project's JRE

I have a project 1 which need to run on 1.7, this project has a dependency on another project 2 built on 1.8.

I am trying to invoke a class from Project 2 inside Project 1, but I am getting a known error which is fixed in 1.8. I f I run this class individually on Project 2 in JRE 1.8, it works fine.

Any leads to chose the target JRE dynamically for a method call?

If you are willing to make a jar and use a wrapper for each program, you can bundle a specific jre with each executable.

  1. Download and run Launch4j

  2. In your eclipse project, click on:

    File -> export -> runnable jar file -> next

    And select the run configuration for the project you'd like to extract. Hit Finish

  3. It's a good idea to test each jar and make sure it runs. Double click, or find it with cmd and do java -jar nameOfJarToTest.jar .

  4. Make a subfolder for your jre. I just called mine "jre".

  5. Copy and paste your bin and lib folders from the correct jre into that folder you just made. Mine are located in a place like C:\\Program Files\\Java\\jre1.8.0_201 if you wanted to run java 8. To be extra clear, your file structure should be

someContainerFolder(folder you just made)

  nameOfJarToTest.jar jre(folder you just made) bin(folder copied from the jre in your file system) lib(folder copied from the jre in your file system) 
  1. Run Launch4j. Add the .jar and select an output file path. Fields pictured below 在此处输入图片说明

  2. Click on the jre tab and add your respective jre in the min jre field. Make sure you select the option Only use private jdk runtimes . Type "jre"(if you named the folder with 'bin' and 'lib' jre) in the "bundled jre path" field pictured below.

在此处输入图片说明

  1. Click on 'build wrapper'. It looks like a settings cog. 在此处输入图片说明

If you do this for both jar files, and make sure the 'bin' and 'lib' folders are from the jre you want to run, the two programs will both run fine in different runtime environments. If you would like one program to be able to call the other, I found the geeksforgeeks website has a very simple and concise way for you to call external executable files from your code if you're okay with canonical paths.

This is not possible, if your project depends on 1.7 it's dependencies should also be 1.7. At runtime all code executes in the same JVM, there is no way to dynamically 'choose' a target JRE.

If you really have no other option and the two libraries should work together, you could possibly build some kind of bridge between project 1 and 2, where project 1 invokes project 2 in a separate JVM instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM