Can a java8 compiled jar file run on java7? [duplicate] - java

This question already has answers here:
Can Java 8 code be compiled to run on Java 7 JVM?
(5 answers)
Closed 5 years ago.
I have a jar file in that some features only belong to java 8(for ex lambda expression), so it is compiled in java 8. Same jar has some features belong to both java 7 and java 8. Now i want that when user run this jar in java 7 , java 7 related classes should work as he/she only wants functionality that work on java 7 only. If he/she wants the functionality related to java 8 , he will run it with java 8.
This java 8 compiled jar should not give an error of "Lambda expressions are allowed only at source level 1.8 or above". And i don't want to create separate jars for java 8 and java 7 users.

Unfortunately not, multi-release jars allow this, they are a new feature in Java 9, but multi-release jars do not go back to java 7, however they will work for future java releases.
Multi-release Jars

Related

How to build real desktop executable application from JavaFX 11 project? [duplicate]

This question already has answers here:
Bundle JavaFX app with openjdk 11 + runtime
(5 answers)
Closed 3 years ago.
I have written a new JavaFX 11 project. This project is modular and uses JavaFX 11 & JDK 11 too. It doesn't include any build tools such as Maven or Gradle or something else. Now the project has been completed and I'm trying to package it as an executable application for Windows platform (.exe file with dependencies). Anybody could help me with the solution or documentation references??
The first question is why do you start a new project based on the outdated Java 11 and even worse JavaFX 11? The current version of Java is 13 (and 14/15 already availabe as EA releases) and the current JavaFX version is also 13 (and 14 as EA). If it absolutely has to be Java 11 you can still combine that with the latest JavaFX release. So, 11/13 or even 11/14 would be ok.
In order to answer your question: If you were willing to switch to Java 14 you could directly use the new jpackage tool which is included in this release to build an exe and because your project is modular it would be just calling a single command.

Can a java 8 project depend on a java 11 dependency in gradle

I have a project in Java 8 and attempting to utilise a library written in Java 11. I am getting an error:
class file has wrong version 55.0, should be 52.0
Is this something that is basically not possible or is there some Gradle configuration which allows a Java 8 project to use a library written and compiled to Java 11?
The short answers is YES, you can use a Java 11 dependency in a Java 8 project.
The following error class file has wrong version 55.0, should be 52.0 happens when you are trying to load a Java class compiled with Java 11 in a Java 8 Runtime Environment, the version of the compiled class is incompatible with older runtime environment versions.
To be able to run your Java 8 project with the Java 11 dependency, you will need to run your project in a Java 11 Runtime Environment, in most cases this is possible without the need of changes to the source code (sometimes you will need to add explicitly some dependencies that were removed from Java 11 like Java EE and Corba modules).

e(fx) clipse doesn't work on fresh macOS Mojave Java 11 [duplicate]

This question already has answers here:
How to add JavaFX runtime to Eclipse in Java 11?
(6 answers)
Closed 4 years ago.
I just installed the newest Eclipse and e(fx)clipse, but it didn't change anything - I have no idea why javafx imports are not resolved.
Sorry I can't provide more proves I tried to solve it, but I just think (and remember it was I like that before) installing it and restarting Eclipse should be sufficient.
The purpose of e(fx)clipse is not to resolve JavaFX imports. If using Java 11 or higher, you have to add the JavaFX dependency yourself because JavaFX has been removed in Java 11. See Java 11 release notes:
JavaFX is no longer included in the JDK. It is now available as a
separate download from openjfx.io.
See: Getting Started with JavaFX 11
e(fx)clipse offers as a runtime e. g. a way to build JavaFX OSGi/Eclipse-based applications and provides tooling for JavaFX, e. g. to edit FXML files.

JavaFX Java 11 and beyond [duplicate]

This question already has an answer here:
JavaFX applications built to target Java 8 - How to keep running with Java 11?
(1 answer)
Closed 4 years ago.
Many work environments continue to stay on JRE8 -- I'm a little confused on how to proceed with developing for the latest versions of Java. I compile JavaFX applications with Java SE 8 using NetBeans 9. Is there a way for me to start distributing via JDK 9, 10, and 11+ but still keep it compatible with all the JRE8 environments?
Or once I compile via JDK11, will it only be compatible when or if an environment upgrades their runtime? Or can I distribute a completely separate jar that runs without the need for a JRE and start today (even if they stay on JRE 8)?
There is a break between Java 8, and the modular Javas 9+. Though the module system has support for combined non-modular and modular code, JavaFX becomes OpenJFX, and I would not rely on there being one code base for long.
Also mind that modular means you can deliver your application with JRE parts as a small standalone executable.
So develop in Java 11+, possibly "convert" the sources to java 8 with a small tool written yourself. Restrict the usage of var and other short-cuts. (Maybe develop in java 8 and convert to Java 9+?).

Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled against Java 6?

Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled for compatibility with Java 6? I do not have the source code of the dependency. I have the compiled jar as a maven dependency.
Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled for compatibility with Java 6?
Let's unpick this:
You have a project that is compiled so that will run on a Java 6 JRE. (Lets suppose that you only use Java 6 APIs in that project.) The .class files for this project must have a classfile format major version less or equal to 50 ... otherwise a Java 6 JRE won't be able to load them.
Then you have a dependency that is "compiled in Java 7". That could mean one of two things:
It could have been compiled using a Java 7 tool chain but with a target version of Java 6.
It could have been compiled using a Java 7 tool chain for Java 7.
In both subcases above above, you should be able to use the dependency in your Java 6 project if you run the project on a Java 7 JRE1. A Java 7 JRE can load and run classfiles compiled for Java 6. In one of the subcases, you will be loading classes with two (or more) class version numbers. But that is OK.
On the other hand, if you try to run the code on a Java 6 JRE, then:
Subcase 1 will work provided that the Java 7 dependency doesn't make use of any Java 7 (or later) APIs; i.e. it only uses Java standard classes, methods, etc that were present in Java 6 or earlier.
Subcase 2 will not work. The Java 6 JRE won't be able to load the dependency. Indeed, if the dependency is static (i.e. the project source code has compile time dependencies on the APIs of the dependent), then the project code won't build ... because the Java 6 compiler should refuse to read the dependency's newer version classfiles.
The most advisable approach is to migrate your project and your execution platform to Java 7. Or better still to Java 8 or Java 11, since Java 7 is EOL'd
If you can't do that, the next best thing would be to avoid using the Java 7 dependency ... until you can upgrade.
If you have customers who insist they you continue to support Java 6, then they are impeding your ability to progress your product line. They should be charged a premium for that.
If you have decided to avoid upgrading your Java platform for internal reasons, this decision is accumulating technical debt ... that your organization will need to "pay down" that debt in the long term.
1 - .... or JDK. A JDK is equivalent to a JRE for the purposes of running code.
In your case you actually ask if there is Forward Compatibility between Java 6 and Java 7. Generally speaking Java does not support Forward Compatibility as the 1.7 JVM cannot run code compiled with 1.6. This happens mainly because the version of 1.7 compiled Java bytecode is not known by the older version (1.6).

Categories

Resources