How to pass javac options such as --add-exports to the Kotlin compiler? - kotlin

I'm unable to set javac arguments when compiling a Kotlin file. The kotlinc -Xjavac-arguments option looks like the expected way to achieve this, but using Kotlin 1.8.0 and Java 17 this doesn't work.
For example, considering a simple .kt file with a single import from a non exported package:
import com.sun.tools.javac.util.Context
Invoking kotlinc with:
kotlinc -Xadd-modules=jdk.compiler -Xjavac-arguments=--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED test.kt
gives:
test.kt:1:33: error: symbol is declared in module 'jdk.compiler' which does not export package 'com.sun.tools.javac.util'
import com.sun.tools.javac.util.Context
^
What am I missing?

Related

How to compile a Kotlin file to JavaScript?

Hi I am getting started with KotlinJS on Node and I've put a very simple kotlin file and I want to compile it using the raw kotlinc-js compiler. Without using gradle
package main
fun heavy() {
(1..10_000_000).forEach { it*it }
}
fun main() {
heavy()
println("Bye JS")
}
Here's the make command I've tried yet without success:
build-js:
kotlinc-js main.kt -output main.kt.js
It compiles fine, but when I attempt to run node main.kt.js:
throw new Error("Error loading module 'main.kt'. Its dependency
'kotlin' was not found. Please, check whether 'kotlin' is loaded prior
to 'main.kt'."); ^
Error: Error loading module 'main.kt'. Its dependency 'kotlin' was not
found. Please, check whether 'kotlin' is loaded prior to 'main.kt'.
at Object. (/home/nanospicer/KotlinProjects/KotlinScripting/main.kt.js:2:9)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47
Ok, so I figured it's missing the kotlin runtime. I'll try same argument as the JVM compiler: -include-runtime which leads to the error:
error: invalid argument: -include-runtime
info: use -help for more information
Then I tried:
build-js:
kotlinc-js main.kt -kotlin-home "/mnt/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2021.2.1/plugins/Kotlin/kotlinc/" -libraries "/mnt/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2021.2.1/plugins/Kotlin/kotlinc/lib/kotlin-stdlib-js.jar" -module-kind commonjs -main call -output main.kt.js
But it still leads to the same error. I also tried removing the libraries flag and it didnt work either
I've found the culprits.
I basically created a nodejs project from IntelliJ IDEA and took a quick read through the gradle tasks which hinted me that it was preparing a node environment for me under the hood.
I took a quick glance at npm.js and found that in fact there's a package called kotlin which is the javascript's stdlib for the compiler.
I finally tweaked the build command on my Makefile to use a module-kind of common-js and it worked out!
//Definitive main.kt I ended up using
fun heavy() {
(1..100_000_000).forEach { it*it }
}
fun main() {
heavy()
println("Bye JS")
}
Install kotlin's stdlib using npm:
$ npm i kotlin --save
And my Makefile build command looks like:
$ kotlinc-js main.kt -module-kind commonjs -main call -output main.kt.js
A small breakdown of the arguments:
-module-kind commonjs it's telling the compiler to generate the code-style for a node project (not quite, but you get the gist)
-main call determines whether the compiled module should invoke the main function. This is because if we set the value to noCall we're not gonna run the main function and it may behave just like a library.
-output main.kt.js the name I chose myself without any reason, as long as it's a .js file you'll be able to run it but I wanted to keep it like this to compare it to another main.js file I was generating!

How to run main function Kotlin file on command-line with string array arguments

i'm trying to run one of .kt files that i have for my side project and I suddenly wanted run my *.kt file that has
fun main(args: Array<String>) {
...
}
on command-line tool. I did install kotlin compiler, set up the path. All I have to do is to figure out how to pass args parameter through command line and cannot find a way to do that.
I've looked through kotlin compiler options and how to run kotlin file on command-line and it did not help at all.
Thanks in advance.
Just type them after the command:
java -jar myjar.jar arg_one arg_two
//OR (After compiling to jars you can specify the main class to run)
kotlin -classpath myjar.jar MyKt 'arg_one' 'arg_two'
//OR
kotlin MyKt 'arg_one' 'arg_two'
Then:
val argOne = args[0] // "arg_one"
First you have to compile your file to a JAR:
kotlinc filename.kt -include-runtime -d output.jar
Then you can run that JAR with java:
java -jar output.jar argument0 argument1

PixiJS for Haxe won't import - Type Not Found error in IntelliJ

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:

Kotlin Script (.kts) - How to divide it into multiple files?

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.

Robot not able to import .java library

I am trying to import the 'SwingLibrary' into my Robot project and have had a lot of issues with it. I was able to work around my first error that seems very common by removing the version number from the file name:
Importing test library 'SwingLibrary' failed: ImportError: No module named SwingLibrary
Now everything is named SwingLibrary (jar & xml included) so I got a little further but I am now seeing the error:
Importing test library 'SwingLibrary' failed: Expected class or module, got javapackage.
I know Robot is supposed to know when it's trying to import a python or jython package but for some reason it doesn't seem to be working here. I did not have an issue importing the java version of Selenium or any of the standard libraries.