SlideShare a Scribd company logo
How to generate JAR file dynamically
compiling Java code in Kotlin
Mitsunori Komatsu
About me
• Mitsunori Komatsu, 

Software engineer at Treasure Data
• github:komamitsu

msgpack-java

(jackson-dataformat-msgpack),

fluency (yet another fluent logger java)

treasuredata_fdw (with td-client-rust)
Why did I try it?
• First, I developed just a tiny Lisp
interpreter in Kotlin for exercise
• But basically, interpreters aren’t so fast.
So, I tried dynamically compiling Lisp
code to Java byte code
• Finally, I tried outputting those Java
byte code to a JAR file
Where can we see the
source code?
https://github.com/komamitsu/tiny-
kotlin-lisp
How did I do that?
• Convert Lisp AST to Java source code
traversing the tree using Visitor pattern
• Compile the Java source code using
http://janino-compiler.github.io/janino/
• Extract the Java byte code with a bit hack
• Create a JAR file from the byte code and
dependent classes
Convert Lisp AST to Java source code
traversing the tree using Visitor pattern
@CompileVisitor.kt
Nothing special…
Convert Lisp AST to Java source code
traversing the tree using Visitor pattern
@CompileVisitor.kt
Nothing special…
http://janino-compiler.github.io/janino/
With Janino!!
Janino can not only compile a set of source files
to a set of class files like JAVAC, but also compile
a Java expression, block, class body or source
file in memory, load the bytecode and execute it
directly in the same JVM.
Compile the Java source code
Compile the Java source code
and Extract the Java byte code
• It’s really easy to dynamically compile
Java source code and invoke it with
Janino
• But Janino doesn’t provide any API to
get compiled Java byte code…
Compile the Java source code
and Extract the Java byte code
Compile
Extract
Access a hidden field
ByteArrayClassLoader.classes in Janino
using reflection!@CompileVisitor.kt
Create a JAR file from the byte
code and dependent classes
Prepare dependent class files
@CompileVisitor.kt
Create the compiled class file
Get Java byte code using
Class#getResourceAsStream
Create a JAR file from the byte
code and dependent classes
@CompileVisitor.kt
Setup a manifest
Create a JAR file reading class files
Pitfalls
@CompileVisitor.kt
• When writing Kotlin code, it easily has dependencies
with internal Kotlin classes (e.g
kotlin.KotlinNullPointerException). So the classes are
needed to be included in a JAR file to avoid runtime
failures.
Enjoy programming!

More Related Content

PPTX
Elixir - After 2 years in action + code WebUp
PDF
WebUp Feb 2017 - How (not) to get lost in bigger Ruby on Rails project.
PPTX
Kotlin - A Programming Language
PPTX
Gdg dev fest 2107 to kotlin, with love
PDF
Ruby and Rails short motivation
PPTX
Nodejs from zero to hero
PPTX
Java byte code presentation
PDF
PHP7.1 New Features & Performance
Elixir - After 2 years in action + code WebUp
WebUp Feb 2017 - How (not) to get lost in bigger Ruby on Rails project.
Kotlin - A Programming Language
Gdg dev fest 2107 to kotlin, with love
Ruby and Rails short motivation
Nodejs from zero to hero
Java byte code presentation
PHP7.1 New Features & Performance

What's hot (20)

