简体   繁体   English

如何将我的 Eclipse 项目转换为早期的 Java 版本?

[英]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.我在 Eclipse 中有一个项目,它以前使用 JRE7 并引用了 JRE7 系统库。 I absolutely need it to now run in JRE6.我现在绝对需要它在 JRE6 中运行。 I have not used any Java 7 specific syntax so the source code itself is entirely compatible.我没有使用任何 Java 7 特定语法,因此源代码本身是完全兼容的。 Here is what I have already done:这是我已经做过的事情:

  • I Installed JDK6.我安装了JDK6。
  • I then went to Window > Preferences > Java > Installed JREs and set JRE6 as default.然后我转到Window > Preferences > Java > Installed JREs并将 JRE6 设置为默认值。
  • I configured the build path of my project to reference the JRE6 system library and not the JRE7 one.我将项目的构建路径配置为引用 JRE6 系统库而不是 JRE7 系统库。
  • Finally I went to Run Configurations > JRE and set it to run in JRE6.最后,我转到运行配置 > JRE并将其设置为在 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 JRE 与 project.class 文件兼容性不兼容: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.正如我之前提到的,该项目的实际代码与 Java 6 语法没有什么不同,因此它将在 JRE6 中运行。 So presumably I need to somehow recompile all the.class files from the source code.所以大概我需要以某种方式重新编译源代码中的所有 the.class 文件。 I thought Eclipse would do this automatically.我认为 Eclipse 会自动执行此操作。 Any ideas?有任何想法吗?

First, clean your project:首先,清理你的项目:

Project > Clean项目 > 清洁

If that doesn't fix things...如果那不能解决问题...

Second check your project specific java compiler:其次检查您的项目特定的 java 编译器:

Project > Properties > Java Compiler项目 > 属性 > Java 编译器

The second method provided by Colin worked for me. Colin 提供的第二种方法对我有用。

Here is a visual representation of all steps for that approach:以下是该方法所有步骤的直观表示:

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

I thought Eclipse would do this automatically.我认为 Eclipse 会自动执行此操作。 Any ideas?有任何想法吗?

No Eclipse does not clean all projects until you ask it to do so.除非您要求 Eclipse 这样做,否则 Eclipse 不会清理所有项目。 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.这可以通过在文件 project_directory/.settings/org.eclipse.jdt.core.prefs 中手动编辑项目首选项然后在 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更改项目->属性->Java 编译器->编译器合规级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Eclipse中更改“已安装的项目方面”的Java版本? - How do I change the Java version of my “installed project facet” in Eclipse? 如何将受版本控制的 Eclipse 中的本地 Java 项目推送到 GitHub? - How do I push my local Java project in Eclipse that is version controlled to GitHub? 如何将我的项目的新版本上传到SVN(eclipse)? - How do I upload a new version of my project to SVN (eclipse)? 如何在OSX上安装Java SDK的早期版本 - How do I install an earlier version of Java SDK on OSX 如何在 Eclipse 中向我的 Java 项目添加资源文件夹 - How do I add a resources folder to my Java project in Eclipse 如何在Eclipse中的项目中包含.class文件? (JAVA) - How do I include .class files in my project in Eclipse? (Java) 如何在 Eclipse 中同时构建我的自定义 java 包和项目? - How do I do in Eclipse to build my custom java package and project at the same time? 如何组织我的Java Eclipse项目,以便用户只需要进行一次导入? - How do I organize my Java Eclipse project so that the user then only needs to do one import? 如何将SVN项目作为Java项目签入Eclipse? - How do I check out an SVN project into Eclipse as a Java project? 如何在另一个项目中使用Eclipse OSGi Java插件? - How do I use my Eclipse OSGi Java plug-in in another project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM