# Sourcecompatibility targetcompatibility usage reasons

**URL:** <https://discuss.gradle.org/t/sourcecompatibility-targetcompatibility-usage-reasons/25133>\
**Category:** Help/Discuss\
**Created:** [December 14, 2017, 5:23pm UTC](https://discuss.gradle.org/t/sourcecompatibility-targetcompatibility-usage-reasons/25133 "2017-12-14T17:23:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ttrungvo](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@ttrungvo](https://discuss.gradle.org/u/ttrungvo)\
**Post date:** [December 14, 2017, 5:23pm UTC](https://discuss.gradle.org/t/sourcecompatibility-targetcompatibility-usage-reasons/25133/1 "2017-12-14T17:23:10Z")

</div>

It seems like I can set sourcecompatibility and targetcompatibility through java-plugin

I wonder what’s the reasons for us to use sourcecompatibility/targetcompatibility other than to be backward compatible with older JDK?

would it be easier to upgrade to latest java if sourcecompatibility/targetcompatibility isn’t set?

Should sourcecompatibility and targetcompatibility be used at all?  
Thank you very much for your help.

Cheers,  
Tony

---

<div class="post-metadata">

**Author:** ![nerro](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/nerro/32/5340_2.png) [@nerro](https://discuss.gradle.org/u/nerro)\
**Post date:** [December 14, 2017, 9:45pm UTC](https://discuss.gradle.org/t/sourcecompatibility-targetcompatibility-usage-reasons/25133/2 "2017-12-14T21:45:07Z")

</div>

You can make sure that the source code is compatible with certain JDK, but should generate class files for use on another version of JDK. Why you would do this is another matter 😉.

I think default Gradle uses installed JDK versions, isn’t?

[https://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#crosscomp-options](https://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#crosscomp-options)

---

<div class="post-metadata">

**Author:** ![xenoterracide](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/xenoterracide/32/5232_2.png) [@xenoterracide](https://discuss.gradle.org/u/xenoterracide)\
**Post date:** [December 15, 2017, 3:07am UTC](https://discuss.gradle.org/t/sourcecompatibility-targetcompatibility-usage-reasons/25133/3 "2017-12-15T03:07:50Z")

</div>

> [@ttrungvo](#):
>
> would it be easier to upgrade to latest java if sourcecompatibility/targetcompatibility isn’t set?

yes? no? seems like the default right now is java `1.8` so it would be easier to upgrade to 9 by setting them to that.

but yes, this is primarily a way to be compatible with an older JVM, and or to set yourself to whatever language version your team want to use.

---

<div class="post-metadata">

**Author:** ![mauromol](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mauromol/32/994_2.png) [@mauromol](https://discuss.gradle.org/u/mauromol)\
**Post date:** [December 21, 2017, 4:21pm UTC](https://discuss.gradle.org/t/sourcecompatibility-targetcompatibility-usage-reasons/25133/4 "2017-12-21T16:21:56Z")

</div>

`sourceCompatibility` is a way to declare which version of Java your source code is compatible with. This has many implications, like telling the compiler which rules should apply to parse the code (for instance, `enum` started to be a reserved keyword only in Java5+) or how the IDE that integrates with your Gradle script should set up your project class path (for instance, if `sourceCompatibility` = 1.7, it should bind your project to a Java 7 JRE).

`targetCompatibility`, instead, is a way to declare which kind of byte code the compilation process should produce. By default, if not specified, `targetCompatibility`=`sourceCompatibility`. If you specify a `targetCompatibility` lower than `sourceCompatibility`, you’re trying to do something interesting in theory, but risky in practice, because either at compile time or just at runtime you may incur in “class not found” errors due to class path mismatch between what your source needs and what your compiler/runtime provides.
