1

Quick background: I'm working on an application to upload some csv data to a third party vendor. We have an AWS S3 bucket that watches for file uploads and the triggers a Lambda. The lambda then runs our kotlin code. The code downloads the newly arrived file from the bucket, builds a config file, and then executes a jar file (supplied by the vendor) which is responsible for actually sending the file along to it's final destination.

It is that last step that I am struggling with. What is the kotlin syntax for calling an executable jar? Is this even possible? If not, what alternative options are there?

For reference, here is the 'pipeline' function being called by the lambda:

fun pipeline(inputStream: java.io.InputStream, dp: INT295DependencyProvider): Either<Throwable,String> {

    val j = dp.journalFactory("app.eventmanagement.pipeline")
    val input = IOUtils.toString(inputStream, Charset.defaultCharset())
    j.debug(input)
    return dp.json.value().fromJson<S3EventNotification.S3Event>(input).fold({
        j.error("Error extracting key: " + it.message)
        it.left()
    },{
        event->
        j.debug("getting key from event: $event")
        val key = event.let { it.records?.firstOrNull()?.s3?.s3object?.key }
        j.debug("key: $key")

        if(key != null)
        {
            //Download the csv file from the S3 bucket
            val badgeFile: File = createTempFile(suffix = ".csv").toFile()
            badgeFile.copyInputStreamToFile( s3ReadObjectFromBucket(dp.bucket.value(), key, dp.s3.value()) )
            //build a config file for the jar
            val configFile = buildPropertiesFile(dp, "config",
                TsProperties(dp.baseUrl.value(), dp.appId.value(), dp.tsSecret.value(), dp.badgingKey.value()))

            //execute the jar to send the downloaded csv file to the vendor
            //java -jar badgeupload-2.0.jar --config config --badgesCsv badges
            "passed".right()
        }
        else
        {
            Throwable("Key not found").left()
        }
    })
}
3
  • 3
    One way would be to launch an external Java process that executes the jar. See stackoverflow.com/questions/35421699/… Commented May 10 at 2:32
  • 2
    Other than running an external process you could create a classloader for the new jar file, then use that classloader to load a class and run constructors/methods. This would execute in the given JVM. Which model are you after? Commented May 10 at 20:33
  • @dnault That method looks promising. I will give it a try. Commented May 13 at 15:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.