K2JVMCompiler

Hello
I am part of 2Sigma development team. My current project is “beakerx”.

In this project we can run code of some languages in browser.
Something like Your “kotlinc-jvm” interactive shell.
But in browser and with extra functionality.
It is based in Python Jupyter notebook. And we have different kernels for them : “groovy” , “Java” , “Scala” , “Cpp” …
Now I try to write “Kotlin” kernel. And I need to run kotlin code without installing languages (Having only “jar” files downloaded by gradle).
Basically I make them run, but I have some questions:

  1. I use K2JVMCompiler, is it best way ? (Us I understand “kotlinc-jvm” interactive shell uses it too.)
  2. I Have no “kotlinHome” , language is not installed locally. I have only jar files.
    And not setting “kotlinHome”, I always get warings:
warning: classpath entry points to a non-existent location: <no_path>\lib\kotlin-runtime.jar
warning: classpath entry points to a non-existent location: <no_path>\lib\kotlin-script-runtime.jar
warning: classpath entry points to a non-existent location: <no_path>\lib\kotlin-reflect.jar

But this jar already in classpath, this “non-existent location” adds by “kotlinHome”
So, how can I get rid of this warning ?

          arguments.suppressWarnings = true;
          arguments.coroutinesState = K2JVMCompilerArguments.ERROR;

Do not help.

All code in there:

          K2JVMCompilerArguments arguments = K2JVMCompilerArguments.createDefaultInstance();
          //arguments.kotlinHome = pathToCore.toString(); // Nothing to set !!!
          arguments.includeRuntime = true;
          arguments.destination = outDir;
          arguments.classpath = getEntriesAsString(classpathEntries, ";");
          arguments.verbose = false;
          arguments.suppressWarnings = true;
          arguments.coroutinesState = K2JVMCompilerArguments.ERROR;
          arguments.freeArgs = new ArrayList<>();
          arguments.freeArgs.add(sourceFile.toString());
          
          MessageCollector collector = new PrintingMessageCollector(errorPs, MessageRenderer.PLAIN_RELATIVE_PATHS, arguments.verbose);
          exitCode = comp.exec(collector, Services.EMPTY, arguments);
          
          if (ExitCode.COMPILATION_ERROR == exitCode) {
            j.outputObject.error(new String(errorBaos.toByteArray(), StandardCharsets.UTF_8));
            j.outputObject.executeCodeCallback();
          } else if (ExitCode.OK == exitCode) {
          ....
          }

Warnings Example:

More details about this problem can be found in this PR:

1 Like

I think the question is linked to this one: Standalone Kotlin REPL?.
We need a way to call for kotlin compiler from the code as it is done in GroovyShell and now JShell. I am also using beaker (with groovy for now), but I also have embedded shell in my code (groovy for now) and would like to use kotlin for that.

You should do the following:

  1. Pass -no-stdlib (K2JVMCompilerArguments.noStdlib) to prevent the compiler from adding the jars with the non-existing paths to the classpath.
  2. Add the path to kotlin-stdlib.jar on your system (and kotlin-reflect.jar + kotlin-script-runtime.jar, if needed) to the classpath.

The strange error message is indeed an issue, we’ll change it to output something like “the Kotlin standard library is not found” instead: https://youtrack.jetbrains.com/issue/KT-18859

I need remove this warning at all !
arguments.suppressWarnings = true;
do not help

Some more details can be found in here:

The warning should be gone if you’ll add

arguments.noStdLib = true;

to your code.

Yes , thanks, it helped :slight_smile: