简体   繁体   中英

How do I convert my Eclipse project to an earlier Java version?

I have a project in Eclipse which previously used JRE7 and referenced the JRE7 system libraries. I absolutely need it to now run in JRE6. I have not used any Java 7 specific syntax so the source code itself is entirely compatible. Here is what I have already done:

  • I Installed JDK6.
  • I then went to Window > Preferences > Java > Installed JREs and set JRE6 as default.
  • I configured the build path of my project to reference the JRE6 system library and not the JRE7 one.
  • Finally I went to Run Configurations > JRE and set it to run in JRE6.

Immediately after that last step, the top of the dialogue shows a message that says:

JRE not compatible with project.class file compatibility: 1.7

And when I run the project I get this error message:

java.lang.UnsupportedClassVersionError: ExampleProcessingApp : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:314)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:146)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:608)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:798)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:727)
    at sun.applet.AppletPanel.run(AppletPanel.java:380)
    at java.lang.Thread.run(Thread.java:679)

As I mentioned previously, the actual code of the project is no different from Java 6 syntax so it would run in JRE6. So presumably I need to somehow recompile all the.class files from the source code. I thought Eclipse would do this automatically. Any ideas?

First, clean your project:

Project > Clean

If that doesn't fix things...

Second check your project specific java compiler:

Project > Properties > Java Compiler

The second method provided by Colin worked for me.

Here is a visual representation of all steps for that approach:

在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述

I thought Eclipse would do this automatically. Any ideas?

No Eclipse does not clean all projects until you ask it to do so. It only cleans the generated class files of the sources you have modified. You should do a clean build of your solution, rebuild everything and run again, it should work just fine.

Thanks to all. Although it's solved, I would like to add another way of doing this to add another flavor.


It can be done by editing manually project preferences in file project_directory/.settings/org.eclipse.jdt.core.prefs and then refreshing the project in eclipse.

从 1.7 更改为 1.6

I've head a case where cleaning the project didn't solve the issue. I actually had to close and reopen the specific project to make it work.

Change Project->Properties->Java Compiler->Compiler Compliance Level

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