I have created Spring Boot project with
SDK 11. Java Version 11.0.3
Kotlin as language
Gradle
I'm following this Tutorial:
https://scotch.io/#grahamcox82/how-to-build-a-simple-rest-api-with-kotlin-and-spring-boot
I'm trying to
import java.time.Instant
in my Kotlin data class
And have an error
Unresolved reference: java
build.gradle.kts file:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.1.6.RELEASE"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.2.71"
kotlin("plugin.spring") version "1.2.71"
}
group = "com.smight"
version = "0.0.1"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
Maybe I should install java library? How can I check this?
Can anyone help please?
According to some research, this error can appear in this conditions :
You created a Kotlin2Js project instead of Kotlin JVM (source), try to recreate your project by selecting the right project type
or
You are using a Kotlin version that does not support JDK 11 (source), install JDK 8 instead and reconfigure your JAVA_HOME environment variable
It may as well be an error in your build.gradle file, copy/paste it in your question if the solutions above doesn't work
To get a more specific error you should first clean the autogenerated files
$ ./gradlew clean
In a modularized Spring project using Kotlin DSL the unresolved reference error could occur because the submodules are bootable.
build.gradle.kts (Project)
...
subprojects {
...
tasks.getByName<BootJar>("bootJar") {
enabled = false
}
tasks.getByName<Jar>("jar") {
enabled = true
}
}
GL
The problem was that JDK was not correct found from IntelliJ
I solved the problem so:
File -> Project Structure -> SDKs -> "+"
Find the path to your SDK where it is installed
New Project
Copy/Paste
Rebuild
Related
I'm going through modularizing my own projects. One of my classes uses the following imports:
import com.gluonhq.charm.down.Services;
import com.gluonhq.charm.down.plugins.StorageService; // error here on "com.gluonhq.charm.down.plugins"
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.control.Dialog;
import com.gluonhq.charm.glisten.control.SettingsPane;
import com.gluonhq.charm.glisten.control.settings.DefaultOption;
import com.gluonhq.charm.glisten.control.settings.Option;
import com.gluonhq.charm.glisten.mvc.View;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
In my module-info.java I have declared:
requires charm.glisten;
requires charm.down.core;
requires charm.down.plugin.storage;
as per auto-fix suggestions of Eclipse. However, I get the error specified for the above line:
The package com.gluonhq.charm.down.plugins is accessible from more than one module:
charm.down.plugin.device,
charm.down.plugin.display,
charm.down.plugin.in.app.billing,
charm.down.plugin.lifecycle,
charm.down.plugin.push.notifications,
charm.down.plugin.runtime.args,
charm.down.plugin.statusbar,
charm.down.plugin.storage
The charm modules are automatically named since they are not Java modules apparently. This could be related to the issue. Before modularizing my projects, there were no such issues. How do I solve this?
build.gradle:
buildscript {
repositories {
jcenter()
google()
mavenCentral()
maven {
url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
}
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'com.gluonhq:client-gradle-plugin:0.1.30'
}
}
plugins {
id 'org.openjfx.javafxplugin' version '0.0.9'
id 'org.beryx.jlink' version '2.21.2'
id 'com.google.osdetector' version '1.6.2'
id 'eclipse'
id 'org.kordamp.gradle.jdeps' version '0.11.0'
}
apply plugin: 'com.gluonhq.client-gradle-plugin'
repositories {
jcenter()
maven {
url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
}
}
sourceCompatibility = 14
targetCompatibility = 14
ext.platform = osdetector.os == 'osx' ? 'mac' : osdetector.os == 'windows' ? 'win' : osdetector.os
dependencies {
compile "org.openjfx:javafx-base:14:$platform"
compile "org.openjfx:javafx-graphics:14:$platform"
compile "org.openjfx:javafx-controls:14:$platform"
compile "org.openjfx:javafx-fxml:14:$platform"
runtimeOnly "org.openjfx:javafx-graphics:14:win"
runtimeOnly "org.openjfx:javafx-graphics:14:mac"
runtimeOnly "org.openjfx:javafx-graphics:14:linux"
compile 'com.gluonhq:charm:5.0.0-jdk9'
compile 'org.reactfx:reactfx:2.0-M5'
compileOnly "org.projectlombok:lombok:1.18.12"
annotationProcessor 'org.projectlombok:lombok:1.18.12'
}
javafx {
version = "14"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
mainClassName = 'com.my.app.MainClass'
jar {
manifest {
attributes 'Main-Class': 'com.my.app.Launcher'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
eclipse {
classpath {
downloadJavadoc = true
downloadSources = true
}
}
wrapper {
gradleVersion = '6.5.1'
}
JDK 14
Eclipse 4.16
buid.gradle declares the dependency 'com.gluonhq:charm:5.0.0-jdk9' (I saw that version 6 exists, should I upgrade?)
The old Gluon jfxmobile plugin, that was used to create mobile applications with Java 1.8 or Java 9, is EOL.
To be able to run those mobile applications with Java/JavaFX 11+, you have to replace that plugin new Gluon Client plugin.
More details:
Client Maven plugin,
Client basic samples.
Documentation.
One main difference, the new plugin uses Maven instead of Gradle. However, thanks to the community, there is also a version of the Client plugin for Gradle, that might be a little bit behind the maven counterpart).
In order to migrate your project to Java 11+ and replace one plugin with the other, you have to modify your build file.
plugins {
// new client plugin
id 'com.gluonhq.client-gradle-plugin' version '0.1.30'
id 'org.openjfx.javafxplugin' version '0.0.9'
id 'org.beryx.jlink' version '2.21.2'
id 'com.google.osdetector' version '1.6.2'
id 'eclipse'
id 'org.kordamp.gradle.jdeps' version '0.11.0'
}
repositories {
jcenter()
maven {
url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
}
}
sourceCompatibility = 14
targetCompatibility = 14
dependencies {
compile 'com.gluonhq:charm:6.0.5'
compile 'org.reactfx:reactfx:2.0-M5'
compileOnly "org.projectlombok:lombok:1.18.12"
annotationProcessor 'org.projectlombok:lombok:1.18.12'
}
javafx {
version = "14"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
gluonClient {
// target = "ios" // uncomment to deploy on iOS
// target = "android" // uncomment to deploy on Android
attachConfig {
version = "4.0.8"
services 'display', 'lifecycle', 'statusbar', 'storage'
}
}
You will notice the main changes:
Charm (Gluon Mobile) is now 6.0+
Charm Down has been renamed to Attach, current version is 4.0.8 (you will have to refactor the package names, like com.gluonhq.charm.down.plugins.StorageService to com.gluonhq.attach.storage.StorageService).
You can still run the project with your JDK, with ./gradlew run (using the JavaFX plugin).
With the new Client plugin you will also be able to create a native image, that will run on desktop (Windows, Linux, MacOS) and mobile (Android, iOS), leveraging GraalVM.
Following the Client requirements, download GraalVM for your host machine from here, set GRAALVM_HOME, and you will be able to run:
// build the native image, it takes some time:
./gradlew clean nativeBuild
// run the native image
./gradlew nativeRun
And if you have a mobile device at hand, only by enabling the target to iOS or Android in your build will build and deploy the native image to that mobile platform.
Currently, I cannot get this project to run, somehow the Gradle cannot find the Lombok module. I'm using
lombok version 1.18.12
OpenJDK 11.0.8
Gradle 6.4
Based on this github issue, then the problem should be solved at this version, but it doesn't work for me.
Here is the error
> Task :Model-library:compileJava FAILED
/home/dauto98/path..to..project/src/main/java/module-info.java:2: error: module not found: lombok
requires static lombok;
below is my gradle.build.kts file
plugins {
java
`java-library`
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation("junit", "junit", "4.12")
compileOnly("org.projectlombok:lombok:1.18.12")
annotationProcessor("org.projectlombok:lombok:1.18.12")
testCompileOnly("org.projectlombok:lombok:1.18.12")
testAnnotationProcessor("org.projectlombok:lombok:1.18.12")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_11
}
my module-info.java file
module my.module.main {
requires static lombok;
}
After a while, I found out that the problem is I didn't turn on module path inference explicitly in the Gradle build file as stated in here
Add this to the gradle.build.kts file:
plugins.withType<JavaPlugin>().configureEach {
configure<JavaPluginExtension> {
modularity.inferModulePath.set(true)
}
}
I am trying to run some sample code from ArcGIS github here.
When I import and try to run the project in IntelliJ Community 2019 I get an error:
Cause: org/openjfx/gradle/JavaFXPlugin has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
My build.gradle:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
ext {
arcgisVersion = '100.6.0'
}
repositories {
jcenter()
maven {
url 'https://esri.bintray.com/arcgis'
}
}
configurations {
natives
}
dependencies {
compile "com.esri.arcgisruntime:arcgis-java:$arcgisVersion"
natives "com.esri.arcgisruntime:arcgis-java-jnilibs:$arcgisVersion"
natives "com.esri.arcgisruntime:arcgis-java-resources:$arcgisVersion"
}
javafx {
version = "11.0.1"
modules = [ 'javafx.controls' ]
}
task copyNatives(type: Copy) {
description = "Copies the arcgis native libraries into the .arcgis directory for development."
group = "build"
configurations.natives.asFileTree.each {
from(zipTree(it))
}
into "${System.properties.getProperty("user.home")}/.arcgis/$arcgisVersion"
}
run {
dependsOn copyNatives
mainClassName = 'com.mycompany.app.App'
}
wrapper {
gradleVersion = '5.0'
}
What I have tried so far
I have downloaded Java 11 SDK and installed it. I changed the Java Version in IntelliJ to 11.
The error is still present even after changing to Java 11.
Update
The run configuration window does not let me select java version:
Similar question here - but no answer.
How do I fix this error?
I found the answer thanks to CrazyCoder. I had to set the JDK version in Intellij in
File>Settings>Build, Execution, Deployment>Build Tools>Gradle
.
Today I started a new JDK 11 project with Gradle 5.0 (using the Gradle Wrapper) and created a basic build script:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.gradle:osdetector-gradle-plugin:1.6.0'
}
}
plugins {
id 'java'
}
apply plugin: 'com.google.osdetector'
ext.platform = osdetector.os == 'osx' ? 'mac' : osdetector.os == 'windows' ? 'win' : osdetector.os
version = '0.1.0'
repositories {
mavenCentral()
}
dependencies {
compile "org.openjfx:javafx-graphics:11:$platform"
}
This - rather basic - build script results in the error
'compile' in 'org.gradle.api.artifacts.dsl.DependencyHandler' cannot
be applied to '(groovy.lang.GString)'
Is that a JDK 11, a Gradle 5.0 or a user error? I've never seen that before.
According to JetBrains support this is a known bug and will be fixed in 2018.3.1.
See: https://youtrack.jetbrains.com/issue/IDEA-203393
The messages only appear within IntelliJ using Gradle 5.0 (e.g. 2018.3).
The build works fine, started from console or from IntelliJ.
Ok, so I'm new to Gradle and Kotlin and I am having a hard time understanding how things glue together here...
I need to configure a project that should run on Java 7 (client limitations -_-) and I want to use Kotlin with it.
Right now I have the following build.gradle file that is working but I want to ask a few things that I couldn't find anywhere else:
buildscript {
ext {
springBootVersion = '1.5.15.RELEASE'
kotlin_version = '1.1.1'
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
group = 'com.springkotlin'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-security')
compile('com.onelogin:java-saml:2.3.0')
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
testCompile group: 'javax.inject', name: 'javax.inject', version: '1'
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
Now the questions:
I have tried using kotlin_version = '1.2.70' (released last few days!) and I got the error KotlinPluginWrapper : Unsupported major.minor version 52.0. I'm guessing then this is due to Kotlin 1.2.X only being able to "compile" (is that the word?) with Java 8+. Is that right? Is 1.1.1 the right version to use here or is there a way to use 1.2.70 that would work with Java 7? Will I be missing a lot of stuff for using it?
I want to understand the 3 kotlin stuff I had to setup on the script. Correct me please:
kotlin-gradle-plugin: Is used to define which version of Kotlin I will be using(?)
apply plugin: 'kotlin': As far as I know from Gradle, this should add tasks to work with Kotlin but running gradle tasks I didn't see anything different... So what is it really for?
kotlin-stdlib-jdk7: I'm guessing this is Kotlin lib of functions, classes, etc. What I don't understand though is the difference between stdlib and stdlib-jdk7. The documentation says it contains "addition extension functions". But which ones? Also, should I define a version for this guy? Or does it automatically picks up the kotlin-gradle-plugin version?
Thanks in advance,
Currently the compiler of the Kotlin language requires JDK 8 to run. A project compiled with Kotlin can target any Java starting from Java 6.
A recipe to setup Gradle build of a project that runs on Java 7 is following:
run Gradle with Java 8 or later
for all Kotlin compile tasks
specify jvmTarget = "1.6" in kotlinOptions
specify path to JDK 7 in jdkHome in kotlinOptions
if your project contains java code specify sourceCompatibility, targetCompatibility convention properties of the Java plugin
specify the following options of all java compile tasks:
isFork = true
forkOptions.javaHome = "<path to JDK 7>"
for all Test tasks specify executable as "<path to JDK 7>/bin/java"
The full sample:
sourceCompatibility = 1.7
targetCompatibility = 1.7
def JDK_7 = System.getenv("JDK_7") // or some other way to get path to JDK 7 installation
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
kotlinOptions {
jvmTarget = "1.6"
jdkHome = JDK_7
}
}
tasks.withType(JavaCompile) {
options.fork = true
options.forkOptions.javaHome = file(JDK_7)
}
test {
executable = "$JDK_7/bin/java"
}
Kotlin can target either Java 6 or Java 8 and I don't think this has changed. However, it is quite likely that the default has changed from Java 6 to Java 8, so try as suggested here:
compileKotlin {
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
kotlinOptions {
jvmTarget = "1.6"
apiVersion = "1.2"
languageVersion = "1.2"
}
}
The version of kotlin-gradle-plugin is the version of the Kotlin compiler used to compile your code. The version of the stdlib is the version of your runtime library. It is highly recommended to use the same version here.
The apply plugin: kotlin adds some tasks under the hood - just continue using the java tasks like gradle assemble, gradle build and gradle run as they will invoke the kotlin specific tasks
kotlin-stdlib-jdk7 adds very little value - unless you use features of the java library that were introduced in java 7 and that have extensions from Kotlin's stdlib, you'll be fine to just use the default stdlib (which targets Java 6 and is a dependency of kotlin-stdlib-jdk7 anyways).