Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJeremiah Shore
31,168 PointsIssues with Gradle: JDK version and language level
While working through the Intro to Java Web Development with Spark course, I've encountered some repeat issues with Gradle.
The general problem I seem to have is that every time Gradle is refreshed and dependencies are checked/updated, my project reverts to an incorrect JDK version and language level. This has led to some repeat manual work. Each time I would need to go to File > Settings > Build, Execution, Deployment > Compiler > Java Compiler and then set the Target bytecode version from 1.5 to 1.8. I would also need to navigate to File > Other Settings > Project Settings > Project and change the Project language level to 8 so that lambdas could be supported.
After some reading in the Gradle Documentation I discovered that I could set the courceCompatibility = 1.8 in build.gradle to resolve my issues. Note that this documentation does not recommend setting the languageLevel directly, but instead the sourceCompatibility or targetCompatibility.
I've shared this info mostly for the benefit of others. However, the questions I have for more advanced users are: Am I losing any functionality or compatibility by doing this? Should I be doing anything differently?
2 Answers
Chris Ramacciotti
Treehouse Guest TeacherThe sourceCompatibility
and targetCompatibility
properties are used as command line arguments when compiling your source code. For example, using this in your Gradle build file:
sourceCompatibility = 1.7
targetCompatibility = 1.7
Will result in a command like this:
javac Main.java -source 1.7 -target 1.7
The problem is that if your source includes language features introduced in Java 8, your code won't compile. In another scenario, let's say that you don't use any new language features, like lambdas, but you've used the new Java 8 date/time API. In this case, your code might compile (depending on IDE settings), but when trying to run it on a Java 7 JVM, you'll get runtime errors.
Long story short (maybe too late for that?): unless you know that your bytecode will be running on an earlier JVM version, use the latest by setting your source & target compatibility to the latest version, or by deleting those entries altogether.
As far as configuring IntelliJ to honor the IDE's JDK settings in the generated build.gradle file, well, I haven't been successful there. But, if you figure out how to do that, be sure to share it here! :)
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsHi, Jeremiah.
Put this to our common java slack channel. I don't remember for now video. But I do remember that in one of the courses, Chris Ramacciotti, just commented this and it was fine for him. I think that in most of the project this line is not even used, because that what intellijidea is complaining sometimes. So put the question in Slack, mention Chris. I'll try to find a video, where he comments it out, may be it will be of help to you.
Jeremiah Shore
31,168 PointsJeremiah Shore
31,168 PointsThanks Chris Ramacciotti! I'm content knowing that I don't really need to worry about this unless, as stated, I know my code will be running on an older JVM. That is, so long as the project is staying configured for the correct language level and JDK version upon each Gradle refresh ;)