问题
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?
回答1:
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).
回答2:
First, its @Deprecated
, and second - double-check if you are really compiling with Java 1.6. What are you using? javac? Eclipse?
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.
回答3:
Syntax error, annotations are only available if source level is 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 > Compiler in workspace preferences and set the Compiler compliance level to 1.5 or newer. You need to check this in the Java Compiler entry in Project's properties as well.
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
Are you sure you are compiling with Java 1.6 not 1.4 (or older)? What compiler are you using?
回答8:
@Deprecated
not @Deprecate
If you are using Eclipse, make sure the settings for the Java Compiler are set to 1.6 compliance.
回答9:
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.
来源:https://stackoverflow.com/questions/1790957/deprecate-in-java-1-6