Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional in Java 1.7? Gradle Ignoring Source/Target Compatibility?

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?

like image 279
EngineerBetter_DJ Avatar asked Sep 26 '22 23:09

EngineerBetter_DJ


1 Answers

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.

like image 189
Nitram Avatar answered Sep 29 '22 23:09

Nitram