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.

Community
  • 1
  • 1
  • You will have to download the modules for the gson stuff or clone https://github.com/google/gson and build yourself, after which you can add it to the classpath when compiling – user May 24 '20 at 19:16
  • 1
    Thanks for the tip, but I am still not making progress. I tried working with that GitHub repository, but could not get gradlew to work. So I downloaded the corresponding jar file, gson-2.6.2.jar. And I can get my program to COMPILE with -cp to point to that jar file. But I cannot get the jar file to run, despite pointing java to that same jar file, again with the -cp command line. I get ClassNotFoundException. Regret answers on stackoverflow do not work for me for that latter problem. Regret having such basic issues, but I come from C/C++. And I'd like this to work outside Android Studio! – Robert Lasater May 24 '20 at 22:04
  • Again, I appreciate your tip and regret I have so little background with Java/kotlin/gradle. I am sure someone who has worked in that environment would know what to do. – Robert Lasater May 24 '20 at 22:07
  • "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." Then I suggest you: 1. read https://docs.gradle.org/current/samples/sample_kotlin_application.html and see if it helps (after adding dependencies); 2. if it doesn't, ask about _that_, providing your `build.gradle` file. – Alexey Romanov May 25 '20 at 07:12

0 Answers0