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

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.

Related

Why aren't there compatible JREs for JavaSE12+?

I am getting an error when trying to run a .java file as a Java Application and I get the following error:
Unbound classpath container: 'JRE System Library [JavaSE-14]' in project <project_name>
I tried to change the execution environment and for JavaSE12 and later versions there are no compatible JREs. (But for JavaSE11 the compatible JRE is java-11-openjdk-amd64)
I do not know what to do because the project I am working on doesn't work with old Java versions. How can I choose an environment for the latest Java versions?
Note: I am using Ubuntu 20.04 if that makes any difference.
tl;dr
I am not a Linux user, so I may not know best. But I suspect the simplest approach to running your .java file is to:
Obtain and install a JDK for Java 15 for Linux
Call java app on the command-line, passing the path to your .java file.
Backwards-compatibility is a priority for the Java team. Most any existing Java app should be able to run with the latest version of Java. There are exceptions, but they are very few.
JRE is passé
The JRE (Java Runtime Environment) was a subset of the JDK (Java Development Kit), omitting some of the programmer tools. The JRE as a separate product seems to be getting phased out.
Oracle and much of the Java community has shifted away from the idea of regular users having Java installed on their personal computers. Instead, apps should be delivered with a JVM specific to their host platform bundled within the app. This bundling of a JVM can be done using newer tools such as jlink and jpackage.
For more info, read:
Java Client Roadmap Update - Oracle (2020-05)
Java is still Free
Obtain a JDK
You said you have a .java file to execute. That file must first be compiled before it can be executed. The more recent versions of the java app can do both steps at the same time, compile & execute.
First download and install a JVM for your host platform.
Java 11 is the current long-term support (LTS) version. Java 15 is the latest release. You may want to read about the six-month release cadence for Java.
You have a bounty of vendor choices providing implementations of Java. Here is a graphic flowchart I made to assist in choosing a vendor.
If the steps shown here are overwhelming, I suggest either:
Using apt-get or similar package installer to obtain a build of OpenJDK for Ubuntu. I am not a Linux-user, so I do not know the details.
Head over to AdoptOpenJDK to download an installer for Linux.
Some motivations to consider in selecting a vendor.
Compile & run your app
Once your JDK is installed, on a console (such as Terminal.app in macOS), run something like the following. The java command should both compile and execute your .java file, if that single file makes up your entire app.
java /path/to/some/folder/MyJavaApp.java

Another "Build path specifies execution environment JavaSE-13." problem

So, the full warning looks like
Build path specifies execution environment JavaSE-13. There are no JREs installed in the workspace that are strictly compatible with this environment.
I just installed JDK(version: 14.0.1 from here) and Eclipse(Version: 2020-03 (4.15.0) from here)
I've read post1 and post2 but i dont have any libraries in Java->Build Path -> User libraries. It's empty at all. In Installed Libraries i don't have any jdk{version}. I only have Name "JDK" and Location "D:\..."
Am i missing something vital? Because i have no idea how to fix it.
I am newbee, so if you have an answer or following question, please write a bit simpler. Thank you.
This should really be a comment, but I felt like spelling things out. The main thing to watch out for here is that with Java its version comes into play at least 4 times and there is nothing strictly stopping you from having 4 different versions configured on each occasion, which may or may not work. Add Eclipse and it becomes at least 5:
The java binary used to run Eclipse itself. (Configured via eclipse.ini)
This can affect how plugins behave. I remember a scenario where m2e would refuse to use a Java compliance level older than the version of the JRE running Eclipse. I can't make too confident specific calls from the top of my head here, but this is something to keep in mind.
Should be latest and greatest, though Java 8 appears to be the defensive option for obscure scenarios, such as projects depending on deprecated internal libraries. (tools.jar comes to mind)
The javac (java compiler) binary used to build your project. (Sits next to the java binary in a bin subdirectory of the JDK)
Should also be latest and greatest: every version of javac is capable of compiling source code written for previous versions as well as generating compatible byte code for older versions. This is what the following two points are about.
The Java version that javac is told to assert that your code is compatible with. (javac -source parameter)
The Java version that javac is told to make your build compatible with. (javac -target parameter; equal or greater than the value of -source)
The java binary used to run your build.
Should also be latest and greatest, though the minimum version required here is what was given to javac via -target, and incompatibility scenarios do exist.
The latest trend here is to ship every Java application with its own JRE to rule out problems arising from users having configured or installed Java in an incompatible manner. A user will then be required run a specially crafted native executable or a shortcut which will then invoke Java with the right arguments to start the application.
Now, you do not invoke javac or java even. In fact, the former is not even invoked by Eclipse. Eclipse actually comes with its own Java compiler, Eclipse Compiler for Java (ECJ). For what it's worth, we don't usually need to worry about that however. For Eclipse to know anything about available JREs, you need to match JDK locations on disk to Java versions "known" to Eclipse. This is done under Window -> Preferences -> Java -> Installed JREs -> Execution Environments.
After doing that, they should pop up in all the places where you can specify Java versions, which is mainly Project settings and possibly run configurations you have set up or Eclipse has set up for you. Project settings can then be set to up to infer the compliance level for building from the version of the JRE configured to run the build.
By the way, note that you probably want to use AdoptOpenJDK instead of the release from Oracle for reasons. You'll also want to just unpack any JREs which aren't the latest one in a nice folder somewhere on your hard drive instead of installing them. Installing them mostly means to make .jar files run with that version when launched from your OS's file browser. If you are on Linux, sdkman is currently the preferred way to do this.

