Eclipse: export a Runnable JAR file to be executed on an older JRE

旧城冷巷雨未停 提交于 2019-12-10 13:58:10

问题


Exporting a Runnable JAR file from Eclipse is by far the quickest way to launch an Eclipse project from command line on another machine. See this answer.

A problem is that if the JRE installed on the machine you want to run from is older than the JDK used by Eclipse, you get this error:

java.lang.UnsupportedClassVersionError: test_hello_world : Unsupported major.minor version 51.0

I know that on Eclipse I can build the project with a lower compliance level (e.g., 1.6 instead of 1.7), but this does not seem to affect an exported JAR file.

Any idea on how to export a Runnable JAR file to be executed on an older JRE?


回答1:


This procedure seems to work for me (I tested with Java 1.2 and Eclipse Juno):

  • Create a new project.
  • Set the execution environment to J2SE-1.2.
  • Create a main class that prints one line to System.out.
  • Run the project.
  • Export the runnable jar.
  • Run the jar in JRE 1.2.



回答2:


By using Maven you can specify any version of JRE. And in result you will get jar file build to specified version of JRE. For example:

<plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
</plugins>



回答3:


Changing the compliance level should work. You have to perform a rebuild of your project. When you export a Jar eclipse do not compile your project just for package it, it usees the already compiled files.




回答4:


Change your "Java Compiler" settings in eclipse. Make compiler compliance level to 1.6.

How can I build a jar for a previous Java version?

Then export the 'Runnable Jar'. It will work !!




回答5:


I changed the compliance level and then also had to do the following:

  • Right click each project and open properties->Java Build Path->Libraries
  • Remove the JDK you don't want, in my case JDK 8
  • Add Library -> JRE System Library -> Select the version you want, in my case 7
  • Java Complier -> Set compliance level to the level you want, in my case 7
  • Click ok, repeat as needed.


来源:https://stackoverflow.com/questions/16730386/eclipse-export-a-runnable-jar-file-to-be-executed-on-an-older-jre

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