How to run a jar in different versions of Java

1

How is it possible to run a jar in different versions of Java? To give a very basic example, download a jar-calc (a calculator) a simple jar and I could run it on a PC with winXP java 7 and the same jar run it on a Windows 7 java 8 No problems.

I did a hello world with a JOptionPane.showMessageDialog and I can run it in a machine with Java 8.60 but I take it to any other machine with java including 8 but another release and it throws me that the jar is corrupt. If I install the same version of Java works without problems in another machine.

    
asked by Juanito 01.10.2017 в 15:51
source

1 answer

1

It will depend a lot on the version with which you compile your .java files. Usually, in the IDE, when you create the project, you indicate the version of JDK that you are going to use. Apparently, it seems that you created the jar-calc project using JDK 7 (1.7.x), so it can be run with Java 7 or higher, as you mentioned, Java 8.

If you want your jar to be able to run with previous versions, you should make sure that when compiling your file the expected version is used. You can do this in multiple ways (examples pointing to Java 6).

  • In the IDE (Netbeans, Eclipse, IDEA, etc), you must ensure in the properties of the project, that points to the specific version of Java p.e. Java 6.

  • If you use maven, you can indicate the compiler and execution version by indicating it as properties in your pom (source: link ):

    <project>
      [...]
      <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
      </properties>
      [...]
    </project>
    
  • In gradle, you can add these lines in the build.gradle file:

    apply plugin 'java'
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
    
  • Executing from the command line, when using javac , you can indicate the previous version (source: docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html):

    javac -source 1.6 -target 1.6 <clases y carpetas a compilar>
    
  • answered by 01.10.2017 в 16:08