0

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.

10
  • 1
    This 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? Commented Nov 22, 2015 at 11:54
  • If you get compilation errors, how do you complete your application? Commented Nov 22, 2015 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. Commented Nov 22, 2015 at 11:55
  • I call from code compiled on java6 to code compiled on java7 on jvm 7. will I have troubles with it? Commented Nov 22, 2015 at 11:57
  • You will get exception Exception in thread "main" java.lang.UnsupportedClassVersionError: <package name> : 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 Commented Nov 22, 2015 at 11:58

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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: Sure it is, because your library compiled with Java 6 uses Java 7 libraries comming with Java 7.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.