3

Source code (standalone)

import com.google.gson.Gson
import com.google.gson.GsonBuilder

class LocationReport(var dateTimeStamp : String,
                     var latitude : Double, var longitude : Double)
{
}

fun main(args: Array<String>) {
    val gson = Gson()
    val gsonPretty = GsonBuilder().setPrettyPrinting().create()

    val report = LocationReport("24-May-2020 8:15 AM PDT", 37.422, -122.084)

    val jsonReport: String = gson.toJson(report)
    println(jsonReport)

    val jsonReportPretty: String = gsonPretty.toJson(report)
    println(jsonReportPretty)
}

When I attempt to compile with kotlinc, it fails with "error: unresolved reference: google". Without the references to Gson and related constructs, the program compiles and the .jar file can be run without incident. But of course I can't get the JSON functionality.

When I tried to create a gradle project, I eventually got the build to succeed, but when I attempt to run the resulting .jar file, I get "no main manifest attribute" in that .jar file.

I did search for "no main manifest attribute, without success. In particular, this answer does NOT work for me.

I regret if there is an obvious answer, or if I failed to follow proper protocol. I have been working several hours on this and have tried several different approaches without success. I'd really like to run that program OUTSIDE Android Studio.

Thank you for your consideration.

Update: thanks to user who pointed out I needed to download a Gson .jar file.

Problem now is the kotlin compiler kotlinc does not appear to recognize the value of the environmental variable CLASSPATH. By using the -cp option, I can get kotlinc to locate that Gson jar, but with the resulting .jar file java cannot locate that Gson jar, either with -cp on the command line or CLASSPATH configured to locate the Gson jar.

Locating the Gson jar works with java. I found a simple Java program that references Gson. Both javac (the compiler) and java locate the same Gson jar through the CLASSPATH variable.

So why can't kotlinc create a jar such that the java runtime can locate the Gson jar? I'd really like to get this working outside of the Android Studio. From the command line it is much easier to complete initial verification and debugging. Notice my original program prints two different JSON strings. It's possible with Android Studio, but from my beginner perspective more difficult.

Thanks again for reading. Please forgive any violations of stackoverflow protocol.

4

0 回答 0