error: unresolved reference when trying to import a package - kotlin

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

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.

How to create parameterized hooks in junit 5?

I am writing a test in using junit 5 where I want to run a same test on different sets of data. To achieve that, I am using parameterization. Now a situation came in where I want to run something before each set. I want to implement that in #BeforeEach hook and after some research, I found that ParameterResolver implementation can be one of the solution for that. But with that, test is getting issues to get parameter in #BeforeEach hook.
Here I am adding my code. These are kotlin classes. One test class StringDataClientTest which extends StringDataTest in which I am adding test parameter and #beforeEach hook. Adding #ExtendWith with this class which is extending StringParameterResolver class.
StringDataClientTest:
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
class StringDataClientTest : StringDataTest() {
#DisplayName("First Client Test")
#ParameterizedTest
#MethodSource("getString")
fun firstClientTest(data: String) {
System.out.println("First client test for: $data")
}
}
StringDataTest:
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.provider.MethodSource
#ExtendWith(StringParameterResolver::class)
open class StringDataTest {
companion object {
#JvmStatic
fun getString(): List<String> {
return listOf(
"Parameter 1",
"Parameter 2",
"Parameter 3",
"Parameter 4"
)
}
}
#BeforeEach
#MethodSource("getString")
fun beforeEveryTest(value: String) {
System.out.println(" --------- Running before hook for: $value")
}
}
StringParameterResolver:
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.ParameterContext
import org.junit.jupiter.api.extension.ParameterResolver
class StringParameterResolver : ParameterResolver {
override fun supportsParameter(parameterContext: ParameterContext?, extensionContext: ExtensionContext?): Boolean {
return parameterContext!!.parameter.type === StringDataTest::class.java
}
override fun resolveParameter(parameterContext: ParameterContext?, extensionContext: ExtensionContext?): Any {
return StringDataTest()
}
}
And I am getting this exception in console output:
org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [java.lang.String arg0] in method [public final void com.thescore.test.cases.team.testCode.StringDataTest.beforeEveryTest(java.lang.String)].
Expecting:
Running before hook for: Parameter 1
First client test for: Parameter 1
Running before hook for: Parameter 2
First client test for: Parameter 2
Running before hook for: Parameter 3
First client test for: Parameter 3
Running before hook for: Parameter 4
First client test for: Parameter 4
Haven't done parameterized hooks before. Please help with this. Thanks!

Kotlin class in other package not resolved

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.

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.

How should an inline function on a Kotlin class be imported

There is a kotlin file in a project dependency jar (called, for example, KotlinClass) with an inline function
package io.pack
inline fun <T> every(){
///does stuff
}
If I import it into a Java class as a static:
import static io.pack.KotlinClass.every;
The import is recognised.
If I import it into a Kotlin class:
import io.pack.every
or (this ought not to work anyway, but tried for completeness) as
import io.pack.KotlinClass.every
It is not recognised.
(Note: If I create my own Kotlin file with an inline function, then that can be imported into a Kotlin class with no problem. The problem is when importing from a specific project dependency.)
What might be stopping the import of this function into a kotlin class?
It works for me. I create a Kotlin module with a file called TopLevelStandalone.kt:
package io.pack
inline fun every(){
print("Inline")
}
fun normalFun() {
print("Normal")
}
The jar this project builds into contains the class file, and a META-INF/top-level-standalone.kotlin_module containing:
io.packTopLevelStandaloneKt
I then create another Kotline module, manually adding the jar to its dependencies. I'm now able to call every or normalFun:
import io.pack.every
import io.pack.normalFun
fun main(args: Array<String>) {
every()
normalFun()
}