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.
Related
I want to install Java on a few different servers for running a third party service on tomcat.
I am not a Java developer and I got a bit confused when I began searching for the installer.
I checked the different Java versions and their long term support dates.
It seems that Java 11 JDK is - LTS so i decided to use it.
But when I was trying to find a runtime version (JRE?) since this is the version I am supposed to install on the server(?) I only found Java JRE 8.X.X, and this got me confused.
There is no higher major version of the JRE?
How does it work if you use the JDK 11 or even 14-15 as a developer and then use Java 8 JRE on your deployed servers?
Maybe the JDK holds inside of it the Java 8 JRE version?
Or do you actually need to install the JDK version on the servers instead?
The JRE is a subset of JDK. It contains everything needet to run Java applications but no support for development. So, if you need a JRE, a JDK is also good, although it conatins much stuff that you will not need.
Normally, for executing a Java application, you need a JRE with the same version or higher than the JDK used for development. But the developer can advise the compiler to generate code for a lower version. If he does so, he cannot use the features of the higher versions. For example the compiler of JDK 1.8 can produce code for JRE 1.6. You should consult the manual, to see which old versions are supported by the compiler of a specific version.
After some research,
It seems there is no separate JRE section anymore.
Also JDK 11 for production is not free and I guess this is an ongoing trend for the near future.
On a side note there is the open JDK 11 version but it does warn you about outdated security updates.
So basically in my case sticking to updated Java JRE 8 version is good enough for the near future.
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.
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
I have developed a Java Application in Eclipse using JDK 1.7.0, the application runs and everything works as expected.
Now I would like to send the application to a colleague of mine who is using a different version of the JDK. He is using 1.6.x.
I am not interested in him opening up the application and debugging through it in Eclipse. I would only like him to run it. If I make a runnable .jar of the application and send that to him, would it work even though he has a different JDK installed on his machine?
Thank You.
You need to compile it using JDK 1.6 (or lower). Provided your code doesn't use features from Java 7, you won't have to change anything in your sources
It would work if your colleague had a newer JRE than the JDK you used to compile it. All versions of JRE are backwards-compatible with all class files built with previous versions, but the opposite can clearly not work because new versions introduce new features. Your colleague will get an UnsupportedClassVersionError.
However, it is easy to set up in Eclipse the target class version for your project: go to Project Properties, Java Compiler, and under JDK Compliance choose the version 1.6.
Needless to say, this will fail if your source code uses any features or library calls which the version 6 does not support.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java SE 6 vs. JRE 1.6 vs. JDK 1.6 - What do these mean?
I found that sometimes applications ask for java_home to point to a JDK and won't work with a JRE.
Why is that the case? Is it correct?
Short version: some applications use libraries/code that is contained only in the JDK.
Longer version: Usually every Java program should be able to run with just the JRE. And most applications actually work with the JRE.
Now the JDK comes with some additional libraries and tools that you usually only need when developing applications (and not when you simply need to run them).
But occasionally an application decides to use some of the code that gets delivered with the JDK when it runs. It's very rare and is usually not a good idea (unless that application is itself about developing Java applications).
A good example where old versions of Apache Tomcat: it used the Java compiler bundled with the JDK to compile JSP code into bytecode. For this it used tools.jar which is included in the JDK but doesn't get delivered with the JRE.
Newer Tomcats switched to a separate compiler (I think it's based on the Eclipse compiler) for this and no longer require the JDK: they run just fine with just the JRE.
#Rekin mentioned another good example: Maven uses the JDK because it actually compiles Java code.
One application I worked on was using the JDK to compile automatically generated java and load it for user after that (a db-oriented compiler/runner).
#Joachim has a more complete answer.