0

Can we use a JDK 1.8 compiler to compile source code whose sourceCompatibility option is set to 1.7.

ModuleA has this set to 1.7 and ModuleB and ModuleC has this set to 1.8. What compiler should I use for compiling all the above modules. I am assuming it should be JDK 1.7.

Rpj
  • 5,348
  • 16
  • 62
  • 122

3 Answers3

1

You can run into problems when compiling 1.7 code with an 1.8 compiler. Merely setting -source and target to 1.7 is not sufficient and you should get a warning during compilation that complains about the bootstrap class path.

If you accidentally use a class or method introduced with 1.8 - e.g. this one, the compiler won't catch it. Running your code on a 1.7 runtime will fail then with NoSuchMethodError.

So, if you intend to run on your code on a 1.7 runtime, you should either compile with an 1.7 compiler or set the bootclasspath.

Frank Neblung
  • 3,047
  • 17
  • 34
0

I assume you have two questions:

Use jdk8 to compile a program written for java7: Yes, java is famous for its backward-compatibility.

Use what jdk to compile a project with modules written with different source compatability: Use the lastest version. In your case, jdk8. If something went wrong, install and use multiple jdks.

EDIT: It's true that problems may come if you compile a java 7 prog with jdk8, but in my 200+ local builds and 400+ CI builds, nothing went wrong. The problem rate is so low that I merely take it into account. If you consider it INSANELY important, ALWAYS install and use the matching jdks.

glee8e
  • 6,180
  • 4
  • 31
  • 51
0

I agree with Frank, you may face runtime issues. An actual one we found by using newer JDK to compile code that should run on older JRE.

Community
  • 1
  • 1
Fernando Miguélez
  • 11,196
  • 6
  • 36
  • 54