Are there any issues when projects have different Java compiler versions? - java

I have an Eclipse workspace with several Java projects (in Maven); these have dependencies on each other. Recently I started changing just some of the projects to target Java 1.7, and other than resolving some new warnings, nothing is different when compiling.
However, it feels like something might go wrong when I try to run everything. How are class files loaded, and are there any issues, in the following situations?
Is there a problem when a Java 1.6 project depends on a Java 1.7 project? Will the 1.6 VM just refuse to run any 1.7-generated bytecode, or does something weird happen in order to get it to run?
Is there a problem when the reverse happens?

When you have java 1.6 project depends on a Java 1.7 project or java 1.7 project depends on a Java 1.6 project, you should always run your program on the higher version of JVM, which is java 1.7 in this case. In a nutshell, in most cases, class files built with the Java SE 6 compiler will run correctly in Java SE 7. But there are some exceptions. Please see Incompatibilities between Java SE 7 and Java SE 6 for a list of incompatibilities between Java 6 and Java 7.

You can build a project with JDK 1.6 that has some libraries that where generated with 1.7
HOWEVER if some of the code from the project 1.7 needs some JDK 1.7 feature (for example, it uses the new Swing combobox with generics) it won't run on a 1.6 JVM.
So this is something to be very careful about, as you can run into compile time trouble (which is at least not silent) but also runtime trouble. If you can avoid this, it might be better for you.

I have also faced a similar problem and as per my knowledge i don't think that 1.6 VM can run any 1.7-generated bytecode but i think the reverse is possible as 1.7 is a upgraded version of 1.6VM.

Related

Create Jar Compatible With Java 7 With IntelliJ Using JDK8

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.

What is the difference between using JDK 7 vs using JDK 8 with compiler compliance level 1.7?

I was wondering if there is any difference running/building a software under JDK 8 and using compiler compliance level 1.7 vs JDK 7 as system default? I am more interested in reference to Android building, building apps, Eclipse, Android Studio, etc.
Yes, there are loads of new classes in the JDK 1.8, for example, the java.time classes. You won't get those if you build in JDK 1.7; but you will be able to use them if you build in JDK 1.8 with compiler compliance level 1.7.
Yes there is a difference between running/building a software under JDK 8 and using compiler compliance level 1.7 vs JDK 7 as system default.
running a software under JDK 8 and using compiler compliance level : You compile in jdk 1.7 but run on 1.8. No problem, your programm will work as needed.
JDK 7 as system default : You compile in 1.7 version and run on the same version.
I am wondering in waht case you would like to use the first case ?
truck load of difference actually. With JDKs the the compliance level is a directive to the compiler to specifically use the optimizations and linking features for the version you specified. It has a lot more going under the hood but I don't think you want to know that. New JDK versions bring new features and the compilers in those version are able to understand and link those features when building class files or assembled code of your source Java files. Consequently the JVM runtime in those JDKS is also equipped to handle such optimizations and cases and process them. So without compliance levels the class file that you build with JDK8 would only run correctly with JDK8 based runtimes. They may not do so with JDK7 or 6. To counter this problem and thus allow your JDK8 compiled code to run on JDK8,7 and maybe even on 6, hyou need to add compliance level to compiler directives accordingly. Downside is that you may not be able to use some latest features which the compiler offers but such cases are far few and outweigh the need for interoperability and potability.

Java projects forcing dependents to adopt Java versions?

Here's a question. Suppose I have a Maven project ("A") that pulls in a different Maven project ("B") as a dependency. Both currently use Java 7 to compile. If Project B switches to Java 8, does it force Project A to use Java 8 as well?
If so, is there a way around it - that is, have Project B generate code that Project A can use, without downgrading Project B to Java 7 or upgrading Project A to Java 8? For example, having Project B do some form of "source 1.8 target 1.7", for example. (javac doesn't seem to like that, and I can't find an alternative compiler that will - for example, plexus-compiler-eclipse doesn't support Java 8, but that's another issue altogether; the issue is that of decoupling the generated bytecode from the source version that was used to generate it)
Java is backwards compatible but not forwards compatible. When running a program with a JVM (JRE) version older than what the program was compiled with you will get an error, (the usual Unsupported major.minor version error). Its quite logical. Some new byte code directives might have been introduced in the newer JRE version which the old JRE doesn't know about (especially with all the new Java 8 fancy stuff).
If you have a dependency that is compiled with Java 8 (although I doubt any of the popular dependencies have already been migrated to exclusively use Java 8!) then you have to upgrade your project to use Java 8.
On the other hand, if your project is upgraded to Java 8, then any dependencies, irrespective if they were compiled with Java 8, 7 or 6, will work, due to the backwards compatibility guarantee.
If Project B switches to Java 8, does it force Project A to use Java 8 as well?
Yes, unless you can compile Project B with -source 1.7 -target 1.7, which means you can't use Java 8 features in B.
It has nothing to do with Maven.

How to launch a WAR by restricting the environement to JDK 1.5 with Websphere 7?

I would like to know if it is possible to launch a war which have to run with the JDK 1.5 (not compliant with JDK 1.6) under Webpshere 7.
It is not possible/supported to run WebSphere Application Server 7.0 process with any other JDK than the one that is bundled with the product.
I guess I'm confused by your question. JRE's are to be backwards compatible. So the 1.6 JRE within IBM WebSphere Application Server V 7.x.x.x will run Java EE code as far back as Java EE 1 (which I think might be Java 1.2 compliant) and should support Java SE code as far back as 1.1. (Why you'd want to go that far back, I'm not sure....).
There really is no trouble to it. Just compile your code with a 1.5 JDK (that's what we do), or if you want you could compile it with a 1.6 compiler and flag the compiler to use 1.5.
Your code doesn't have to be 1.6 compliant. It just has to be 1.5 compliant and it will run fine. That is the whole point of abstracting and backwards compatibility with the JDK and JRE.
As an example for you, we have an enterprise application that we compile with Maven 2 using Sun/Oracle's 1.5 JDK. We package in several open source project jars, some of which are even as old as 1.3 compliant and/or compiled.
We package all of this up as an EAR using Maven 2 and we deploy it to IBM's WebSphere Application Server (and we run 7.0.0.9). Our code runs just fine. We even have some modules that we do compile with JDK 1.6 to leverage certain functions in 6, but most of the code is compiled as 1.5. And it runs fine.
So I guess I'm confused what you are asking. Could you provide a more concrete example of what you are trying to do? Because from what you have told me, you should be able to run just fine in WAS 7 without compiling at JDK 6.

JDK version problem

I am looking for clarification in jdk versions.
I am getting error
java.lang.UnsupportedClassVersionError:Bad version number in .class file"
I hope this error is for different jdk version. I am having myeclipse6.0.1 and weblogic10.3.
Now, in my systems has jdk1.5 but weblogic has jdk1.6, as well myeclipse supporting 1.5 only.
I read the all forums and they said compile is different version and running different version.
So, here i thought program compliling 1.5 and running 1.6.
Is it correct ?
This means that the Java program was compiled for a newer version of Java than you are trying to run it with.
The easiest way to be certain is to use the same version of Java for compiling and building as is to be used on the final system. For you, this is most likely Java 5.

Categories