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

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.

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

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 to recreate Eclipse's ignoring of binary incompatibilities in an Ant javac invocation?

Eclipse provides a Java build-time option to ignore problems with incompatible required binaries at compile-time (see screenshot below; Ignore is the default actually):
I had needed this because I'm compiling a library on Eclipse Indigo (which I believe internally uses a 1.7 compiler) with a 1.6 target, but which is dependent on a library with a 1.8 target. (Everything will still be run on a 1.8 JVM where that dependent library is used, so I want the compilation to work.)
However, when I came to translate this to an Ant javac task in a build script, I've no idea how to specify this option (or, indeed, whether it boils down to a single compiler option). Having a look at the Java 6 javac 'man page' doesn't suggest anything obvious. (I appreciate that I am here using a 1.6 compiler — I'm on Ubuntu 12.04 which still has Java 1.6 — instead of Eclipse's internal 1.7 one, but that shouldn't affect the question.)
So:
(i) What does this boil down to in terms of javac options (or, equivalently, how is Eclipse achieving this?)?
(ii) How, if at all, is this translateable to an Ant javac task invocation?
To see the command that Eclipse is running when you click "Run":
Go to the Debug window, which is generally within the Debug perspective
Expand your application and right click on Java executable underneath (image below in case that's badly explained)
Click Properties and see the Command Line box
It could be that Eclipse is explicitly excluding the incompatible library? Hopefully it shows up in the command line box here.

Selecting the correct Java, JRE and JDK versions when building an executable JAR for distribution?

I am putting together my first Java package for distribution to users, and am running into some difficulties. I have jar packages that I've built that users can't run; the error messages vary, but are all "version" something or another. I suspect I'm selecting the wrong build paths, and I'm not quite sure where to start troubleshooting because I don't clearly understand the differences between the Java executable (javaw.exe), the JDK and the JRE; I have some questions that I'd like answered which will help in that understanding. I'm used to the way that C# executables compile in VS; wrapping my head around how executable jar files come together is still a little mysterious to me.
Although I've done a few google searches, most of what I'm finding is how to build a jar file, but not how to manipulate/use/select the Java, JRE and JDK versions appropriately to ensure compatibility. I do understand that the JRE includes the virtual machine that allows Java bytecode to run anywhere, and that the JDK includes development tools...but as to figuring out which version I AM running, vs which version is used when building jar files and which version SHOULD be used...I am completely lost.
I'm using Eclipse Indigo under 64-bit Win7. My build path includes the following:
JRE System Library: C:\Program Files\java\jre7\*
External paths: C:\<MyDocuments>\java\lib\commons-io-2.4*
I also have, installed on my machine, the following paths which are NOT included in the build:
C:\Program Files (x86)\java\jre7\
C:\Program Files (x86)\java\jre1.5.0_22\
C:\Program Files (x86)\java\jdk1.7.0_21\
The users that will be running this executable file are only supported (by corporate IT) up to JRE5. I suspect that my building this pointing it jre7 is one of the things that's messing with me.
My first stupid question is whether there's a difference between "Java" and the "JRE" when it comes to version numbers. For instance, when I read about JavaSE7 or JavaEE7, are they talking about the JRE version for the standard or enterprise editions? Are the development kits and runtime environments just components to JavaSE/EE? Or are they separate and distinct products?
Then, my understanding is that I should build this jar using the lowest-common-denominator JRE expected from my users. In this particular case, because the corporate standard is JRE5, I should build this pointing to JRE5 instead of JRE7. Is that a correct assumption?
Does it matter if I build using the 64bit or 32bit version? Some older machines may still be 32bit running JRE5, so I need to make sure I'm backwards compatible.
The Program Files (x86) naming conventions confuse me. JRE7 installed as \jre7. However, JRE5 installed as \jre1.5.0_22, and I also have jdk1.7.0_21, which, based on the JRE name, I assume to be the Java7 Development Kit. Do I need to install the Java5 Development Kit to properly build this program? And am I properly interpreting the versions from the filenames? that 1.5 represents Java5 and 1.7 represents Java7?
Then, my last question I know there's an Eclipse option to copy the external libraries to the project. I assume that if I do this, this will be included with the jar so the users do NOT need to have the Apache Commons jars on their local machines in order for this to run properly. Does the manifest include any confirmations that these files were included? I've been unable to find any references...so I'm trying to verify if I'm even pulling the Commons libraries over and where to look to get that confirmation.
This is just speculation, but I think you may have to re-write the program with the java5 jdk, because some of the APIs/libraries used in java7's jdk may not be recognized by the java5 jre. (Just like it wouldn't be possible to play PS4 games on a PS2... without any serious modding)
If you write the program in Eclipse, you can just select what library you want to use as you create the Java project. At the "Create a Java Project" screen, you can select the "Use an execution environment JRE:" or "Use a project specific JRE" or "Use default JRE" option. (I have jdk6 and jdk7 installed on my computer so I'm able to select those two options; which is why you may have install the jdk5 in order to create a java 5 project.)
You are properly interpreting the filenames. jre1.5.0_22 is java 5 update 22.
There is a way to specify which JRE is used to run the jar file.
cmd prompt >
"C:\Program Files (x86)\java\jre1.5.0_22\bin\javaw.exe" -jar "filepath_filename.jar"
Drop the "w" from the javaw.exe, and you'll be able to see any console and/or stack trace output.

Java compilation problem, Linux, project from Eclipse re-compile on Linux

I am trying to recompile an existing Java project exported from Eclipse. It is necessary to recompile this because I am running simulations remotely on other machines where a different (older) version of Java is installed. I have tried recompiling my .java file which specifies the simulation in question. However, it appears that it is necessary to recompile all other classes etc as well. Has anyone got an idea how to do this WITHOUT using Eclipse (I am not the Admin on the other machines and thus Eclipse is unavailable to me) and not manually because the project is quite huge?
Thanks a lot for any suggestions!
I recommend you to always have an command line way to build an application. The usual way to do this in Java is using ANT (or Maven).
As #Santiago Lezica says, Eclipse can generate an Ant file.
I believe that Eclipse allows you to build for an older target platform than the one you are currently running. That way you can do all of your builds locally.
The second approach has the advantage that you can fix any problems arising from compiling for the older platforms (e.g. use of new language features, use of new classes / methods) from the comfort of your own ... workstation.
There is another option that you should consider: Tell Eclipse to generate code for the old Java version (see the compiler options). That way, you can create code that runs on Java 1.3, even if Eclipse uses Java 5.
Not sure what your requirements are, but you could set the compiler level for your projects at the (older) level of your Linux installs. This would cause Eclipse to recompile it at that version, instead of a newer version.
At my company we use IBM's Rational Application Developer (instead of pure Eclipse), but I am assuming the option is in the same spot. If you right-click on your project, you can go to the Java Compiler options and then set the compatibility to the level of that on Linux (1.3, 1.4, etc.).
Since compile Java byte-code is supposed to be portable (for the most part), this should get you past most of your problems.
Otherwise, the other option is to use something like Ant or Maven scripts (which can be kicked off by Eclipse) and then just use a property to set the compiler right before you run it. This way you don't have to switch properties on your projects all the time, if you truly do need "newer" compiled code and can't live with "older" code on both systems.

Categories