{"id":3699,"date":"2017-07-20T04:00:44","date_gmt":"2017-07-20T04:00:44","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3699"},"modified":"2017-10-19T03:24:32","modified_gmt":"2017-10-19T03:24:32","slug":"how-to-run-your-kotlin-gradle-built-app-from-the-command-line","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/how-to-run-your-kotlin-gradle-built-app-from-the-command-line\/","title":{"rendered":"How to run your Kotlin gradle built app from the command line"},"content":{"rendered":"<p>So you build your Kotlin app, you went through the trouble of creating a build.gradle script that you build with<\/p>\n<p><code>gradle assemble<\/code><\/p>\n<p>this outputs a a &#8220;build\/libs\/kotlin.jar&#8221; .jar file, but you have no clue how to run your Kotlin code from the command line.<\/p>\n<p>Doing it by hand with &#8220;java -cp <insane classpath here>&#8221; is too much work, and there is a way to do<\/p>\n<p><code>gradle run<\/code><\/p>\n<p>or even<\/p>\n<p><code>gradle -b \/home\/myuser\/mykotlinapp\/build.gradle run<\/code><\/p>\n<p>in case you need to run your Kotlin script from a cronjob.<\/p>\n<p>Make sure you have the following inside your build.gradle script in order to make the &#8220;run&#8221; task available<\/p>\n<p><code><br \/>\napply plugin: 'application'<\/p>\n<p>\/\/ DO notice the \"Kt\" suffix on the class name below, if you don't use the Kt generated class you will get errors<br \/>\nmainClassName = 'com.myapp.MyKotlinAppKt'<\/p>\n<p>\/\/ optional:  add one string per argument you want as the default JVM args<br \/>\napplicationDefaultJvmArgs = [\"-Xms512m\", \"-Xmx1g\"]<br \/>\n<\/code><\/p>\n<p><strong>What if I don&#8217;t want to use gradle, and just java<\/strong><\/p>\n<p>ok&#8230; ok&#8230;<br \/>\n<code>java -cp $KOTLIN_LIB\/kotlin-runtime.jar:build\/libs\/kotlin.jar:<other jars here> com.myapp.MyKotlinAppKt<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So you build your Kotlin app, you went through the trouble of creating a build.gradle script that you build with gradle assemble this outputs a a &#8220;build\/libs\/kotlin.jar&#8221; .jar file, but you have no clue how to run your Kotlin code from the command line. Doing it by hand with &#8220;java -cp &#8221; is too much [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[1472,583,1576,1575,1449,1153,1367],"class_list":["post-3699","post","type-post","status-publish","format-standard","hentry","category-code","tag-command-line","tag-java","tag-jre","tag-kotlin","tag-linux","tag-posix","tag-unix"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-XF","jetpack-related-posts":[{"id":3315,"url":"https:\/\/www.gubatron.com\/blog\/gradle-how-to-add-a-list-of-local-jars-to-the-build-classpath\/","url_meta":{"origin":3699,"position":0},"title":"GRADLE: How to add a list of local .jar files to the build classpath","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"Sometimes you don't want\/cant use maven repos, all you have is a bunch of local jars on disk that you want to use as part of your compilation classpath, and the freaking gradle documentation is too vague. Here is an example: [pastacode lang=\"java\" manual=\"dependencies%20%7B%0Acompile%20files(%E2%80%98lib%2Fjars%2Fgettext-commons.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Flucene-3.5.0.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjaudiotagger.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fh2-1.3.164.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fmessages.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fslf4j-api-1.7.5.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fjaudiotagger.jar%E2%80%99%2C%0A%E2%80%98lib%2Fjars%2Fmetadata-extractor-2.6.2.jar%E2%80%99%0A)%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3802,"url":"https:\/\/www.gubatron.com\/blog\/gradle-groovy-a-simple-way-to-check-if-a-gradle-task-name-has-been-invoked-e-g-assemblerelease-for-android-developers\/","url_meta":{"origin":3699,"position":1},"title":"gradle\/groovy: A simple way to check if a gradle task name has been invoked (e.g. &#8220;assembleRelease&#8221; for Android developers)","author":"gubatron","date":"May 4, 2019","format":false,"excerpt":"If you google for this question, you'll find a bunch of crap answers about creating tasks and checking the task graph, bullshit. All you need to do is check if a parameter has been passed to gradle. Keep it simple and stupid: [pastacode lang=\"java\" manual=\"boolean%20isAssembleRelease%20%3D%20gradle.startParameter.taskNames.contains(%22assembleRelease%22)\" message=\"\" highlight=\"\" provider=\"manual\"\/] (If you're\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2019\/05\/simple.jpg?fit=1000%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2019\/05\/simple.jpg?fit=1000%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2019\/05\/simple.jpg?fit=1000%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2019\/05\/simple.jpg?fit=1000%2C667&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3321,"url":"https:\/\/www.gubatron.com\/blog\/solved-gradle-how-to-increase-the-java-compilers-available-heap-memory\/","url_meta":{"origin":3699,"position":2},"title":"[SOLVED] Gradle: How to increase the Java Compiler&#8217;s available Heap Memory","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"The documentation is not very clear on what all the available options are... after much Googling and many different attempts finally figured out how to raise the maximum heap of the compiler from within the gradle.build script. [pastacode lang=\"java\" manual=\"apply%20plugin%3A%20%E2%80%98java%E2%80%99%0A%0AcompileJava%20%7B%0A%2F%2Fraise%20heap%0Aoptions.fork%20%3D%20%E2%80%98true%E2%80%99%0Aoptions.forkOptions.with%20%7B%0AmemoryMaximumSize%20%3D%20%E2%80%9C2048m%E2%80%9D%0A%7D%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/] Update: So I've noticed this works great\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3331,"url":"https:\/\/www.gubatron.com\/blog\/gradle-how-to-copy-files-from-another-jar-into-your-resulting-output-jar\/","url_meta":{"origin":3699,"position":3},"title":"GRADLE: How to copy files from another .jar into your resulting output .jar","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"In our project we like to deliver a single jar as the final product, if you need to copy files that live on an existing jar into the Gradle's output jar, this example shows you how to do that (and more) [pastacode lang=\"java\" manual=\"jar%20%7B%0A%2F%2Fthis%20is%20how%20you%20change%20the%20name%20of%20the%20output%20jar%0AarchiveName%3D%E2%80%99frostwire.jar%E2%80%99%0A%0A%2F%2Fsome%20exclusion%20rules%20to%20keep%20your%20.jar%20clean%0Aexclude(%E2%80%98META-INF%2F*.SF%E2%80%99%2C%20%E2%80%98META-INF%2F*.DSA%E2%80%99%2C%20%E2%80%98META-INF%2F*.RSA%E2%80%99%2C%20%E2%80%98META-INF%2F*.MF%E2%80%99)%0A%0A%2F%2Fhere%20we%20grab%20all%20the%20.class%20files%20inside%20messages.jar%20and%20we%20put%20them%20in%20our%20resulting%20jar%0Afrom%20(zipTree(%E2%80%98lib%2Fjars%2Fmessages.jar%E2%80%99))%20%7B%0Ainclude%20%E2%80%98**%2F*.class%E2%80%99%0A%7D%0A%0A%2F%2Fhow%20to%20manipulate%20the%20jar%E2%80%99s%20manifest%0Amanifest%20%7B%0Aattributes%20%E2%80%98Main-Class%E2%80%99%3A%20%E2%80%98com.limegroup.gnutella.gui.Main%E2%80%99%0A%7D%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3874,"url":"https:\/\/www.gubatron.com\/blog\/coding-solved-gradle-build-android-breaks-after-upgrading-a-dependency-with-nullpointerexception-thrown-at-programclass-constantpoolentryaccept\/","url_meta":{"origin":3699,"position":4},"title":"[CODING\/SOLVED] gradle build (android) breaks after upgrading a dependency with NullPointerException thrown at ProgramClass.constantPoolEntryAccept","author":"gubatron","date":"February 27, 2020","format":false,"excerpt":"You've just upgraded one of your Android project's dependencies and when you .\/gradlew assembleRelease the build process breaks. You invoke it again with --stacktrace to find the following exception: \u00a0 [pastacode lang=\"java\" manual=\"java.lang.NullPointerException%0Aat%20proguard.classfile.ProgramClass.constantPoolEntryAccept(ProgramClass.java%3A537)%0Aat%20proguard.shrink.UsageMarker.markConstant(UsageMarker.java%3A1246)%0Aat%20proguard.shrink.UsageMarker.visitRequiresInfo(UsageMarker.java%3A1040)%0Aat%20proguard.classfile.attribute.module.ModuleAttribute.requiresAccept(ModuleAttribute.java%3A138)%0Aat%20proguard.shrink.UsageMarker.visitModuleAttribute(UsageMarker.java%3A739)%0Aat%20proguard.classfile.attribute.module.ModuleAttribute.accept(ModuleAttribute.java%3A99)%0Aat%20proguard.classfile.ProgramClass.attributesAccept(ProgramClass.java%3A619)%0Aat%20proguard.shrink.UsageMarker.markProgramClassBody(UsageMarker.java%3A124)%0Aat%20proguard.shrink.UsageMarker.visitProgramClass(UsageMarker.java%3A94)%0Aat%20proguard.classfile.visitor.MultiClassVisitor.visitProgramClass(MultiClassVisitor.java%3A67)%0Aat%20proguard.classfile.visitor.MultiClassVisitor.visitProgramClass(MultiClassVisitor.java%3A67)%0Aat%20proguard.classfile.visitor.ClassNameFilter.visitProgramClass(ClassNameFilter.java%3A128)%0Aat%20proguard.classfile.ProgramClass.accept(ProgramClass.java%3A430)%0Aat%20proguard.classfile.ClassPool.classesAccept(ClassPool.java%3A124)%0Aat%20proguard.classfile.visitor.AllClassVisitor.visitClassPool(AllClassVisitor.java%3A45)%0Aat%20proguard.classfile.visitor.MultiClassPoolVisitor.visitClassPool(MultiClassPoolVisitor.java%3A85)%0Aat%20proguard.classfile.ClassPool.accept(ClassPool.java%3A110)%0Aat%20proguard.shrink.Shrinker.execute(Shrinker.java%3A90)%0Aat%20proguard.ProGuard.shrink(ProGuard.java%3A381)%0Aat%20proguard.ProGuard.execute(ProGuard.java%3A145)%0Aat%20proguard.ProGuard.main(ProGuard.java%3A572)\" message=\"\" highlight=\"\" provider=\"manual\"\/] This is a ProGuard bug, which my friend, has been solved by the ProGuard\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3336,"url":"https:\/\/www.gubatron.com\/blog\/gradle-how-to-specify-resources-from-different-folders-on-your-sourceset\/","url_meta":{"origin":3699,"position":5},"title":"GRADLE: How to specify resources from different folders on your sourceSet","author":"gubatron","date":"July 29, 2014","format":false,"excerpt":"Sometimes you need to have resources in your sourceset and these may come from different locations on disk, the official documentation is lacking in real world examples, or at least I just didn't understand it very well, but from reading forums I finally got it to work. In the example\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3699"}],"version-history":[{"count":2,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3699\/revisions"}],"predecessor-version":[{"id":3706,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3699\/revisions\/3706"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}