5

My Java 7 project is building fine in Gradle even though it depends on java.util.Optional.

I am very, very confused. java.util.Optional was only a thing in Java 8, right?

I have a Gradle project, and I have set both sourceCompatibility and targetCompatibility to 1.7.

In one class in this module I import java.util.Optional, which I thought wasn't included until Java 8.

If I run gradlew clean assemble, the compiler even gets called with right options. There is a warning that sounds a bit fishy though. However, the jar gets built just fine.

15:51:12.998 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter] Executing actions for task ':core:compileJava'.
15:51:13.000 [DEBUG] [org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler] Compiler arguments: -source 1.7 -target 1.7 -d /Users/deejay/workspace/number-jumble/core/build/classes/main -g -encoding UTF-8 -classpath /Users/deejay/workspace/number-jumble/model/build/libs/model-1.0.jar:/Users/deejay/.gradle/caches/modules-2/files-2.1/com.badlogicgames.gdx/gdx/1.3.1/18995f8b7b19118975722370818023c76a86fdf4/gdx-1.3.1.jar:/Users/deejay/.gradle/caches/modules-2/files-2.1/com.badlogicgames.gdx/gdx-freetype/1.3.1/1f647429753c8c91fb8fc477ffe5a4c9cde2f429/gdx-freetype-1.3.1.jar:/Users/deejay/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/NumberJumbleGame.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/complexity/ChangeComplexityScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/libgdx/SizeToAndLayoutAction.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/libgdx/TimeLabel.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/services/AppropriatelySizedFontService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/services/DateService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/GameSessionUi.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/HardcodedStreakAssessmentService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/LandscapeGameSessionUi.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/PortraitGameSessionUi.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/RackScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/RackSummaryScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/StreakAssessmentService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/splash/ReturnSplashScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/GdxLocalStatisticsRepository.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/MapBackedStatisticsRepository.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/StatisticsRepository.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/StatisticsService.java
15:51:13.001 [INFO] [org.gradle.api.internal.tasks.compile.jdk6.Jdk6JavaCompiler] Compiling with JDK Java compiler API.
15:51:13.007 [ERROR] [system.err] warning: [options] bootstrap class path not set in conjunction with -source 1.7
15:51:13.185 [ERROR] [system.err] 1 warning

Am I going mad?

DeejUK
  • 12,891
  • 19
  • 89
  • 169
  • "[ERROR] [system.err] warning: [options] bootstrap class path not set in conjunction with -source 1.7" means that compiler will enforce Java 7 syntax but it can't guarantee that you use only classes available in Java 7. – Oleg Estekhin Aug 21 '15 at 15:09
  • Thanks Oleg - is that "can guarantee" meant to be "can't guarantee"? – DeejUK Aug 21 '15 at 15:10
  • possible duplicate of [Gradle compileJava task warning: \[options\] bootstrap class path not set in conjunction with -source 1.6](http://stackoverflow.com/questions/16679593/gradle-compilejava-task-warning-options-bootstrap-class-path-not-set-in-conju), also [warning: [options] bootstrap class path not set in conjunction with -source 1.5](https://stackoverflow.com/questions/7816423/warning-options-bootstrap-class-path-not-set-in-conjunction-with-source-1-5?rq=1) – Oleg Estekhin Aug 21 '15 at 15:10
  • 1
    yes, I meant "can't", edited the comment. [How to set Gradle `options.bootClasspath` in an os independent manner?](https://stackoverflow.com/questions/22681544/how-to-set-gradle-options-bootclasspath-in-an-os-independent-manner) can be also of interest to you. – Oleg Estekhin Aug 21 '15 at 15:15

1 Answers1

4

You aren't going mad :D

The java.util.Optional was introduced in Java 8. That is correct. And I guess you are compiling against Java 8. So the classes used (and checked) are in fact the ones of Java 8.

sourceCompatibility only affects the syntax styles used. Not the classes used. With this set to 1.7 you can't use lambda expressions for example. But you can use the classes of Java 8. How ever if you use the classes of the new java, your application will fail at runtime.

targetCompatibility only affects the compiled language level. So the version of the java binary class code. Again, access to java classes is possible during compilation, but it will fail if it is executed with a older version of java, simply because the referenced class is not present.

Nitram
  • 6,486
  • 2
  • 21
  • 32
  • Blimey - ten years as a Java developer and I never realised this is how it worked! – DeejUK Aug 21 '15 at 15:14
  • 2
    @Deejay, it worth mentioning the upcoming [JEP-247](http://openjdk.java.net/jeps/247) which (if implemented) will make the compatibility compilation more robust. It will actually check whether the given class or method existed in the selected Java version! – Tagir Valeev Aug 21 '15 at 15:56