Problem
After compiling all relevant .kt files into .jar and .class files, running the main .jar initially works fine. However, if I perform any action in the application that references code from library files, it crashes with the NoClassDefFound
runtime error.
Request
An explanation as to how Kotlin looks for and resolves classpaths for .class (and potentially .jar?) files at runtime, that I may ensure Kotlin can successfully locate those dependencies.
Context
I’m experimenting with machine-learning AI models’ ability to self-inspect and self-modify. I’m also taking this opportunity to re-familiarize myself with the nuances of the Kotlin language tools. To make results scalable, as well as to minimize my resource footprint and potential security risks, I’m opting to use the CLI only, without any IDE or Build Manager whatsoever.
Current Situation
My code is organized within a total of 3 files: one Main file and two Libraries (“Lib1” and “Lib2”). After some minor difficulties, I was able to routinely compile and run the current application in its entirety with all the files located in the same directory. My troubles arose when organizing files into sub-directories. My current directory and file organization is as follows:
MainDir
│
├ Main.kt, Main.jar, Main.class
│
├ Lib1Dir
│ └ Lib1.kt, Lib1.jar, Lib1.class
└ Lib2Dir
└ Lib2.kt, Lib2.jar, Lib2.class
I obtained the current Main.jar
file by running kotlinc Main.kt -cp ./Lib1/Lib1.jar:./Lib2/Lib2.jar -include-runtime -d Main.jar
within MainDir, if that information is important. Each of the Lib.jar
files was generated via a kotlinc LibX.kt -d LibX.jar
invocation within Lib1Dir and Lib2Dir. I also generated .class files by running kotlinc FILE_NAME.kt
.
This automatically created directories based on the package names specified within each of the files. Although they are not listed, I did not initially delete them. In fact, I actually tried to initially run the application with the .class files remaining within those auto-generated extra directories. When I received the NoClassDefFound
runtime error, that’s when I moved the .class files up to the directories specified in the ASCII diagram listed above. I received the same error. I lastly attempted to move the Main.jar
file into the directory immediately above MainDir, just in case. This also yielded the same exact outcome.
If anyone could point out what I’m doing wrong here, I’d very deeply appreciate it. I understand that the vast majority of people prefer to let the IDE handle this minutia, but that’s not really an option for me in my current circumstance. Plus, this obstacle indicates to me that there’s clearly some misunderstanding or shortcoming in my knowledge, regarding the way Kotlin works. And I’d very much like to plug that gap.