I have program compiled on java 6. And library compiled on java 7. Can I call from java 6 compiled code to java 7 compiled code or I will have run time errors. I know that I will have errors on compilation, but i will change jar after compilation. I run my application on jvm 7.
-
1This is unclear. Do you want to run a Java 6-compiled application that depends on a Java 7-compiled library on a Java 6 VM? Java 7 VM? – Tunaki Nov 22 '15 at 11:54
-
If you get compilation errors, how do you complete your application? – Thilo Nov 22 '15 at 11:55
-
You can execute a java 6 compiled program in java 7 but vice versa is not possible e.g Strings in switch will not compile in java 6. If you want everything to compiled and executed in java 6 you can use target flag in java so that it works fine in lower versions. – Shriram Nov 22 '15 at 11:55
-
I call from code compiled on java6 to code compiled on java7 on jvm 7. will I have troubles with it? – Anton Nov 22 '15 at 11:57
-
You will get exception Exception in thread "main" java.lang.UnsupportedClassVersionError:
: Unsupported major.minor version 51.0 because the “jar” is compiled with JDK 1.7, but you try to run in a JDK 1.6 environment – Naruto Nov 22 '15 at 11:58 -
Shriram i don't have compilation errors. I have old version of library compiled on java 6. – Anton Nov 22 '15 at 11:59
-
If you try to compile on Java6 and it fails (because the compiler could not load the Java7 library jar file), then you don't have "code compiled on Java 6". If the compilation does not fail on Java 6, you are probably fine. But why use such an ancient compiler? Try to match the JDK you intend to deploy to for best results. – Thilo Nov 22 '15 at 11:59
-
If you compile against one version of the library and then deploy a newer one, this may or may not work. Check the documentation of the library for how compatible they are. Ideally, don't do that. Develop, test and deploy the same versions of all libraries. – Thilo Nov 22 '15 at 12:00
-
naruto - i run it on jvm 7 i in my test it's works. But I carry if it works in all cases. – Anton Nov 22 '15 at 12:01
-
Thilo - it's my own library – Anton Nov 22 '15 at 12:02
2 Answers
In general Java 7 is backward compatible, so you can use libraries compiled with Java 6.
For example: If your library compiled with Java 6 uses a javax
package, it will use the library comming with Java 7 and compiled with Java 7.
I use Apache CXF 2.4.6 (compiled with Java 5) with Java 7. Apache CXF calls Servlet API (compiled with Java 6) of JBoss 7.

- 15,689
- 25
- 79
- 125
-
It's not a question. I ask if I can call from code compiled on lower jdk to code compiled in upper jdk. If code compiled on lower jdk ran on upper jvm. – Anton Nov 22 '15 at 12:09
-
@Anton: Sure it is, because your library compiled with Java 6 uses Java 7 libraries comming with Java 7. – dur Nov 22 '15 at 12:11
Yes, you can, but there are issues with the classfile formats; see this question for the exact mapping of Java versions and classfile format versions.
Specifically, if you have source code A that you are compiling with JDK 6 but you have JAR in the compilation path that has .class files that are with major version 51, then the compilation will fail because the compiler will not be able to load the class files.
However, when compiling you can specify a '-target 1.6' flag so that the generated classfile is compatible with Java 6. If you have access to the source of your library you can recompile it with that target so that it's compatible with a 1.6 compiler.
Lastly (and obviously), the classfile format version must be understood by the JVM.

- 1
- 1

- 16,017
- 2
- 36
- 40