I'm writing a java application using JDK 12.
From Java 11, Oracle doesn't provide any JRE.
So, JDK 11 and JDK 12 don't have Java Runtime Envirnoment.
How can i execute jar written using jdk 11 or jdk 12?
I tried using jre 8 and it works.
How is possible open jar file written using jdk 12 with jre 8?
I read on Internet that from Java 11, jar files have a little jre inside.
But i need java 8 anyway for open them?
Both Java 11 and Java 12 JDKs have all you need to run a packaged jar file. Assuming that your installation is correct and you have the correct environmental variables in place, simply doing:
java -jar <jar_file>
should work properly for you. Granted JDKs nowadays do not have a specific jre folder as it was in the past, but that does not mean that they do not provide anything needed for you to run a java application. Just have a look at the bin folder for more.
Now regarding your second question. Is it possible to run a java application that has been compiled against a newer version using an older jre. Well the answer is it depends. Normally running bytecode produced from a newer jdk against an older jre will result in class compatibility errors. This can be circumvented by compiling your application in compatibility mode (most likely your case).
So to sum up. Go ahead and install the correct Java version (ideally the one you develop with and planning to deploy on). Set up your environment correctly and you'll have no problem running anything.
Related
I removed java-11 and installed jdk-1.8. Now when I try to start eclipse then its throwing the following error:
Being a beginner I searched and found some solutions but not worked for me
Ok. I'm going to make some assumptions based on some implications of what you've said here.
I assume that you had Java 11 installed, and you had installed Eclipse, and it was working.
You then, for some reason that I can't understand, decided to uninstall Java 11 and replace it with Java 8.
The error message is because when you had Java 11 installed and you installed Eclipse, Eclipse stored the path to the Java 11 distribution in the "eclipse.ini" file. Now that Java 11 is gone, it cannot start up.
The easiest way to fix this is to reinstall Java 11, and hopefully it will install into the same location as that error message shows it is looking for it.
Now, to address why you might have thought to replace Java 11 with Java 8. I'm guessing you're working on an application that requires Java 8 to compile and run. In this context, it's somewhat understandable that you would have done what you did, but it was the wrong thing to do.
Eclipse can run with one Java version, but build and run applications with a different Java version. In fact, I think it's generally a good idea to run Eclipse with the newest version of Java it can run (generally about a version short of the latest), but build your applications with the version of Java required for those applications.
So, you should have both Java 11 (or newer) AND Java 8 installed. Run Eclipse with Java 11, and build and run your applications with Java 8. In Eclipse, you have to register the Java 8 distribution in "Installed JREs" in Eclipse preferences, and also record that Java distribution in the "JavaSE-1.8" Execution Environment, in the preferences tab right below "Installed JREs". Set your application to require "JavaSE-1.8".
For years I have distributed a piece of software that relies on javafx, and the solution has been to include a JRE in the distributed software and run my .jar file with a script that sets JAVA_HOME. It has worked great.
The Mac version has always been a challenge: I have to download the .dmg from java, unpack it, and tweak the script for (historically) a different directory structure (which may be an artifact of the .dmg packaging, I don't know). But it always worked. (I do this on Linux.)
I recently updated the included JRE to the latest version (1.8.0_291) and it no longer works on the Mac, even after I corrected for the new directory structure. Users report that the script errors with:
Error: could not find libjava.dylib
Error: Could not find Java SE Runtime Environment.
...and indeed, there is no libjava.dylib. In the previous Mac JRE version I was using (1.8.0_92) this file was at:
lib/jre/Contents/Home/lib/libjava.dylib
...but it is not present anywhere in the latest. I also note that the latest JRE totals 107 MB, where the older one was 164 MB, so it would appear that there is something I'm not understanding about these more recent JRE downloads. E.g. perhaps the mac version requires an installer to run to be complete?
Where can I download a distributable Mac JRE?
Failing that, what is the proper way to make a contained Mac JRE?
It appears that AdoptOpenJDK is a good general way to go for multi-platform pre-built JREs, but they do not include JavaFX.
I attempted to use OpenJDK in conjunction with libraries from OpenJFX from openjfx.io, and although my application superficially worked, there were very many exceptions implying poor compatibility between the two. (I was unable to match versions precisely for JDK 11. It's possible that the latest version of both (JDK 16) might interoperate better.)
I found the Liberica JDK from BellSoft. Their "Full JRE" includes javafx and is working. I do get one swing-related exception on startup, but it doesn't seem to matter to functionality.
Apparently there is also the Azul platform, but I have not investigated it.
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
I'm working on Windows 7.
I have projects on my machine that runs with Java 1.7.
I have now checked out an existing project made by someone else that runs with Java 1.8.
I'd like to know the best way to switch from one Java version to another according on which project I work?
In your case, you can use the setenv.bat file, to set your JRE_HOME version.
On Windows, create the file %CATALINA_BASE%\bin\setenv.bat, with content:
set "JRE_HOME=%ProgramFiles%\Java\jre1.6.0_20"
exit /b 0
So create the file on both tomcat and set jre location
It depends on which frameworks are you using in these projects. If you talk about J2SE java 8 is backward compatible. All the code which is written in Java 7 is completely fine with Java 8. you don't need to worry about.
I am using JDK8 on my normal desktop and I have a separate linux box that I am trying to run the Java program on.
The problem I'm running into is that the linux box is running jdk7, so the computer has JRE7 basically that it is running off of. My question is, is it possible to create a jar file in JDK8 that will be compatible with java 7?
I'm using IntelliJ to compile. I tried to compile in 1.7, but it gave an error when I did end up trying to do it. I compile it here:
In 1.8 it works fine to compile, but when I try to compile in 1.7 it doesn't work.
I know the short answer is to update the linux to JRE 8. But I am curious, is there a way to make it backwards compatible? Or is the other answer to simply install JDK7 on the desktop in order for it to run on the linux box using JRE7?
Java programs built with JDK 8 will only run on machines running JRE 8 (or higher).
I found this when trying to compile classes on my local Linux machine (using JDK 8) and deploying to a remote server running JRE 7. The classes just wouldn't work (like you're finding).
If you want to use JRE 8 on linux, I recommend using the oracle-java8-installer package from webupd8team. Installation instructions found here (assuming Debian based distro).
If you want to compile to JDK 7, it's not good enough to only have JDK 8 installed and pick to compile 1.7. You need JDK 7 installed to and restage your project to use JDK 7.
The thing you have to remember is that the difference between JRE/JDK versions is not just the extra features developers can use (e.g Lambda functions) but it's also that the JRE itself is improved (efficiency, garbage collection, etc.).
As a extreme example: If you wrote code that only used JDK 1 features but compiled it using JDK 8, it wouldn't run on a machine running JRE 1 because the Java classes had been compiled with JRE 8 in mind.
Do note though, that if you're Java Code uses only features from JDK 7 or 6 etc., you might think it good practice to compile using the minimum JDK required to allow for compatibility with more machines. Well...you'd have that compatibility but at a cost of using inefficient, out of date, possibly vulnerable compiled classes (At little extreme, but you get my point).
Are you using any new Java 8 features? Because if you are, this means you cannot build the project against the JRE7.
If you are not using any Java 8 features, you can build to Java 7 most easily by downloading the JDK7 and switching the project to use that instead of the JDK8.
You should also set project language level to 1.7 (and module language level(s) as well, if they're different). It's done in Project Structure settings dialog. After that the project should compile.
Note that you shouldn't use any of the APIs that appeared in 1.8, but such usages will most likely be highlighted in the editor.