问题
Putting a println
statement before a kotlin function returns crashes. stacktrace:
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ gradle clean build --stacktrace
w: Classpath entry points to a non-existent location:
e: /home/thufir/NetBeansProjects/kotlin/src/main/kotlin/example.kt: (14, 5): Unresolved reference: println
> Task :compileKotlin
Using Kotlin incremental compilation
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileKotlin'.
> Compilation error. See log for more details
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileKotlin'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ResolveBuildCacheKeyExecuter.execute(ResolveBuildCacheKeyExecuter.java:61)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:625)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:580)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: org.gradle.api.GradleException: Compilation error. See log for more details
at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:8)
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.processCompilerExitCode(Tasks.kt:359)
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.callCompiler$kotlin_gradle_plugin(Tasks.kt:333)
at org.jetbrains.kotlin.gradle.tasks.KotlinCompile.callCompiler$kotlin_gradle_plugin(Tasks.kt:231)
at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.execute(Tasks.kt:203)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTaskAction.doExecute(DefaultTaskClassInfoStore.java:179)
at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:135)
at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:122)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
... 29 more
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
2 actionable tasks: 2 executed
thufir@dur:~/NetBeansProjects/kotlin$
Taking out the offending line, a successful output:
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ gradle clean runShadow
w: Classpath entry points to a non-existent location:
> Task :compileKotlin
Using Kotlin incremental compilation
> Task :shadowJar
The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.
> Task :runShadow
Nov 04, 2017 8:42:47 PM net.bounceme.dur.kotlin.App run
INFO: running
Nov 04, 2017 8:42:47 PM net.bounceme.dur.kotlin.App run
INFO: integer is 2
Nov 04, 2017 8:42:47 PM net.bounceme.dur.kotlin.App run
INFO: integer is 4
BUILD SUCCESSFUL in 1s
7 actionable tasks: 7 executed
thufir@dur:~/NetBeansProjects/kotlin$
java:
package net.bounceme.dur.kotlin;
import java.util.logging.Logger;
public class App {
private static final Logger LOG = Logger.getLogger(App.class.getName());
private void run() {
LOG.info("running");
int integer = 2;
LOG.info("integer is\t\t" + integer);
new demo.Foo();
demo.ExampleKt.bar();
integer = demo.ExampleKt.doubleUp(integer);
LOG.info("integer is\t\t" + integer);
}
public static void main(String[] args) {
new App().run();
}
}
kotlin:
package demo;
class Foo
val PI = 3.14
var x = 0
fun bar() {
x += 1
}
fun doubleUp(value: Int): Int {
return value * 2
}
build file:
plugins {
id 'com.gradle.build-scan' version '1.8'
// id 'java'
id 'application'
id "org.jetbrains.kotlin.jvm" version "1.1.51"
id 'com.github.johnrengelman.shadow' version '2.0.1'
}
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
//publishAlways()
}
configurations {
provided
}
shadowJar {
baseName = 'kotlinAdder'
classifier = null
version = null
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = 'net.bounceme.dur.kotlin.App'
repositories {
jcenter()
}
configurations {
provided
}
dependencies {
}
environment:
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ kotlin -version
Kotlin version 1.1.51 (JRE 1.8.0_151-b12)
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ kotlinc -version
info: kotlinc-jvm 1.1.51 (JRE 1.8.0_151-b12)
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ javac -version
javac 1.8.0_151
thufir@dur:~/NetBeansProjects/kotlin$
project:
.
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── net
│ │ └── bounceme
│ │ └── dur
│ │ └── kotlin
│ │ └── App.java
│ └── kotlin
│ └── example.kt
└── test
└── java
12 directories, 8 files
It's about as simple as possible, Java invoking Kotlin. Not sure how else I can simplify it. This is a version problem??
While I'm using netbeans gradle is purely being run from the CLI. I can even delete out the NB files should that prove useful..
Probably I'm just not printing to the console correctly.
回答1:
doh. kotlin required.
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ gradle clean shadowJar
> Task :compileKotlin
Using Kotlin incremental compilation
> Task :shadowJar
The SimpleWorkResult type has been deprecated and is
scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
thufir@dur:~/NetBeansProjects/kotlin$
thufir@dur:~/NetBeansProjects/kotlin$ gradle runShadow
> Task :runShadow
Nov 04, 2017 10:43:44 PM net.bounceme.dur.kotlin.App run
INFO: running
Nov 04, 2017 10:43:44 PM net.bounceme.dur.kotlin.App run
INFO: integer is 2
hellooo from kotlin
Nov 04, 2017 10:43:44 PM net.bounceme.dur.kotlin.App run
INFO: integer is 4
BUILD SUCCESSFUL in 1s
6 actionable tasks: 3 executed, 3 up-to-date
thufir@dur:~/NetBeansProjects/kotlin$
...
plugins {
id 'com.gradle.build-scan' version '1.8'
// id 'java'
id 'application'
id "org.jetbrains.kotlin.jvm" version "1.1.51"
id 'com.github.johnrengelman.shadow' version '2.0.1'
}
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
//publishAlways()
}
configurations {
provided
}
shadowJar {
baseName = 'kotlinAdder'
classifier = null
version = null
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = 'net.bounceme.dur.kotlin.App'
repositories {
jcenter()
}
configurations {
provided
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"
}
have to add the dependency.
来源:https://stackoverflow.com/questions/47118023/kotlin-unresolved-reference-println-from-gradle-on-the-cli