running java code in higher versions, compiled in lower java versions - java

I have java EE artifacts like .jar , .war, .ear files compiled and running in java 1.6 version. i wanted to run them (.ear file [.jar + .war]) in latest java versions like java 1.8.
Which Option would be the best considering moving to new java version.
1) Recompile Code in 1.8 and deploy to Application server
- to be at a little safer.
2) Just deploy to java 1.8
- No Code Compilation required (means .ear file generated using java 1.6). .ear file can be safely deployed to latest java 1.8 version
- probably we wont end-up having runtime issues ?
As this this is a production code base and no developer. Hence No Code changes in either cases, the only change is java version. I prefer going with Option-1, as compilation in higher version of exiting java classes to avoid any runtime issues.
Any thoughts ! or other useful options in this regard.
Thanks

As #Stultuske mentioned you only telling no code changes possible in either cases then your first case not valid one only.
But anyway as per java(Oracle) compatibility specification Java 8 is backward compatible with previous versions for sure no doubt about this except some very rare cases where binary incompatibilities happened.
For more information about this you can refer below stack overflow link --
Can newer JRE versions run Java programs compiled with older JDK versions?

Recently I did a similar thing but we updated the code/dependencies if needed.
I will suggest recompiling the code base first because this will tell you quickly if it or any dependencies don't support the newer Java version. If compilation is successful, then deploy to test environment and do sanity, regression, etc.
I hope you aren't talking about deploying to Production environment directly :)

Related

How to link code that is compatible with other version of java

My existing server is running on Java 1.6 and I cant upgrade it.
I need to use some third party jar/api that is compatible with Java 1.8, when I write some code to access its API, eclipse throws
Exception in thread "main" java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version
How I can proceed now? Not sure if this is duplicate question, if yes please provide some link on this.
The point is: Java virtual machines are not forward compatible.
A .class file that was generated by an "n+1" compiler can't be used on a "n" JVM. (unless you specifically instruct the compiler to compile for older versions of java)
Your choices:
see if you can acquire a version of that library compiled for Java 6
see if you can run your application on a Java 8 JRE (there is no problem running java6 classes on a newer JVM!)
Option 1 can get pretty ugly - as that library might have dependencies on system classes that Java6 doesn't have.
This is not possible to use multiples Java version or even multiples JVMs in the same project
If you really need this API, then you have just few choices, the best one is to upgrade all the project to use Java 1.8
No, you can't use jars compiled under java 1.8 in an environment running at java 1.6
As others have pointed out, you can't do this. However, you may be able to find older versions of the libraries you wish to use, and those libraries may support your particular version of Java. This potentially means that you will be adopting bugs into your software, but that's just how software evolves over time.
If you must use this 3rd party jar,
then you must compile the 3rd party jar using -target 1.6 (to target java 1.6).
Maybe you will get the source and compile it yourself or maybe you will get somebody else (the vendor, perhaps) to compile it.
All other options are:
Upgrade your JVM to Java 8.
Don't use that 3rd party jar. Either write or find one that is compatible with java 6.

Is there any impact if we deploy the war files which is built on JDK 1.6.0.35 on production?

In production environment we are using JDK 1.6.0.26. is any impact if we deploy the war files which is built on JDK 1.6.0.35?
One can not be certain :)
In theory java is backward compatible which means that newer runtime can run code build for older runtime. But the other way is not true.
In your case only the minor minor version differ and I believe it will be ok.
Don't forget to run your end to end tests to verify that everything is working ;)

JAVA Compilation : compile with new JVM version and run on older version

Here is a small explanation of my problem.
I have an application which was compiled with java 1.5. This application is installed on 2000 pc (blockboxes) installed at customer premisses.
The jar of my application is often updated to add new feature and fixes, but for technical reasons, it is not very easy to update the java version, so I must keep using Java 1.5 on those existing machines.
Few months ago, I got a request for a new requirement for this application. To fulfil this task I have added the usage of Hazelcast in this application.
My problem if the following:
- Hazelacast jar file needs Java 1.6 or above, so I must compile my application with 1.6
- The new functionality using hazelcast will only be activated on demand by settings a new parameter. This means that it will not be used in the 2000 already installed blackboxes.
- All new blackboxes will be installed with Jave 1.6 or above to be able to use the Hazealcast functionality.
My problem is that I want to have a unique source code and unique version of my application for old blackboses using Java 1.5 and new blackboxes using 1.6 or above.
In the beginning, my idea was to always compile with version 1.5 and make sure that the new functionality would only be activated in blockboses using java 1.6 or above. This option is not working, because when I compile with 1.5, the compiler complains that Hazelcast jar file needs 1.6 :(
The second option would be to compile with 1.6, but then I cannot be sure that my application will still work properly on all blackboxes using 1.5. :(
I'm would like to know if someone here would know how to solve this kind of problem?
Just let me know if my explanation is not clear ;)
Thanks in advance for your help.
JVM is Backward compatible.You can run almost all code from Java 1 on Java 8.
So the best way is to use the option two. Compile it with 1.6 on some testing machines. And if it works( which most probably will) you don't have to make much change to the application .
You can compile your code to Java 1.5 bytecode using JDK 1.6, just take care of the following:
-source=1.5 and -target=1.5 compiler options
bootclasspath should point to rt.jar from JRE 1.5
See this post for more info: http://www.draconianoverlord.com/2014/04/01/jdk-compatibility.html
The post also recommends simply building your application with older JDK if possible. You need to figure out a different build process and exclude incompatible libraries from the classpath. If you use Maven, consider having two pom.xml files, with an optional parent file.

Running java application on different JDK from original

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.

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