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

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.

Related

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

kotlin program error: no main manifest attribute in jar file

I wrote a simple kotlin helloworld program
hello.kt
fun main(args: Array<String>) {
println("Hello, World!")
}
Then I compiled it with kotlinc
$kotlinc hello.kt -include-runtime -d hello.jar
there was no errors and hello.jar was generated.
when I ran it
$java -jar hello.jar
it said there is no main manifest attribute in hello.jar
$no main manifest attribute, in hello.jar
I couldn't figure out this problem.
My kotlin version is 1.3.40, JDK version is 1.8.0
I came accross this answer while having the same issue with Kotlin and gradle. I wanted to package to get the jar to work but kept on pilling errors.
With a file like com.example.helloworld.kt containing your code:
fun main(args: Array<String>) {
println("Hello, World!")
}
So here is what the file build.gradle.kts would look like to get you started with gradle.
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
application
kotlin("jvm") version "1.3.50"
}
// Notice the "Kt" in the end, meaning the main is not in the class
application.mainClassName = "com.example.MainKt"
dependencies {
compile(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<Jar> {
// Otherwise you'll get a "No main manifest attribute" error
manifest {
attributes["Main-Class"] = "com.example.MainKt"
}
// To avoid the duplicate handling strategy error
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
// To add all of the dependencies otherwise a "NoClassDefFoundError" error
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
So once you gradle clean build you can either do:
gradle run
> Hello, World!
Assuming your projector using the jar in build/libs/hello.jar assuming that in your settings.gradle.kts you have set rootProject.name = "hello"
Then you can run:
java -jar hello.jar
> Hello, World!
Try to upgrade to version 1.3.41 and using JDK 1.11+.

Kotlin compiler does not find import kotlin.test

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

Unable to resolve class org.gradle.util.TestUtil in Gradle

This error means that class TestUtil is not on the classpath and the compiler can not find it. I came across such error hundred times before and there was missing Jar or wrong written class names, but now I just don't know what is wrong.
In my buildSrc dir I have custom Task and I made test for it:
package com.example.core.tasks;
import spock.lang.Specification
import org.gradle.api.Project
import org.gradle.util.TestUtil
public class GetInfoTaskTest extends Specification {
def "check files"(){
given:
def project = TestUtil.createRootProject()
when:
GetInfoTask getInfo = project.tasks.create("getInfo", GetInfoTask)
then:
getInfo instanceof GetInfoTask.class
}
}
In the build script in the buildSrc dir:
dependencies {
compile localGroovy()
testCompile 'org.spockframework:spock-core:0.7-groovy-1.8'
testCompile gradleApi()
}
Error:
GetInfoTaskTest.groovy: 4: unable to resolve class org.gradle.util.TestUtil
# line 3, column 1.
import org.gradle.util.TestUtil
^
I checked that this TestUtil is not internal. I still don't know why gradle can not find it.
TestUtil is an internal class. You can't use it in your own code.