Want this course?
Mixed projects (Clojure with some Java)
Course: JVM Fundamentals for Clojure
gen-interface, sometimes you need to write some Java. In this lesson, we see how to integrate Java source files into Leiningen and Boot projects.
Okay, so we've got a mixed project. We have a Clojure project that's mostly Clojure but there's a couple of Java files that we wanna keep. For whatever reason, Java is the right tool for the job. So how can we do this in leiningen? We wanna compile this Java file so that when the jvm starts up then it's accessible by the Clojure code. So here's my class file. As you can see, it's just an src jvm_playground. Jvm_playground, I've made it match the package. That's very important for Java.
Java like its packages and so it's in the source path for Clojure. I'm gonna go to the shell and I'm gonna run the javac command, lein javac. This is the leiningen. We're gonna do boot in a second. Now notice it didn't say anything. That's a bad sign because whenever leiningen compiles Java, it's gonna tell you so what's the problem there? The problem is we need to add the source paths and there's Java source paths. So we have to add the Java source paths and of course it's just src. Let's go back to the shell. We'll run it again. This time, it finds it. Now, it will also compile this if you do an uberjar or something but I just wanna see that...
Okay, so it has no problem with this Java file coexisting in the same directory as Clojure. You can of course put it in another directory. That's fine. Just make sure that your source paths... So let me write that out, source-paths. The default is src but you might do something like src/clj and src/java, like that but we're gonna keep it all in src. So they're coexisting and that means they're gonna share package names and namespace names and stuff like that. Okay now that is how you compile them but you might want some options.
So there's javac-options, which you can put here. I'm not gonna go over all the options. You can look the ones up that you need. I'm just gonna show you how you specify them. So if you wanna say I'm gonna target the jdk 1.6 and source also target 1.6 so you can do that and those will get set when you do lein javac.
Okay. Now, that's leiningen. Boot is a little different. So boot will find the Java files and compile them. It has its own command called boot javac but it takes an options. So if I want those same options, it's gonna be an array of things to pass to the javac program. Source, 1.6, like that. There, so that compiled the Java files into class files.