I have a Generator.kts file. When I execute it using:
kotlinc -script Generator.kts
everything works as expected.
However, now my script has grown and I need to separate that class into multiple files.
I did that but when I try to execute it again I get the following errors:
Generator.kts:8:23: error: unresolved reference: CSVReader
val csvData = CSVReader().readCSV()
^
Generator.kts:10:23: error: unresolved reference: Folders
val folders = Folders()
^
Generator.kts:14:9: error: unresolved reference: KeyStore
KeyStore().generateKeyStoreFile(
Basically it fails to find all of the classes I created (CSVReader.kt, Folders.kt and KeyStore.kt). All those classes are in the same folder (including Generator.kts).
How can I run a Kotlin script that uses multiple files?
You could either compile all your sub-scripts into an artifact and add it to the classpath
Or you could use a third party tool like kscript to include them on the fly into your master script.
#!/usr/bin/env kscript
#file:Include("utils.kt")
val robustMean = listOf(1.3, 42.3, 7.).median()
println(robustMean)
For details and examples see https://github.com/holgerbrandl/kscript#ease-prototyping-with-include
Disclaimer: I'm a contributor of kscript.
Related
In Multiplatform project, I want to use main function inside jvmMain to test the library I’m working on, but when I’m trying to run this it returns an error Error: Could not find or load main class MainKt for following function in Main.kt
fun main() {
println("Hello World!")
}
I've got Main.kt file in a root directory of my module, but putting it under some package is also throwing this error.
I'm using Java 8 JDK:
Are there any additional steps I should make to be able to use main function?
EDIT:
I did additional checks and I found out that when in the project I have android target then in the jvm main function I’m getting this error above. When I’m creating a new project with exactly the same configuration, but without android target it’s working properly, main function compiles and it is printing a message.
Any guess what is causing this problem?
I want to be able to use bazel to organize a simple kotlin project.
I am using the templates as listed in rules_kotlin (https://github.com/bazelbuild/rules_kotlin)
This is my BUILD file
load("#io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
kt_jvm_library(
name = "redis-tools",
srcs = glob(["*.kt"]),
deps = [],
)
This is my WORKSPACE file
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
rules_kotlin_version = "legacy-1.3.0"
rules_kotlin_sha = "4fd769fb0db5d3c6240df8a9500515775101964eebdf85a3f9f0511130885fde"
http_archive(
name = "io_bazel_rules_kotlin",
urls = ["https://github.com/bazelbuild/rules_kotlin/archive/%s.zip" % rules_kotlin_version],
type = "zip",
strip_prefix = "rules_kotlin-%s" % rules_kotlin_version,
sha256 = rules_kotlin_sha,
)
load("#io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
kotlin_repositories() # if you want the default. Otherwise see custom kotlinc distribution below
kt_register_toolchains() # to use the default toolchain, otherwise see toolchains below
My kotlin file main.kt:
import java.util.Scanner
fun main(args: Array<String>) {
// Creates a reader instance which takes
// input from standard input - keyboard
val reader = Scanner(System.`in`)
print("Enter a number: ")
// nextInt() reads the next integer from the keyboard
var integer:Int = reader.nextInt()
// println() prints the following line to the output screen
println("You entered: $integer")
}
I use a very simple file to test my setup.
Without bazel, I can do these to build: kotlinc main.kt -include-runtime -d test.jar
then run it with java -jar test.jar
When I use bazel build ., bazel creates a few folders. I tried to run java -jar bazel-out/darwin-fastbuild/bin/redis-tools.jar but it failed.
$ java -jar bazel-out/darwin-fastbuild/bin/redis-tools.jar
no main manifest attribute, in bazel-out/darwin-fastbuild/bin/redis-tools.jar
Did I miss a target? How can I run the main.tk from the bazel output?
The jar you are trying to run is missing a manifest file which declares its main class.
For executing a binary, Bazel uses a shell script wrapper which includes the required jvm flags and its run-time dependencies.
Notice that you are using kt_jvm_library. This rule builds a shared dependency without the wrapper. To include a wrapper you should use the kt_jvm_binary rule. Then you can specify the main class by setting the main_class attribute.
Notice that you can use the bazel run :redis-tools to run the jar (use -s to see which script Bazel excuted)
You can also use bazel build :redis-tools_deploy.jar to build a "fat-jar" which will include the manifest.
I'm trying to run the most basic PixiJS for Haxe sample program, but IntelliJ gives a Type Not Found error.
The failing code:
package ;
import pixi.core.Application;
The error message:
Main.hx:3: characters 7-28 : Type not found : pixi.core.Application
I think the issue is PixiJS-specific, as when I follow the same install-and-import steps for other Haxe modules (eg flambe, flixel) they behave as expected. I've tried with both 4.7.1 and 4.5.5, and as both global and project-specific libraries (separately).
(I hybridized several screenshots to save space)
When running from the console, using:
> haxe -main Main -lib pixijs -js output.js
Everything seems to compile (running the compiled code results in a PIXI is not defined error which is a seperate issue).
UPDATE 1, how I'm adding the dependency:
After executing haxelib install pixijs, I follow these steps:
UDPATE 2, the config being run:
If you can't get it working in the dependencies section you could try adding the haxelibs pixijs externs src/ folder as an additional sources root in Project Structure -> Modules -> (module name) -> Sources. That's how it worked for me:
I am learning Kotlin by doing Kotlin Koans int IntelliJ EduKotlin plugin (https://plugins.jetbrains.com/plugin/8186). While doing tasks and running them im this plugin is working, I am not able to create new Kotlin script and run it without this plugin.
Sample code in a file named Hello.kt:
fun hello() : String {
return "ok"
}
Then I create new run configuration derived from 'Kotlin script', set 'Working directory' to ProjectName/src folder (or ProjectName folder, same result) and IDE shows me a 'Could not find script file: Hello.kt' warning as in the screen below:
And indeed, while trying to run script I got following stacktrace:
"C:\Program Files\Java\jdk1.8.0_74\bin\java" -Dfile.encoding=windows-1250 -classpath C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-runtime.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -script Hello.kt
exception: java.lang.RuntimeException: Failed to evaluate script: kotlin.KotlinNullPointerException
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:263)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileAndExecuteScript(KotlinToJVMBytecodeCompiler.kt:212)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:181)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:49)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:181)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:138)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:57)
at org.jetbrains.kotlin.cli.common.CLICompiler.doMainNoExit(CLICompiler.java:248)
at org.jetbrains.kotlin.cli.common.CLICompiler.doMain(CLICompiler.java:238)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(K2JVMCompiler.kt:248)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(K2JVMCompiler.kt)
Caused by: kotlin.KotlinNullPointerException
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:475)
... 10 more
Process finished with exit code 2
What more, the hello method is marked as never used. However, when I change file extension from .kt to .ktscript, then this method is marked as used, and the output from running code is following:
"C:\Program Files\Java\jdk1.8.0_74\bin\java" -Dfile.encoding=windows-1250 -classpath C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Users\myUser\.IdeaIC2016\config\plugins\Kotlin\kotlinc\lib\kotlin-runtime.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -script Hello.kt
error: source file or directory not found: Hello.kt
Process finished with exit code 1
So my question is: what am I missing when trying to create and run Kotlin code in a standalone Kotlin script in IntelliJ?
A standalone Kotlin script file needs to have a .kts extension. If you rename your file, you also need to make sure that your run configuration is updated to refer to the new name of the file.
Also, if you create any file (a regular Kotlin file or a .kts file) which only contains a function, running the script will not produce any output, because the function will not be called. To see some output, you need to call the function in your script:
fun hello(): String {
return "ok"
}
println(hello())
The stacktrace that you saw is a bug in Kotlin.
I followed the Kotlin tutorial for eclipse here : Getting Started With Eclipse Luna
However, I'm running into this error:
Error: Could not find or load main class _DefaultPackage
Anyone who knows to get around this?
This was a severe bug (KT-10221) in automatic generation of Launch Configuration in plugin version 0.4.0. It was fixed in 0.5.0 so the recommendend way to workaround is to update plugin.
The source of the problem was that the plugin used an old pattern for generating name of the class for main function that had been abandoned by Kotlin compiler.
It's possible to workaround it by editing launch configuration (Eclipse Menu -> Run -> Run Configurations...) by hand and changing Main class field in Java Application group. If the file is named hello.kt with no package directive, as it is described in tutorial, than corrected string should be HelloKt.
If file has name other.kt with package my.tutorial than the Main Class should contain my.tutorial.HelloKt. You can read more about it in the section Package-Level Functions of Calling Kotlin From Java page.
I have been getting the same issue. And after putting the right compiler output path, it got resolved.
Go to Project -> Project Compiler output :
In the text box, fill this:
[Absolute Path]/{Project Name}/out
In my case I was having this problem while trying to run the program using the Application Gradle plugin. The problem was in the mainClassName property using single quotes instead of double ones
This didn't work:
mainClassName = 'demo.HelloWorldKt'
With double quotes, it works:
mainClassName = "demo.HelloWorldKt"
For me it worked after I installed the correct JDK. I first had JDK 11 but the tutorial I did was with JDK 8 so after I installed this and set it in the "installed JREs" options it found the main class without having any "mainClassName" or any other option in the build.gradle file.
For me, it worked in a fresh eclipse workspace. Possibly, the Kotlin eclipse plugin is not playing well with other plugins (in my case, PyDev).
I'm creating a Kotlin Application with JavaFX and I had this issue until I went to:
Run > Run Configurations > Java Application > Common
I unticked "Allocate console" and it fixed the issue.