在 Java 1.6 中弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1790957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:59:43  来源:igfitidea点击:

Deprecate in Java 1.6

javajavadocdeprecatedjdk1.6

提问by Ian Bishop

In Java 1.5, to deprecate a method you would:

在 Java 1.5 中,要弃用一个方法,您将:

@Deprecated int foo(int bar) {
}

Compiling this in Java 1.6 results in the following:

在 Java 1.6 中编译它会产生以下结果:

Syntax error, annotations are only available if source level is 1.5

语法错误,注释仅在源级别为 1.5 时可用

Any ideas?

有任何想法吗?

回答by gustafc

You have to tell the compiler to use 1.6:

你必须告诉编译器使用 1.6:

javac -source 1.6

Or equivalent for your IDE/build system (as others have suggested).

或等效于您的 IDE/构建系统(正如其他人所建议的那样)。

回答by Bozho

First, its @Deprecated, and second - double-check if you are really compiling with Java 1.6. What are you using? javac? Eclipse?

首先是它@Deprecated,其次是仔细检查您是否真的使用 Java 1.6 进行编译。你在用什么?爪哇?蚀?

If using javac, make sure your JAVA_HOME is set to point to JDK 6, not JDK 1.4.2 If using Eclipse (or any IDE), right click the project > properties > and search for compiler level.

如果使用 javac,请确保您的 JAVA_HOME 设置为指向 JDK 6,而不是 JDK 1.4.2 如果使用 Eclipse(或任何 IDE),请右键单击项目 > 属性 > 并搜索编译器级别。

回答by BalusC

Syntax error, annotations are only available if source level is 1.5

语法错误,注释仅在源级别为 1.5 时可用

This is a typical IDE error message. You've configured the workspace or the project to use compliance level 1.4 or older. Assuming that it's Eclipse (it's at least recognizeable as an Eclipse error), then you need to go to Java> Compilerin workspace preferences and set the Compiler compliance levelto 1.5 or newer. You need to check this in the Java Compilerentry in Project's properties as well.

这是典型的 IDE 错误消息。您已将工作区或项目配置为使用合规性级别 1.4 或更早版本。假设它是 Eclipse(至少可以识别为 Eclipse 错误),那么您需要在工作区首选项中转到Java> Compiler并将编译器合规性级别设置为 1.5 或更高版本。您还需要在项目属性中的Java 编译器条目中进行检查。

回答by Imran Haidry

If you are using Eclipse IDE then

如果您使用的是 Eclipse IDE,那么

1- Select your project in Project Explorer

1- 在 Project Explorer 中选择您的项目

2- Go to Project -> Properties -> Java Compiler

2- 转到项目 -> 属性 -> Java 编译器

3- Check the option for 'Enable project specific settings'

3-选中“启用项目特定设置”选项

4- Set the 'Compiler compliance level' to '1.6'

4-将“编译器合规级别”设置为“1.6”

NOTE: If already set to 1.6 then change it to 1.5.

注意:如果已设置为 1.6,则将其更改为 1.5。

5- Press the 'Apply' button.

5- 按“应用”按钮。

There are issues with the IDE and at times it just doesn't pick up the default selected compiler compliance level. Therefore you have to toggle it and press the apply button for the changes to take effect.

IDE 存在问题,有时它只是不选择默认选择的编译器合规性级别。因此,您必须切换它并按下应用按钮才能使更改生效。

回答by Achim Schmitz

Having read the responses to date, I can see that there is some confusion as to what is happening where Eclipse is involved.

阅读了迄今为止的答复后,我可以看到对于 Eclipse 所涉及的情况存在一些混淆。

I had the same syntax error, checked workspace Java compliance (Window > Preferences > Java > Compiler) and was surprised to see a complier compliance level of 1.6. However, I noticed the link Configure Project Specific Settingsat the top of this preference page. The link takes you to the project's own settings.

我遇到了相同的语法错误,检查了工作区 Java 合规性(Window > Preferences > Java > Compiler)并且惊讶地看到编译器合规性级别为 1.6。但是,我注意到此首选项页面顶部的“配置项目特定设置”链接。该链接会将您带到项目自己的设置。

You can navigate there from the main menu, too. In this case Project > Properties > Java Compiler. There is a check box labelled Enable Project Settingsand in my case this was checked and the setting was 1.4, though I do not remember setting it explicitly. Anyway, you can either let the compliance setting to default to that of the workbench or change the project setting to 1.5 or higher.

您也可以从主菜单导航到那里。在这种情况下Project > Properties > Java Compiler。有一个标记为“启用项目设置”的复选框,在我的情况下,它被选中并且设置为 1.4,但我不记得明确设置它。无论如何,您可以让合规性设置默认为工作台的合规性设置,也可以将项目设置更改为 1.5 或更高版本。

This should fix the syntax error.

这应该修复语法错误。

回答by Jon Skeet

I suspect you've got your source level set to lowerthan 1.5. It should be fine in Java 6 in general.

我怀疑您的源级别设置为低于1.5。一般来说,在 Java 6 中应该没问题。

How are you compiling? If it's with Eclipse, what do your project/workspace settings say under Compiler / JDK Compliance Level?

你是怎么编译的?如果它与 Eclipse 一起使用,您的项目/工作区设置在编译器/JDK 合规性级别下是什么意思?

If you're using javac, run

如果您使用的是 javac,请运行

javac -version

to check what version you're really using.

检查您真正使用的版本。

回答by vincent mathew

This can occur even if java 1.6 is used in eclipse. Click the project and then right click it. Go to properties and in Java compiler section first check enable project specific then manually select 1.6 version even if it is already there by default. This fixed my problem.

即使在 eclipse 中使用 java 1.6,也会发生这种情况。单击项目,然后右键单击它。转到属性并在 Java 编译器部分首先检查启用项目特定然后手动选择 1.6 版本,即使它默认已经存在。这解决了我的问题。

回答by Grzegorz Oledzki

Are you sure you are compiling with Java 1.6 not 1.4 (or older)? What compiler are you using?

您确定要使用 Java 1.6 而不是 1.4(或更早版本)进行编译吗?你用的是什么编译器?

回答by MarkPowell

@Deprecatednot @Deprecate

@Deprecated不是 @Deprecate

If you are using Eclipse, make sure the settings for the Java Compiler are set to 1.6 compliance.

如果您使用 Eclipse,请确保 Java 编译器的设置设置为 1.6 合规性。