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?
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).
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.
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.
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.
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.
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.
Are you sure you are compiling with Java 1.6 not 1.4 (or older)?
What compiler are you using?
#Deprecated not #Deprecate
If you are using Eclipse, make sure the settings for the Java Compiler are set to 1.6 compliance.
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.
Related
It seems like this should be a simple task, with the options in the Preferences menu for different JREs and the ability to set different compiler and build paths per project. However, it also seems to simply not work.
For example, I have my JAVA_HOME set to a jre for Java 1.6. It's still not clear to me how Eclipse uses this, but it appears to be defaulting to this and not taking the project overrides. I have also installed Java 1.5, and added a JRE for this in eclipse in the Java->Installed JREs section.
In my project, I've set the compiler compliance level to 1.5. In the build path for the project, I've added the System Library for the Java 1.5 JRE. However, I'm getting compile errors for a class that implements PreparedStatement for not implementing abstract methods that only exist in Java 1.6 PreparedStatement. Specifically, the methods
setAsciiStream(int, InputStream, long) and
setAsciiStream(int, InputStream)
Strangely enough, it worked when we were compiling it against Java 1.4, which it was originally written for. We added the JREs for Java 1.4 and referenced that system library in the project, and set the project's compiler level to 1.4, and it works fine. But when I do the same changes to try to point to Java 1.5, it instead uses 1.6.
Any ideas why?
From the menu bar:
Project -> Properties -> Java Compiler
Enable project specific settings (checked)
Uncheck "use Compliance from execution environment '....
Select the desired "compiler compliance level"
That will allow you to compile "1.5" code using a "1.6" JDK.
If you want to acutally use a 1.5 JDK to produce "1.5" compliant code, then install a suitable 1.5 JDK and tell eclipse where it is installed via:
Window -> preferences -> Installed JREs
And then go back to your project
Project -> properties -> Java Build Path -> libraries
remove the 1.6 system libaries, and:
add library... -> JRE System LIbrary -> Alternate JRE -> The JRE you want.
Verify that the correct JRE is on the project's build path, save everything, and enjoy!
Eclipse uses it's own internal compiler that can compile to several Java versions.
From Eclipse Help > Java development user guide > Concepts > Java Builder
The Java builder builds Java programs using its own compiler (the Eclipse Compiler for Java) that implements the Java Language Specification.
For Eclipse Mars.1 Release (4.5.1), this can target 1.3 to 1.8 inclusive.
When you configure a project:
[project-name] > Properties > Java Compiler > Compiler compliance level
This configures the Eclipse Java compiler to compile code to the specified Java version, typically 1.8 today.
Host environment variables, eg JAVA_HOME etc, are not used.
The Oracle/Sun JDK compiler is not used.
First off, are you setting your desired JRE or your desired JDK?
Even if your Eclipse is set up properly, there might be a wacky project-specific setting somewhere. You can open up a context menu on a given Java project in the Project Explorer and select Properties > Java Compiler to check on that.
If none of that helps, leave a comment and I'll take another look.
Just to clarify, do you have JAVA_HOME set as a system variable or set in Eclipse classpath variables? I'm pretty sure (but not totally sure!) that the system variable is used by the command line compiler (and Ant), but that Eclipse modifies this accroding to the JDK used
The compiler level used to autocomplete my Java code in Eclipse Neon is 1.8 (proof is it suggests me to use ResourceBundle::getBaseBundleName() for example). I do use a 1.8 JDK but the workspace Java compiler compliance level is 1.7 and project specific settings aren't enabled. How come and how do I change that?
I am having some trouble updating the compiler compliance of my eclipse workspace (I am actually using RTC, but it is essentially eclipse).
Currently, it uses a jdk of 1.6 so I can change the JDK compliance to a limit of 1.6.
However, I am using jdk 1.7 for my project -I have added it as a JRE -
preferences --> Java --> Installed JRE --> added jre with the jdk 1.7 directory.
However, even though I use this as the default JRE it does not allow me to change the compiler compliance level to 1.7.
I am guessing that there is something I am missing.
Any help would be greatly appreciated
The Compiler compliance level is Version Dependent in eclipse. If you are using Eclipse 3.7 or lower then you won't get 1.7 compliance. Use Eclipse JUNO or higher.
For more on JDT Core 7 and its release related information
UPDATE:
Even eclipse 3.7.1 version supports Java 7 features and compliance level.
I use eclipse 3.6.1 with installed JRE 1.7 and I was able to set Java compliance level to 1.7 (on the workspace level - preferences).
In some workspaces I could do that right in the Preferences, in some workspaces I had to do that in a "hard way" updating several properties in the file:
.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.core.prefs
Here are changed lines:
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
Maybe it was just enough to change the line with "org.eclipse.jdt.core.compiler.compliance" property , but I did all three for the sake of completeness and consistency.
If you are using eclipse 3.7 then you add 1.7 into compliance level by following way:-
right click on project
click on Build path->Configure Build Path->project Facets
there you can see one link 'convert to faceted form'. click on it
mark java and select verion 1.7 from dropdown which is present in front of java tab
click on apply and click ok
now you can see that 1.7 version is appeared in compliance level
Note:- Before above step you have to add JRE System library in Build path for jdk1.7
Right click on project you have in use, click on Properties,
enter image description here
go on "Compiler compilance level", and select 1.8
enter image description here
Select your project, then go to Project -> properties.
In Properties -> Java Compiler.
Under JDK Compliance if "use compliance from execution environment " is checked then uncheck it and you would see a dropdown in from of Compiler Compliance Level. Chose your desired version from here.
Click on Apply and Close.
Hopefully, this would resolve the issue.
It seems like this should be a simple task, with the options in the Preferences menu for different JREs and the ability to set different compiler and build paths per project. However, it also seems to simply not work.
For example, I have my JAVA_HOME set to a jre for Java 1.6. It's still not clear to me how Eclipse uses this, but it appears to be defaulting to this and not taking the project overrides. I have also installed Java 1.5, and added a JRE for this in eclipse in the Java->Installed JREs section.
In my project, I've set the compiler compliance level to 1.5. In the build path for the project, I've added the System Library for the Java 1.5 JRE. However, I'm getting compile errors for a class that implements PreparedStatement for not implementing abstract methods that only exist in Java 1.6 PreparedStatement. Specifically, the methods
setAsciiStream(int, InputStream, long) and
setAsciiStream(int, InputStream)
Strangely enough, it worked when we were compiling it against Java 1.4, which it was originally written for. We added the JREs for Java 1.4 and referenced that system library in the project, and set the project's compiler level to 1.4, and it works fine. But when I do the same changes to try to point to Java 1.5, it instead uses 1.6.
Any ideas why?
From the menu bar:
Project -> Properties -> Java Compiler
Enable project specific settings (checked)
Uncheck "use Compliance from execution environment '....
Select the desired "compiler compliance level"
That will allow you to compile "1.5" code using a "1.6" JDK.
If you want to acutally use a 1.5 JDK to produce "1.5" compliant code, then install a suitable 1.5 JDK and tell eclipse where it is installed via:
Window -> preferences -> Installed JREs
And then go back to your project
Project -> properties -> Java Build Path -> libraries
remove the 1.6 system libaries, and:
add library... -> JRE System LIbrary -> Alternate JRE -> The JRE you want.
Verify that the correct JRE is on the project's build path, save everything, and enjoy!
Eclipse uses it's own internal compiler that can compile to several Java versions.
From Eclipse Help > Java development user guide > Concepts > Java Builder
The Java builder builds Java programs using its own compiler (the Eclipse Compiler for Java) that implements the Java Language Specification.
For Eclipse Mars.1 Release (4.5.1), this can target 1.3 to 1.8 inclusive.
When you configure a project:
[project-name] > Properties > Java Compiler > Compiler compliance level
This configures the Eclipse Java compiler to compile code to the specified Java version, typically 1.8 today.
Host environment variables, eg JAVA_HOME etc, are not used.
The Oracle/Sun JDK compiler is not used.
First off, are you setting your desired JRE or your desired JDK?
Even if your Eclipse is set up properly, there might be a wacky project-specific setting somewhere. You can open up a context menu on a given Java project in the Project Explorer and select Properties > Java Compiler to check on that.
If none of that helps, leave a comment and I'll take another look.
Just to clarify, do you have JAVA_HOME set as a system variable or set in Eclipse classpath variables? I'm pretty sure (but not totally sure!) that the system variable is used by the command line compiler (and Ant), but that Eclipse modifies this accroding to the JDK used
Is there any way to restrict a java project configured with 1.4 compiler compliance level from using 1.5/1.6 specific API where the system is playing with 1.6 JRE?
Setting only 1.4 compiler compliance level on my project does not warn me from using especially 1.5/1.6 specific java.lang.String methods.
Make sure that you're also using an 1.4 JRE system library in your project. The 1.4 compiler settings change the features which will be written into the class files, it doesn't restrict what you can see from the libraries in your project.
Open the properties for your project and check what you find under "Java Build Path" -> "Libraries" -> "JRE System Library". Click "Edit" to change or add new Java versions.
I would suggest the safest (and perhaps the simplest) way is to compile using a JDK 1.4, and run using a JRE 1.6.
You will need the rt.jar from a 1.4 JRE (for instance by having the 1.4 JRE installed). If using javac use the following options:
javac -source 1.4 -target 1.4 -bootclasspath /path/to/j2se1.4/lib/rt.jar [...]
Using javac from a later JRE means that it should have fewer bugs for old source, although it may not necessarily be entirely "bug compatible".
You might also want to use -Djava.ext.dirs=directories and -Djava.endorsed.dirs, but putting things in those directories is generally a bad idea.
Note: Even 1.5 has finished its End Of Service Life period. Get with 1.6! (Or buy one of our excellent Java for Business contracts...)
What I would do is to remove those JDKs/JREs, which you don't want to use, from Eclipse's settings. In your case that would be 1.5 and 1.6.
To do this, go to Windows->Preferences and then type "JRE" in the top left hand side.
I would also check which JRE Eclipse in launched in and maybe edit your eclipse.ini.