Deprecate in Java 1.6

2020-07-19 23:38发布

In Java 1.5, to deprecate a method you would:

@Deprecated int foo(int bar) {
}

Compiling this in Java 1.6 results in the following:

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

Any ideas?

9条回答
forever°为你锁心
2楼-- · 2020-07-20 00:13

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.

查看更多
我想做一个坏孩纸
3楼-- · 2020-07-20 00:14

@Deprecated not @Deprecate

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

查看更多
SAY GOODBYE
4楼-- · 2020-07-20 00:16

You have to tell the compiler to use 1.6:

javac -source 1.6

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

查看更多
Anthone
5楼-- · 2020-07-20 00:24

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

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

If you're using javac, run

javac -version

to check what version you're really using.

查看更多
forever°为你锁心
6楼-- · 2020-07-20 00:25

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

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 Settings at the top of this preference page. The link takes you to the project's own settings.

You can navigate there from the main menu, too. In this case Project > Properties > Java Compiler. There is a check box labelled Enable Project Settings and 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.

This should fix the syntax error.

查看更多
啃猪蹄的小仙女
7楼-- · 2020-07-20 00:26

If you are using Eclipse IDE then

1- Select your project in Project Explorer

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

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

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

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

5- Press the 'Apply' button.

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.

查看更多
登录 后发表回答