Kotlin class in other package not resolved - kotlin

I started with Kotlin and want to exclude files in other packages in Visual Studio Code. Unfortunately it does not work
Person.kt
package com.example
public class Person(
val _firstName: String,
val _lastName: String,
val _age: Int
)
main.kt
import com.example.Person
fun main(args: Array<String>) {
val person = Person("Peter","Pan",12)
println(person)
}
When I hover over in the main function Visual Studio Code show me the Person class, thus it seems that it is recogniced but as far as I run the code I get the following error
[Running] cd "c:\Users\Matthias\Desktop\Kotlin\" && kotlinc Main.kt -include-runtime -d Main.jar && java -jar Main.jar
Main.kt:1:12: error: unresolved reference: example
import com.example.Person
^
Main.kt:4:18: error: unresolved reference: Person
val person = Person("Peter","Pan",12)
^
I played around with other package names like "domain" but then the whole class is not found. I put them in folder but the error stays the same.

Since you are compiling only Main.kt, it doesn't include Person in the Main.jar being built for running. As you are referring to Person in the code, it should be part of the jar.

Related

Kotlin compile error "unresolved reference" only when compiling from command line

I have the Kotlin code:
import com.google.gson.Gson
import java.io.File
class System {
/*
Save current game state to file
*/
internal fun saveGameState(game: Game) {
val gameState = game.getGameState()
val gson = Gson()
val jsonString = gson.toJson(gameState)
val pathname = FILENAME_SAVED_GAME
File(pathname).writeText(jsonString)
}
private fun loadGameState(): Game {
val jsonString = File(FILENAME_SAVED_GAME).readText(Charsets.UTF_8)
val gson = Gson()
val gameState: GameState = gson.fromJson(jsonString, GameState::class.java)
File(FILENAME_SAVED_GAME).delete()
return Game(gameState)
}
...
}
When I run it from within IntelliJIDEA, it compiles and runs fine, including using this class to save state and restore state.
When I compile it from the command line, I use this command:
kotlinc *.kt -include-runtime -d GizmosK.jar
and I this error message:
System.kt:1:12: error: unresolved reference: google
import com.google.gson.Gson
^
System.kt:11:20: error: unresolved reference: Gson
val gson = Gson()
^
System.kt:19:20: error: unresolved reference: Gson
val gson = Gson()
^
I've successfully used this command many times before, but possibly not since adding this System class, which is the only one that imports anything from outside my project.
I've searched, and found this question which appears very similar to my problem, but I have a few concerns.
First, I don't know if this solution applies to my situation since my code is pure Kotlin and not Android.
Second, I don't know how to generalize their solution and make it apply to my situation. Like, I know I probably have to replace something with google.gson or com.google.gson or com.google.gson.Gson in there somewhere, but I don't know where, since there are three things there that look like package names. Do I need all three? I also don't know if expressions like package_name are literal strings I should enter verbatim or if I should replace those words with the actual package name.
Third, I've never had to specify flovar/flavor nor resource_package before in a command line, and I don't want to introduce new variables, if at all possible.
BTW, I'm compiling from the command line to generate a .jar file to distribute so anyone can run it from the command line without sharing my code or requiring they install IntelliJ IDEA.

Kotlin class won't let me extend abstract class from different file

I am working on an object-oriented project, and I want to improve my understanding of the OOP part of Kotlin. I have the following abstract class:
package Objecttest
abstract class Abstractclasstest {
abstract fun testString(s: String): String
}
Now I want to extend it in a new class in a different file like this:
package Objecttest
public class Newclasstest : Abstractclasstest() {
override fun testString(s: String): String {
return s
}
}
but when I try compiling Newclasstest.kt, I am met with the following error message: "error: unresolved reference: Abstractclasstest".
Folder structure:
Objecttest/
├── Abstractclasstest.kt
├── Newclasstest.kt
Why is this and how can I work around it? The most important bit is why, because I want to avoid the same mistake in the future.
It seems to me that you need to compile Abstractclasstest.kt first using kotlinc Abstractclasstest.kt and only then compile Newclasstest.kt as follows: kotlinc -cp . Newclasstest.kt. This will search for class files in the same path as Newclasstest.kt on which it should already find the one corresponding to Abstractclasstest.kt. Or you can just compile the 2 files at the same time using kotlinc *.kt.

Error:(1, 41) Kotlin: Symbol is declared in module 'jdk.internal.opt' which does not export package 'jdk.i

I have a beginner problem.
Just installed IntellijIDEA and JDK (Java Development Kit) and can't build my project.
Code:
import jdk.internal.joptsimple.internal.Strings
fun main(args: Array<Strings>){println("Hello")}
Error:
Error:(1, 41) Kotlin: Symbol is declared in module 'jdk.internal.opt' which does not export package 'jdk.internal.joptsimple.internal'
The proper main function with arguments looks like this:
fun main(args: Array<String>) {
}
Note that args is an array of Kotlin's String not jdk.internal.joptsimple.internal.Strings. So just fix your method's signature and remove the import statement.

error: unresolved reference when trying to import a package

I'm trying to create package in IntelliJ IDEA and it works, but when i try to run file with terminal it doesn't work.
I got some error message : "error: unresolved reference: util".
I put the code in the same directory, here's ny code :
// PersonA.kt
package util
class PersonA {
fun greet() {
println("...")
}
}
// main.kt
import util.PersonA
fun main(args: Array<String>){
val person = PersonA()
val greet = person.greet()
println(greet)
}
add package name in PersonA.kt
package com.example.appname.util //change the com.example.appname with ur package name
and at main.kt
import com.example.appname.util.PersonA

Run single Kotlin file

I have two Kotlin files in the same folder:
Both files have a trivial main method.
I can right click on helloworld.kt file and select "Run..." from the menu.
There's no such option for the circle.kt file though.
This is my run/debug configuration for helloworld.kt:
I tried to create an equivalent configuration for circle.kt, but it complains that the class com.example.kotlin.learning.CircleKt has no main method:
There's actually no class in that file. But there isn't one in HelloWorld.kt as well, and that works.
Here's the code for helloworld.kt:
package com.example.kotlin.learing
fun main (argomenti: Array<String>) {
println ("SUCA!")
println (saluta ( "mario"))
val vettore : Array<String> = arrayOf("pippo", "pluto", "paperino")
println(vettore [0])
}
fun saluta (chi : String) = ( chi + " antani" )
here's circle.kt:
package com.example.kotlin.learing
fun main() = println("pippuz!")
I realize I am missing something deep here. What is that?
Thanks
If I'm not mistaken, main method without parameters is supported from Kotlin version 1.3-RC. Which version of Kotlin are you using?
If you are using an older version of Kotlin, you should pass an array of Strings as the argument of the main method.