How do I compile a .java with support for older versions of Java?
Yes, you can set the version of compiler at compile time. And compile your java code into old versions of java.
Cross-Compilation Example
Here we use javac to compile code that will run on a 1.4 VM.
% javac -target 1.4 -bootclasspath jdk1.4.2/lib/classes.zip \ -extdirs "" OldCode.java
You might also need following parameter to set denote the version of your code.
-Specifies the version of source code accepted.
It seems one cannot set a -source version higher than the -target version. $javac abc/def/HelloDriver.java -source 1.7 -target 1.4 javac: source release 1.7 requires target release 1.7
As of JDK 9, javac support a new option for cross-compiling
javac -source N -target N –bootclasspath rtN.jar
Compiling code for java 8 using the JDK 10 I ran the command javac —release 8 Main.java The result was runnable on java 8.
Doesn’t require an old version of JDK, compiles back as far as version 6 (JDK 10), and only the Unicode version is pulled from the more recent platform.
Old does not mean bad, especially when new means paid or limited features. Also incompatibility with stable and free does not mean good.
You can use javac -target 1.5 .
If you’re using a build system, Eclipse or some other IDE to build jars, please specify which one.
i have netbeans, but i can just use text files. How would i apply that into all .java’s in a directory?
With netbeans right click on your project, go to properties and change the «Source/Binary format» from the dropdown towards the bottom of the dialog that opens
You can specify the target version of the compiler lower than the build java version (all answers basically said that) but you should not: it is quite messy as it only works if you also supply the class libraries for the actual target version. If you do that you are much better off with simply using this old Java version to compile. Much cleaner and less stuff to setup. For Java 9 the options are a bit nicer but you still need an old JDK lying around for the rt.jar.
What you can do is in your ide and dedvelopment cycle use the -target / -bootclasspath trick to get faster compiler and better warnings, but IT and release builds then should be done with the target version.
How can I build a jar for a previous Java version?
I am trying to compile my code and run it on a different server. The problem is my JRE version is Java version «1.6.0_13» and that on the server is Java version «1.4.2». This gives me «unrecognized class file version» exception when I try to run the jar on the server. I cannot compile my code on the server because of various dependencies I will have to set up. So I need to know if there is a way to «use Eclipse» [this is easier than command line since it takes care of dependencies] and compile my stuff using 1.4.2 instead 1.6.0_13. Do I have to uninstall my JRE from my machine, then reinstall previous version and then compile or is there an elegant and cleaner way?
I would suggest you upgrade Java 6 update 13 to Java 6 update 23 as the former version is quite old and there have been many bugs fixed. This goes 10x for Java 1.4.2
4 Answers 4
You should install 1.4.2 along side 1.6 on your machine, and configure that eclipse project to use the 1.4.2 JDK. Other projects may be configured to use the 1.6.
Technically you can compile to 1.4.2 compatibility with JDK 1.6, but odds are you will run into library problems, so it is not generally worth doing.
hey .. thnx. I downloaded j2sdk-1_4_2_19-linux-i586.bin . Can you tell me how can I configure eclipse to use it for my package. In eclipse I have under buildpath=>j2sdk1.4.bin and also jdk1.6.0 [if I take this out ..I get bunch of compile errors] . while in eclipse buildpath=>orderandexport I have checked j2sdk and unchecked jdk1.6.0 . I tried with this but I think its not picking it up ..
I don’t use eclipse much, but here are some instructions: java-forums.org/eclipse/4278-how-change-jdk.html
You can compile for java 1.5 with the parameters
javac -source 1.5 -target 1.5 -bootclasspath "C:\Program Files (x86)\Java\jre1.5.0_21\lib\rt.jar"
Do not forget the bootclasspath, or you will have problems with functions like String.isEmpty() which don’t exist in the java 1.5.
Eclipse Mars (4.5.1)
Change JDK compliance
Window > Preferences > Java > Compiler > JDK Compliance > change Compiler compliance level:
If you want to change other details, you can uncheck «Use default compliance settings».
Press OK and in the «Compiler Settings Changed» window choose:
- Yes if you want to build all projects from the workspace:
- No if you want to build only one/some project(s) later
- Select the project (click on the left side — E.g.: in the Package Explorer) >Project >Build Project
Export jar
Right click on the project (from the left side — E.g.: in the Package Explorer) > Export. > type «jar» to filter the options you have > choose what you want (probably JAR file or Runnable JAR file) > Next > add the details > Finish
How to tell the range of java versions a jar is compiled for
Note that a class file compiled for Java 1.0something will still (in theory) run on the latest JVM. There is no «max version» for a class file.
3 Answers 3
generally MANIFEST.MF file has this information as an attribute, if you don’t find it, extract the jar and choose a class and do
$javap -verbose SomeClass.class | grep 'major' major version: 50
Here is the structure of a compiled java class file stated from this link :
- Magic Number: 0xCAFEBABE
- Version of Class File Format: the minor and major versions of the class file
- Constant Pool: Pool of constants for the class
- Access Flags: for example whether the class is abstract, static, etc.
- This Class: The name of the current class
- Super Class: The name of the super class
- Interfaces: Any interfaces in the class
- Fields: Any fields in the class
- Methods: Any methods in the class
- Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)
As you can see, the second point is the version. Therefore, download an hex editor, open any .class file located in the jar and you will be able to read the version.
Edit : Altough I never verified, the byte offset for the version is suppose to be between 4 to 7, once again from the same link.
Edit 2 : If you prefer doing it with command, check this thread : how to check the jdk version used to compile a .class file
There is no such information in the Jar file, especially when considering that your term “java versions a compiled jar file will work with” heavily depends on how you define “will work”. An application could start, run for a second and then terminate with an exception. Does that already fulfill your definition of “does work”?
As said by others, there is a version number within the class files. You can find the information about how to map that version number to Java version here.
However, that version number only tells you the minimum JVM version that is needed to load that class file. It does not tell you which API it targets. It’s perfectly legal to compile a class file compatible with a Java 1.1 JVM but using Java 8 APIs.
You could scan all class and member references of all class files within a Jar file and compare to the official API versions, however that only tells you which is effectively used, not what’s intentionally targeted. E.g. the application could still rely on certain bugs being fixed or missing functionality filled into already existing APIs. E.g. whether an application relies on the requirement “the JRE’s AWT can load PNG images with correct transparency support” can not be concluded by looking at the class file version number or at which API it refers to.
Specifying which Java version an application or library requires is beyond the scope of simple Jar files, e.g. you may have a look at OSGi or Java Webstart.
how to check the version of jar file?
I am currently working on a J2ME polish application, just enhancing it. I am finding difficulties to get the exact version of the jar file. Is there any way to find the version of the jar file for the imports done in the class? I mean if you have some thing, import x.y.z; can we know the version of the jar x.y package belongs to?
17 Answers 17
Decompress the JAR file and look for the manifest file ( META-INF\MANIFEST.MF ). The manifest file of JAR file might contain a version number (but not always a version is specified).
As i told that I am working on enhancing the application, I have downloaded the jar files and I know the version of the jar files. But I wanted to know the version of the jar which the package belongs to, I hope you get it.
To explain you more, I have import x.y.z, I know that x.y belongs to a-4.1.jar, but the application which i am working has been developed long back, and I dono what version of a.jar file have they used, I am worried about the version because, some of the classes have been depreciated from the jar of the older version (I feel so), because, even though I have jar in the library, I find the import cannot be resolved
So is it that, if you know the version of jar which they used while building the application, you would also use the same version of jar to execute the application?
If that is what your requirement is, then am afraid that you will have to rebuild the application with exclusion of deprecated jars. Because finding version number associated with a given jar is like one to one function. Only one version number for one jar file. But finding which version of jar was used at the time of development of application sounds highly impossible to me unless the developer attaches the required jars with the application.
Recording version in MANIFEST.MF appears to be optional. There’s no version recorded in various sqljdbc42.jar files that I’ve used with Cognos, yet Cognos is able to report a version (4.2.6420.100). Where is it getting this version from if it’s not recorded in the manifest?
You need to unzip it and check its META-INF/MANIFEST.MF file, e.g.
unzip -p file.jar META-INF/MANIFEST.MF
Just to expand on the answers above, inside the META-INF/MANIFEST.MF file in the JAR, you will likely see a line: Manifest-Version: 1.0 ← This is NOT the jar versions number!
You need to look for Implementation-Version which, if present, is a free-text string so entirely up to the JAR’s author as to what you’ll find in there. See also Oracle docs and Package Version specificaion
Just to complete the above answer.
Manifest file is located inside jar at META-INF\MANIFEST.MF path.
You can examine jar’s contents in any archiver that supports zip.
Each jar version has a unique checksum. You can calculate the checksum for you jar (that had no version info) and compare it with the different versions of the jar. We can also search a jar using checksum.
Basically you should use the java.lang.Package class which use the classloader to give you informations about your classes.
String.class.getPackage().getImplementationVersion(); Package.getPackage(this).getImplementationVersion(); Package.getPackage("java.lang.String").getImplementationVersion();
I think logback is known to use this feature to trace the JAR name/version of each class in its produced stacktraces.
Thought I would give a more recent answer as this question still comes up pretty high on searches.
Run the following on the CLi jar file:
unzip -p jenkins-cli.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0 Built-By: kohsuke Jenkins-CLI-Version: 2.210
The CLi version is listed above.
To get the Server Version, run the following:
java -jar ./jenkins-cli.jar -s https:// -auth @.com: version
(the above will vary based on your implementation of authentication, please change accordingly)
Dec 23, 2019 4:42:55 PM org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar getOrCreateProvider INFO: getOrCreateProvider(EdDSA) created instance of net.i2p.crypto.eddsa.EdDSASecurityProvider 2.210
This simple program will list all the cases for version of jar namely
- Version found in Manifest file
- No version found in Manifest and even from jar name
- Manifest file not found
Map jarsWithVersionFound = new LinkedHashMap(); List jarsWithNoManifest = new LinkedList(); List jarsWithNoVersionFound = new LinkedList(); //loop through the files in lib folder //pick a jar one by one and getVersion() //print in console..save to file(?)..maybe later File[] files = new File("path_to_jar_folder").listFiles(); for(File file : files) < String fileName = file.getName(); try < String jarVersion = new Jar(file).getVersion(); if(jarVersion == null) jarsWithNoVersionFound.add(fileName); else jarsWithVersionFound.put(fileName, jarVersion); >catch(Exception ex) < jarsWithNoManifest.add(fileName); >> System.out.println("******* JARs with versions found *******"); for(Entry jarName : jarsWithVersionFound.entrySet()) System.out.println(jarName.getKey() + " : " + jarName.getValue()); System.out.println("\n \n ******* JARs with no versions found *******"); for(String jarName : jarsWithNoVersionFound) System.out.println(jarName); System.out.println("\n \n ******* JARs with no manifest found *******"); for(String jarName : jarsWithNoManifest) System.out.println(jarName);