Compile Java Program in Eclipse with Older JRE Without Installing - java

I want my program to work for people with older versions of Java. My program doesn't need any special new features and it should be able to run fine in Java 6. I have Java 8 installed, and I want to know how I would go about compiling my program in Eclipse in an older version of Java without having to install Java 6.

In the Project Explorer view, right-click on the project and then select Properties.
Select the Java Compiler page in the in the Properties window.
In the JDK Compliance section, select the desired Compiler compliance level.
Click Apply and then Ok.
It will automatically compile to the right version
Note this doesn't actually change the version, just that the compiler would be able to enforce the rules that another Java version would. So it should be compatible
For cross verfiying if it is compiled properly with the lower version,
1. Go to properties
2. go to java build path
3. go to libraries,
4. click on JRE System Library-- double click this to see if exceution environment is properly set.

Related

Eclipse warning "when selecting 9 compliance make sure to have a compatible jre installed and activated..." only shows up in pre-existing projects

If I create a project in Eclipse from scratch, and I assign Java-9 as JRE :
And after that I go to project properties, I see the following message The system libraries from the selected release 9 will be used with '--release' compiler option using current JRE 10:
If I change an existing project of Java 8 to use Java 9, as the image shows:
And after that go to Properties - > Java Compiler, I see a different message, When selecting 9 compliance make sure to have a compatible jre installed and activated:
I don't understand why is there a different message in both scenarios, I believe that I'm doing the same, so I think that should I get the same message.
I want to switch the jdk of my existing project, so my question is: Is there any problem if I keep using the last project with that Warning? Or should I create a project from scratch in order to be sure that will work properly?
EDIT: List of Installed JREs:
Execution environment list:

How to compile Java 7-compatible code under JDK 9 (eclipse) [duplicate]

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

Can you define which Java Version your Java web application should use

Can you define anywhere in your Java Project which JRE/JDK version should it pick up?
In eclipse when I choose the project >> right click >> Java Compiler and check the "Compiler compliance level" I see a certain version marked (1.7,1.6 etc). How is this chosen?
Or is this entirely just dependent on what JDK/JRE runtime version your server (tomcat) is using?
Thanks
Short answer:
No, you can not identify the version of java that will be "picked up" at runtime.
More of an Answer:
There are three Java versions that come into play when building and running something using Java.
The source version. This is the version of Java to which the source of the project complies. When compiling, you can pass a "source" (try google search for "set java source level") parameter to identify this version. In practice, I don't know the value of this.
The target version. This is the version of Java to which the compiled result will comply. As with "source version" you can pass this as a parameter to the compiler.
Runtime version. This is the version of java that is actually installed on the host that is running the compiled java (the byte codes). You can never configure this at compile time since this is the thing that is installed on the runtime host.
There are some caveats.
The source and target version numbers must be equal to or less than the version of the java compiler that is actually compiling the java source. For example, you can choose target version 1.7 if you are compiling using a version 1.8 java compiler. You may not choose target version 1.8 if you are compiling using version a 1.7 java compiler.
It is possible to install multiple versions of java on a host. It is not possible to choose which version of java tomcat will use to execute your application since the version of java that will be used by tomcat is the version of java that is running tomcat.
It is chosen in that drop down menu, you can set it to whatever you'd like. However, you must have that version installed on your system/server in order for the project to function correctly. If you go to Window->Preferences->Java->Installed JREs, you can see which JREs you have installed. When you create a new server element in eclipse, you can also set the JRE of the runtime, and just make sure it matches the version of your project.

how do I get eclipse to use a different compiler version for Java?

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

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