Eclipse Updating the compiler compliance to 1.7 - java

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.

Related

I want to use different JDK version on eclipse [duplicate]

I have two JDKs, for Java 6 and 7.
I want to build my project using both. Initially we only built against 1.6.
I see in my project setting I can select 1.5, 1.6 1.7 as the compiler level.
How are these options added to the IDE? I never installed Java 1.5.
Suppose I wanted Java 1.4. How would I get this to appear in the list? I have added two different JREs in my IDE preferences, but these are not what appears in the dropdown.
You manage the list of available compilers in the Window -> Preferences -> Java -> Installed JRE's tab.
In the project build path configuration dialog, under the libraries tab, you can delete the entry for JRE System Library, click on Add Library and choose the installed JRE to compile with. Some compilers can be configured to compile at a back-level compiler version. I think that's why you're seeing the addition version options.
Some additional steps may be needed to set both the project and default workspace JRE correctly, as MayoMan mentioned. Here is the complete sequence in Eclipse Luna:
Right click your project > properties
Select “Java Build Path” on left, then “JRE System Library”, click Edit…
Select "Workspace Default JRE"
Click "Installed JREs"
If you see JRE you want in the list select it (selecting a JDK is OK too)
If not, click Search…, navigate to Computer > Windows C: > Program Files > Java, then click OK
Now you should see all installed JREs, select the one you want
Click OK/Finish a million times
Easy.... not.
To tell eclipse to use JDK, you have to follow the below steps.
Select the Window menu and then Select Preferences. You can see a dialog box.
Then select Java ---> Installed JRE’s
Then click Add and select Standard VM then click Next
In the JRE home, navigate to the folder you’ve installed the JDK (For example, in my system my JDK was in C:\Program Files\Java\jdk1.8.0_181\ )
Now click on Finish.
After completing the above steps, you are done now and eclipse will start using the selected JDK for compilation.
Configuring JDKs
Windows -> Preferences -> Installed JREs, to configured the installed JDKs
Project Properties, Java Compiler, Enable project specific settings (or configure Workspace settings), JDK Compliance
Project Properties, Java Build Path, Libraries, Add Library, JRE system library, Workspace default or Alternate JRE (one of the JREs configured in
Maven
BUT IF you are using maven, provided that you have your latest JRE (Windows/Preferences/Installed JREs) -for example JDK 1.8
You can select the level 1.6, 1.7, 1.8 by configuring the maven-compiler-plugin source and target attributes, like this
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
And ideally, if you have a parent pom, you can do it for all the modules (Eclipse projects) in the parent pom, in one single place.
Source and Target
If we want to use the Java 8 language features the –source should be set to 1.8. Also, for the compiled classes to be compatible with JVM 1.8, the –target value should be 1.8.
Updating JRE library that is broken in many projects at once (with Maven)
Rather than updating one by one the JRE library, let Maven do it for you.
Selecting the projects and right-clicking for Maven -> Update Project, will set the system library to the path of the installed JDK, in case the paths are broken (because you installed a new JDK or imported from another computer, etc.) and set the JDK compliance according to the maven source and target setting in the pom.
Eclipse's compiler can assure that your java sources conform to a given JDK version even if you don't have that version installed. This feature is useful for ensuring backwards compatibility of your code.
Your code will still be compiled and run by the JDK you've selected.
JDK 1.8 have some more enrich feature which doesn't support to many eclipse .
If you didn't find java compliance level as 1.8 in java compiler ,then go ahead and install the below eclipse 32bit or 64 bit depending on your system supports.
Install jdk 1.8 and then set the JAVA_HOME and CLASSPATH in
environment variable.
Download eclipse-jee-neon-3-win32 and unzip : supports to java 1.8
Or download Oracle Enterprise Pack for Eclipse (12.2.1.5) and unzip
:Supports java 1.8 with 64 bit OS
Right click your project > properties
Select “Java Compiler” on left and set java compliance level to 1.8
[select from the dropdown 1.8]
Try running one java program supports to java 8 like lambda
expression as below and if no compilation error ,means your eclipse
supports to java 1.8, something like this:
interface testI{
void show();
}
/*class A implements testI{
public void show(){
System.out.println("Hello");
}
}*/
public class LambdaDemo1 {
public static void main(String[] args) {
testI test ;
/*test= new A();
test.show();*/
test = () ->System.out.println("Hello,how are you?"); //lambda
test.show();
}
}

Eclipse says jre does not support the current compiler level of 1.8 but it is set at 1.7

I installed Eclipse mars with java JDK8 but was was having lots of problems with it so I uninstalled it changed back to JDK7 and now I'm getting a strange compiler error in a few of my projects. I noticed that when I went into, Windows->Preferences-> Java->Installed JREs, there is an error message at the top saying that "The selected JRE does not support the ...rent compiler compliance level of 1.8" but when I check the compiler compliance level its set to 1.7
Ok so I decided to try starting a new project and when trying to build it a warning came up that my workspace compliance level was at level 1.8...changing the workspace fixed the problem. So new is it possible to change the workspace compliance level or should I just import my projects into a new workspace?
I tried importing into a new workspace,same problem. So I started new project in a new workspace, copyed and pasted the code in a voila it works! Very annoying if I have to do that for every class in every project I want.
Check your compliance settings in:
Preferences > Java > Compiler
Make sure the default is set to 1.7 as it may have defaulted to 1.8 after the initial install/run.
Also check project-specific compliance settings in:
(Project) Properties > Java Compiler
I changed Windows ->Preference->Java->Installed JREs to jdk1.7.0_79 and right clicked the project, Properties ->Java Build Path, made sure that under Libraries tab, it is JRE System Library[JavaSE-1.7]. Clicked Project ->Clean. Right clicked the project again, Properties ->Java Compiler, checked Use compliance from execution environment 'JavaSE-1.7' on the 'Java Build Path'.Then it worked.
If you already changed all settings to 1.7 (both in the eclipse preferences and the project properties) check your project's build path. How? Easy, after you've imported your project, right-button click it and select Build path > Configure Build Path. Then, check if you have your JRE system library correctly set.
If even this is set up ok, I would say that you probably have some code that can only be compiled by jdk 8 and you're trying to use jdk7 compliance level, which eclipse warns you about. An example of this is using lambda functions, which can only be compiled with java 8 as far as I know.
Go to Windows >> Preferences>>Compiler >> Unselect default
compliance settings >> select the compiler compliance level to 1.8
This should reflect in generated .class files compatibility
as 1.8 and source compatibility as 1.8 Step Step
Apply it Step
now click on apply and close
clean and build the project.
I have similar issue with Mars on Pydev environment. Seems that Mars want java jre 1.7.0, so for me was work by copy inside(create if not avaiable) eclipse\jre\ folder and copy the 1.7.0 version of jre inside

Updating JDK in Eclipse

I have been having problems with appcompat_v7, someone told me to use Android-21 target. An error with that got me told to use JDK 1.8. I currently have JDK 1.6 installed. How do I switch Eclipse over to use JDK 1.8? I see no place to set the JDK path. The other time this question was asked, the questioner was referred to the JRE settings ... but the JDK is not the JRE, there are two separate paths. How do I get Eclipse to use the new JDK?
Project Menu > Properties menu item > Java Compiler > Compiler Compliance Level
Select your Java version from the pulldown menu. Obviously, you'll need to install Java 1.8 first to get it to show up as an installed version.

How to change JDK version for an Eclipse project

I need to write a project that's only compatible with Java 1.5. I have Java 1.6 installed. Is there some form of backwards compatibility to get Eclipse to compile with 1.5?
Do I have to install Java 1.5 to get it to show here? Or maybe there is another way?
.
Click on the Add Library button. It brings your screen to point to the Java location.
Select "Directory", button right besides JRE home and point to the installed folder location.
Even though you want to just 1.5 compiler project, you can achieve it by changing compiler settings in Eclipse instead of removing 1.6 JRE and add 1.5 JRE.
GOTO -->JAVA--Compiler---> and change compiler level to `1.5` instead of `1.6`
As davidfmatheson suggested,
Just be careful, especially if you're setting this up for a team of people to work on. If anyone uses anything that is new or changed in 1.6, it will compile, but not run in an environment with JRE 1.5.
Click on the Window tab in Eclipse, go to Preferences and when that window comes up, go to Java → Installed JREs → Execution Environment and choose JavaSE-1.5. You then have to go to Compiler and set the Compiler compliance level.
Right click project -> Properties -> Java Build Path -> select JRE System Library click Edit and select JDK or JRE after then click Java Compiler and select Compiler compliance level to 1.8
If you are using maven build tool then add the below properties to it and doing a maven update will solve the problem
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
In the preferences section under Java -> Installed JREs click the Add button and navigate to the 1.5 JDK home folder. Then check that one in the list and it will become the default for all projects:
Eclipse - specific Project change JDK Version -
If you want to change any jdk version of A specific project than you have to click ---> Project --> JRE System Library --> Properties ---> Inside Classpath Container (JRE System Library) change the Execution Environment to which ever version you want e.g. 1.7 or 1.8.
The JDK (JAVA_HOME) used to launch Eclipse is not necessarily the one used to compiled your project.
To see what JRE you can select for your project, check the preferences:
General → Java Installed JRE
By default, if you have not added any JRE, the only one declared will be the one used to launched Eclipse (which can be defined in your eclipse.ini).
You can add any other JRE you want, including one compatible with your project.
After that, you will need to check in your project properties (or in the general preferences) what JRE is used, with what compliance level:
(source: standartux.fr)
See the page Set Up JDK in Eclipse. From the add button you can add a different version of the JDK...
As I was facing this issue minutes ago, in case you are trying to open an existing project in an environment with a newer JDK, make sure you pdate the JDK version in Project Properties -> Project Facets -> Java.

Deprecate in Java 1.6

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.

Categories