7

I have created an executable JAR file developed on Java version 8. The JAR file was opening on double click. But as the Oracle applications support only Java 6, I had to install JRE 6, but then after the JRE 6 installation, my executable JAR file is not opening.

I have set the JDK 8 bin path in Path environment variables. Is there a solution for this problem? Why is the JAR file not opening after two Java versions in the system?

JAR should open even if two versions 6 and 8 of Java are installed in the system.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
coding_with_sabs
  • 121
  • 1
  • 2
  • 10
  • 8
    *Can we install two versions of Java JDK in windows* Yes. *Why is the jar not opening after two JAVA versions in system?* Because Java 6 can't run Java 8 compiled code, you should be getting an error, and that error should have led you to the solution (recompile your application with Java 6, because Java 8 can run Java 6 compiled code). – Elliott Frisch Apr 01 '19 at 05:12
  • Hi Elliott :), Thank you for responding. My application will support only Java 8 and above.Do you have another suggestion? – coding_with_sabs Apr 01 '19 at 05:16
  • 2
    If your application will only support Java 8 and above, why have you installed Java 6? – Elliott Frisch Apr 01 '19 at 05:19
  • @JTechseeker set default path of your JRE to 6 – Mustahsan Apr 01 '19 at 05:21
  • @ElliottFrisch : Hi Elliot, We are using oracle ERP instance which require java 6 for opening forms. – coding_with_sabs Apr 01 '19 at 05:39
  • @Mustahsan: Hello Mustahsan, Your solution did'nt help :( – coding_with_sabs Apr 01 '19 at 05:48
  • 1
    I often find when installing multiple JDKs on the same system that the last one that installed 'wins' when it comes to the command line. Try reinstalling the highest-versioned JDK last. – prunge Apr 01 '19 at 05:50
  • Regarding JAVA 8 you can get Amazon Corretto or AdoptOpenJDK as a ZIP file. Then you do not have to care about environment variables. – Bernhard Döbler Apr 01 '19 at 09:22
  • @prunge : Hi Your solution solved my issue. I had to reinstall java 8,however java6 was still there in my system and now, forms also working and executable jar also working. Thank you. – coding_with_sabs Apr 03 '19 at 09:37

4 Answers4

11

You are facing a backward compatibility problem. Backwards compatibility means that you can run a Java 6 program on a Java 8 runtime, but not the other way around.

You can run a lower configuration on a higher configuration, not vice-versa

There are several reasons for that:

  1. Bytecode is versioned and the JVM checks if it supports the version it finds in .class files.
  2. Some language constructs cannot be expressed in previous versions of bytecode.
  3. There are new classes and methods in newer JREs which won't work with older ones.

If you really, really want (tip: you don't), you can force the compiler to treat the source as one version of Java and emit bytecode for another, using something like this:

javac -source 1.8 -target 1.6 MyClass.java

You can compile your code to Java 1.6 bytecode using JDK 1.8. Just take care of the following:

  • -source=1.8 and -target=1.6 compiler options
  • If you use Maven, consider having two pom.xml files, with an optional parent file.

Source: Can program developed with Java 8 be run on Java 7?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
1

I am not sure if this solution going to work or not. Try to run command java -version and look if it returns java 6 or 8 path. Also try to give path of JDK 8 as JAVA_HOME variable and add that into path like this path=%JAVA_HOME%/bin and see if it works. If you get the java 6 as java version try to use above method and then install JRE 6

Akshaya KAushik
  • 31
  • 1
  • 12
1

Hi All Thank you for your response. I kept java6 and reinstalled java8 and now forms and jar both are working!.

coding_with_sabs
  • 121
  • 1
  • 2
  • 10
-1

In the short term,
the answer is yes. Since both JDK files are downloaded as jar fils it will ok to download both jar files. The reason to not opening after two java versions is as @Elliott said: "in the system is Java 6 can't run Java 8 compiled code, you should be getting an error." That's exactly true but the problem is how to use multiple versions of JDK in a single machine.

Then we have to move on to long term,
The tricky thing is to manage these multiple JDKs and IDEs. It’s a piece of cake if I just use Eclipse for compiling my code because the IDE allows me to configure multiple versions of Java runtime. Unfortunately (or fortunately), I have to use the command line/shell to build my code. So, it is important that I have the right version of JDK present in the PATH and other related environment variables (such as JAVA_HOME).

Manually modifying the environment variables every time I want to switch between JDKs, isn’t a happy task. But, thanks to Windows Powershell, I’m able to write a script that can do the heavy lifting for me.

Basically, what you want to achieve is to set the PATH variable to add the Java bin folder and set the JAVA_HOME environment variable and then launch the correct Eclipse IDE. And, I want to do this with a single command. Let’s do it.

  1. Open a Windows Powershell.
  2. I prefer writing custom Windows scripts in my profile file so that it is available to run whenever I open the shell. To edit the profile, run this command: notepad.exe $profile - the $profile is a special variable that points to your profile file.
  3. Write the below script in the profile file and save it.

function myIDE{ $env:Path = “C:vraajavajdk7bin;” $env:JAVA_HOME = “C:vraajavajdk7” C:vraaideeclipseeclipse set-location C:vraaworkspacemyproject play }

function officeIDE{
$env:Path  = "C:vraajavajdk6bin;"
$env:JAVA_HOME = "C:vraajavajdk6"
C:officeeclipseeclipse
}
  1. Close and restart the Powershell.
  2. Now you can issue the command myIDE which will set the proper PATH and environment variables and then launch the Eclipse IDE.

As you can see, there are two functions with different configurations. Just call the function name that you want to launch from the Powershell command line (myIDE).

If any issue please put a comment below!

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63