Kotlin compiler does not find import kotlin.test - kotlin

I'm trying to compile the following via command-line:
import kotlin.test.assertTrue
fun main(args: Array<String>) {
assertTrue(false)
}
However, the compiler fails with:
$ kotlinc -d MyCode.jar MyCode.kt
MyCode.kt:1:15: error: unresolved reference: test
import kotlin.test.assertTrue
^
What am I missing?

Are org.jetbrains.kotlin:kotlin-test and org.jetbrains.kotlin:kotlin-test-junit specified in dependencies? kotlin.test is not a base module

Related

How to import com.eclipsesource.v8.NodeJS in kotlin?

in build.gradle i add dependencies:
implementation 'com.eclipsesource.j2v8:j2v8_node:8.1.0'
implementation 'com.eclipsesource.j2v8:j2v8:6.2.1'
Than i try use in MainActivity.kt code:
val nodeJS = NodeJS.createNodeJS()
For this i add import com.eclipsesource.v8.NodeJS
But i see an error: unresolved reference: eclipsesource.
I checked the project structure and the dependencies exist:
Why am i getting an error?

How do you run a kotlin file that accesses another file in vs code

on vs code, i have a folder called master on my desktop with 2 kotlin files, main.kt and foo.kt.
main.kt's code
import master
fun main() {
println("hello")
foo()
}
foo.kt's code:
import master
fun foo(){
println("code from foo")
}
however, when i run kotlinc hello.kt -d hello.jar on the terminal of vs code, it says error: unresolved reference: foo
how can i resolve this? am i naming the packages wrong? I just want to run a very light kotlin package.

Kotlin native platform posix compilation without gradle

I am trying to compile a basic Kotlin/Native program with just the kotlinc-native CLI compiler rather than a full Gradle build. However, I cannot get the platform.posix library to work, despite compiling on linux for linux.
The source code I am using is:
import platform.posix.exit
fun main() {
println("Should exit with status 10")
exit(10)
}
When compiled with the following build.gradle.kts it works fine:
plugins {
kotlin("multiplatform") version "1.6.10"
}
repositories {
mavenCentral()
}
kotlin {
linuxX64("native") {
binaries {
executable()
}
}
}
However, when compiled from the command-line with:
kotlinc-native test.kt
I get:
test.kt:1:8: error: unresolved reference: platform
import platform.posix.exit
^
test.kt:4:5: error: unresolved reference: exit
exit(10)
^
Anyone know how to get Kotlin/Native compilation working fully without using Gradle? Or is there some magic that Gradle does that makes it work?
Thanks.

Kotlin in VS Code - Unresolved reference for user defined class in another file

I'm trying to set up Kotlin for VS Code following the instructions in this article, which basically says to install the Kotlin and Code Runner extensions.
So far, so good, I am able to run the following code:
App.kt:
fun main() {
MyApp().printTest()
}
class MyApp {
fun printTest() {
println("Hello test")
}
}
However, when I try to extract the MyApp class to another file in the same folder, I can't import it:
App.kt:
import MyApp.MyApp // import MyApp doesn't work also
fun main() {
MyApp().printTest()
}
MyApp.kt:
class MyApp {
fun printTest() {
println("Hello test")
}
}
I've tried to import the Java Import Snippets extension, but it doesn't work, showing a unresolved reference for MyApp.
Am I missing some extension or VS Code configuration?
That's the command VS Code is running:
cd ".../testapp/src/" && kotlinc App.kt -include-runtime -d App.jar && java -jar App.jar
Should it reference MyApp.kt somehow?
Note: I'm on Debian.
You have no project's package name defined.
Put the files App.kt and MyApp.kt in the same folder level.
In App.kt, only use
import MyApp // or no import at all,
fun main() {
MyApp().printTest()
}
Read more about Java/Kotlin package

Compiling my Kotlin tests inside of git bash

I am trying to run my code in git bash so that I don't have to run it in Intellij. It runs smoothly inside of Intellij but is giving me issues when I try and compile it inside of gitbash.
Here is the body of code that I am able to run inside of Intellij. The code runs and the test passes when I run it in Intellij.
import org.junit.jupiter.api.*
import testCases.TestCases
import testCases.BaseUser
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
#TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class AllTests : BaseUser() {
#BeforeAll
#Throws(Exception::class)
fun initAll() {
this.initialize()
}
#Test
#Order(1)
fun runAllTests(){
try{
TestCases()
}catch(ex: Exception){
Assertions.fail<Any>()
}
}
}
I am trying to run and compile it by doing this inside of git bash:
kotlinc AllTests.kt -include-runtime -d tests.jar
I keep seeing multiple errors and am unsure how to resolve them:
AllTests.kt:1:12: error: unresolved reference: junit
import org.junit.jupiter.api.*
^
AllTests.kt:2:8: error: unresolved reference: testCases
import testCases.*
^
AllTests.kt:5:2: error: unresolved reference: TestInstance
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
^
AllTests.kt:5:15: error: unresolved reference: TestInstance
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
^
AllTests.kt:6:2: error: unresolved reference: TestMethodOrder
#TestMethodOrder(MethodOrderer.OrderAnnotation::class)
^
AllTests.kt:6:18: error: unresolved reference: MethodOrderer
#TestMethodOrder(MethodOrderer.OrderAnnotation::class)
^
AllTests.kt:7:18: error: unresolved reference: BaseUser
class AllTests : BaseUser() {
^
AllTests.kt:9:6: error: unresolved reference: BeforeAll
#BeforeAll
^
AllTests.kt:12:14: error: unresolved reference: initialize
this.initialize()
^
AllTests.kt:15:6: error: unresolved reference: Test
#Test
^
AllTests.kt:16:6: error: unresolved reference: Order
#Order(1)
^
AllTests.kt:20:13: error: unresolved reference: TestCases()
TestCases()
^
AllTests.kt:33:13: error: unresolved reference: Assertions
Assertions.fail<Any>()
^
It runs in IntelliJ because IntelliJ knows where the jar file for junit is located, so it includes that on the path for running the test. You need to do the same.
You can do it with the -classpath option
kotlinc AllTests.kt -include-runtime -classpath /path/to/junit.jar:/path/to/other.jar -d tests.jar
If you are on windows, separate your jars with ; instead of :.