How to change NetBeans Java Project Runtime?

How can I change the Projects Runtime Platform?
(standard NetBeans [8.1] Java Project [Java Application])
Details:
using Ubuntu with NetBeans 8.1
I have installed JDK 1.6, JDK 1.7 + JDK 1.8
My projects platform is Java 6.
My systems default Java is Java 7.
NetBeans-IDE was started with Java 8.
When I run the project via the IDE (Run project), the Project-Platform is used (which is Java 6).
I cannot change the Runtime Platform!
It's always: Project Platform
I tried to Manage Platforms..., but I still can't change the value of the Runtime Platform-ComboBox.
EDIT:
As I wrote in the comments, I don't want to change the Project-Platform.
The project should be compiled with Java 6 but run with Java 8 (when click on Run project).
What I want is:
Project-Platform = Java 6 (used to compile),
Runtime-Platform = Java 8 (used to run).
How can I change the Projects Runtime Platform?
Unfortunately you can't. As you point out, its value can only be Project Platform. Of course that invites the question "What is the point of even having that Runtime Platform field?".
I suspect that it may have been (unwisely) added to the GUI so that at some time in the future it will be possible to actually select a different JDK to run against. There is an open Bug Report for this: Bug 186747 - Can't Build and Run with Different Java Versions.
It's also worth noting that the Help documentation for the Run screen does not mention that Runtime Platform field. My own view is that the field should be removed until it serves a useful purpose.
What I want is: Project-Platform = Java 6 (used to compile),
Runtime-Platform = Java 8 (used to run).
Unfortunately that can't be done, as shown by the open bug. The best you can do right now is:
Create platforms for JDK 6 and JDK 8 in NetBeans.
Set the Java Platform to JDK 1.6 on the Libraries screen, and set the Source/Binary Format to JDK 6 on the Sources screen, then build to verify that no invalid APIs are being used.
Switch the Java Platform to 1.8 on the Libraries screen to run your 1.6 code under 1.8.
I realize that you don't want to do that, but unfortunately there's no silver bullet in the current release of NetBeans (8.2).
An alternative approach would be to build and run using only JDK 8, but include the Animal Sniffer Maven plugin "for checking projects against the signatures of an API", to verify that your 1.6 code was not using illegal APIs. But of course that's only viable for Maven projects.
Please look at Netbeans Project Setup - Setting the Target JDK in a Project
You just have to change the Runtime Platform at the Libraries option, that's all.
Just went through this pain for Apache NetBeans 14. I was trying to set a remote debug for a Raspberry Pi.
It works only if the jdk match on both the local and remote versions. In my case I set them both to 17.
I would have expected that the default jdk on the local machine should have been able to be changed but I fail to determine how.
I am running Ubuntu on the local machine so maybe the platform cannot be changed (don't know). Moving on.
Changed runtime platform

Compile Java Program in Eclipse with Older JRE Without Installing

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.

Cloudbees RUN#cloud select Java 1.8

I am using Jenkings on Cloudbees and I would like to change the JDK version to 1.8.
I followed the tutorial at developer.cloudbees.com/bin/view/DEV/PreInsatlledJDKs. However, I fail at step 10 -> There is no such option to choose the JDK. Where can I change this?
UPDATE: I made some screenshots to better explain my problem.
This is the jenkins configuration for the JDK. As you can see, I only have one JDK selected and it's Java 8.
However, if I view my "System Information", everything looks like Java 7. It says that "java.home" still points to "/opt/java7/jre" and "java.runtime.version" points to "1.7.0_25-b15" for example.
Not surprisingly, the compilation process of my maven project fails, because for the maven compiler plugin I specified 1.8 as source and target java version.
If you have one single JDK, then you won't see that option, cause it will pick it up by default.
You will se the JDK option with the drop down menu if you have more than 1 JDK configured in your jenkinsURL/configure.

Categories