PPT
Byte code jvm
PDF
Ruby an overall approach
PPTX
Kotlin For Android (at DevFest İzmir 2016)
PDF
The Real Thing: Java Virtual Machine
PDF
Tutorial for developing SILOptimizer Pass
PDF
Java Virtual Machine - Internal Architecture
PDF
Making CLI app in ruby
PPTX
Ruby programming
PPT
Introduction to Ruby on Rails
PPT
Indic threads pune12-akka-actors
PDF
Master the Art of the AST (and Take Control of Your JS!) - Yonatan Mevorach, ...
PPSX
Webpack & EcmaScript 6 (Webelement #32)
PPTX
Architecture diagram of jvm
PDF
Swift language - A fast overview of some features
PDF
Java introduction with JVM architecture
PPT
introduction to java
PDF
Bitter Java, Sweeten with JRuby
PPTX
Introduction to Ruby on Rails
PDF
JVM Interop - Functional Conf 2019
Byte code jvm
Ruby an overall approach
Kotlin For Android (at DevFest İzmir 2016)
The Real Thing: Java Virtual Machine
Tutorial for developing SILOptimizer Pass
Java Virtual Machine - Internal Architecture
Making CLI app in ruby
Ruby programming
Introduction to Ruby on Rails
Indic threads pune12-akka-actors
Master the Art of the AST (and Take Control of Your JS!) - Yonatan Mevorach, ...
Webpack & EcmaScript 6 (Webelement #32)
Architecture diagram of jvm
Swift language - A fast overview of some features
Java introduction with JVM architecture
introduction to java
Bitter Java, Sweeten with JRuby
Introduction to Ruby on Rails
JVM Interop - Functional Conf 2019
Ad

Similar to How to generate JAR file dynamically compiling Java code in Kotlin (12)

PPTX
How to run java program without IDE
PPTX
MWLUG - Universal Java
PDF
College Project - Java Disassembler - Description
PPTX
Object Oriented Programming - Java
PDF
Javanotes ww8
KEY
Java 7: Fork/Join, Invokedynamic and the future
PDF
Ola Bini Evolving The Java Platform
PDF
Advanced Debugging Using Java Bytecodes
PDF
What is Java
PDF
JVM for Dummies - OSCON 2011
PPTX
Java basics
How to run java program without IDE
MWLUG - Universal Java
College Project - Java Disassembler - Description
Object Oriented Programming - Java
Javanotes ww8
Java 7: Fork/Join, Invokedynamic and the future
Ola Bini Evolving The Java Platform
Advanced Debugging Using Java Bytecodes
What is Java
JVM for Dummies - OSCON 2011
Java basics
Ad

Recently uploaded (20)

PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Essential Infomation Tech presentation.pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
top salesforce developer skills in 2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
System and Network Administration Chapter 2
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Transform Your Business with a Software ERP System
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
medical staffing services at VALiNTRY
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PTS Company Brochure 2025 (1).pdf.......
Essential Infomation Tech presentation.pptx
How Creative Agencies Leverage Project Management Software.pdf
Odoo POS Development Services by CandidRoot Solutions
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
top salesforce developer skills in 2025.pdf
Softaken Excel to vCard Converter Software.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
System and Network Administration Chapter 2
Navsoft: AI-Powered Business Solutions & Custom Software Development
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Transform Your Business with a Software ERP System
VVF-Customer-Presentation2025-Ver1.9.pptx
medical staffing services at VALiNTRY
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx

How to generate JAR file dynamically compiling Java code in Kotlin

  • 1. How to generate JAR file dynamically compiling Java code in Kotlin Mitsunori Komatsu
  • 2. About me • Mitsunori Komatsu, 
 Software engineer at Treasure Data • github:komamitsu
 msgpack-java
 (jackson-dataformat-msgpack),
 fluency (yet another fluent logger java)
 treasuredata_fdw (with td-client-rust)
  • 3. Why did I try it? • First, I developed just a tiny Lisp interpreter in Kotlin for exercise • But basically, interpreters aren’t so fast. So, I tried dynamically compiling Lisp code to Java byte code • Finally, I tried outputting those Java byte code to a JAR file
  • 4. Where can we see the source code? https://github.com/komamitsu/tiny- kotlin-lisp
  • 5. How did I do that? • Convert Lisp AST to Java source code traversing the tree using Visitor pattern • Compile the Java source code using http://janino-compiler.github.io/janino/ • Extract the Java byte code with a bit hack • Create a JAR file from the byte code and dependent classes
  • 6. Convert Lisp AST to Java source code traversing the tree using Visitor pattern @CompileVisitor.kt Nothing special…
  • 7. Convert Lisp AST to Java source code traversing the tree using Visitor pattern @CompileVisitor.kt Nothing special…
  • 8. http://janino-compiler.github.io/janino/ With Janino!! Janino can not only compile a set of source files to a set of class files like JAVAC, but also compile a Java expression, block, class body or source file in memory, load the bytecode and execute it directly in the same JVM. Compile the Java source code
  • 9. Compile the Java source code and Extract the Java byte code • It’s really easy to dynamically compile Java source code and invoke it with Janino • But Janino doesn’t provide any API to get compiled Java byte code…
  • 10. Compile the Java source code and Extract the Java byte code Compile Extract Access a hidden field ByteArrayClassLoader.classes in Janino using reflection!@CompileVisitor.kt
  • 11. Create a JAR file from the byte code and dependent classes Prepare dependent class files @CompileVisitor.kt Create the compiled class file Get Java byte code using Class#getResourceAsStream
  • 12. Create a JAR file from the byte code and dependent classes @CompileVisitor.kt Setup a manifest Create a JAR file reading class files
  • 13. Pitfalls @CompileVisitor.kt • When writing Kotlin code, it easily has dependencies with internal Kotlin classes (e.g kotlin.KotlinNullPointerException). So the classes are needed to be included in a JAR file to avoid runtime failures.