Is it possible to easily manage and compile native Java classes alongside Clojure in a project using leiningen?
I am working at a pretty low level (with netty nio) and thinking that some of the plumbing classes would actually be easier to handle as raw java both in terms of constructing the code as well as performance.
As of Leiningen 2.x, :java-source-path has been replaced with :java-source-paths, whose value is now specified as a vector rather than a string.
A good place to find a full (up-to-date) documentation of Leiningen features is to peruse the sample project file. In this case, you will see:
:java-source-paths ["src/main/java"]
In Leiningen tutorial there is following statement
For projects that include some Java code, you can set the :java-source-path key in project.clj to a directory containing Java files. Then the javac compiler will run before your Clojure code is AOT-compiled, or you can run it manually with the javac task.
so it should work out of box if :java-source-paths option is set
Use Vinyasa - I wrote it especially to deal with this problem
Here is a blog post Dynamic reloading of java code in emacs/nrepl
Related
So i have tried to find an answer but didn't manage to do it yet. What I'm trying to do is to write a Java code for example in Atom editor, and compile it on Atom itself, without the need of an integrated development environment. Same goes with adding classes to a project.
Is that possible?
Yes. That is how they wrote the first Integrated Development Environments. Although the compilation step is typically performed externally from the editor, if the editor can execute external programs then you can "integrate" compilation. Adding "classes" is a little more involved, in that it requires your "editor" to understand the packaging and structure of the files composing the classes - at that point you have an integrated development environment.
Yes it's possible with atom package build. To install the package Edit-->Preferences-->Install and type build and hit Enter. Then you should configure your custom build command as mentioned in official package website. You may just fill cmd: javac in yml file for example
You need to install a package that will compile. Atom is a hackable text editor but not really an IDE as you pointed out. That said there are a lot of packages for compiling and for working with java.
You can look at Build Tools Package for compiling. It looks like it will work for Java but there are many compilers in the package list.
I would search on their package site for the keyword "Java" to see what java packages you might want to use. Then do another search for "Compile" to see what compiler you might want to use if the build tool above doesn't work for you. You can search atom packages at https://atom.io/packages.
I'm thinking about start using (not playing with) Clojure. Are there any useful guides? I'm not asking about lein, javac or any other 'small' manual tools. I need to know how to have Java and Clojure sources in eclipse in the same project. How to make them calling each other without compilation errors? How to configure maven? How to set up fully productive development environment? Is it even possible at the moment? What plugins may be useful? Where to start?
I have a fully working production setup with Eclipse, Maven and Clojure that is working very nicely at the moment. Hopefully it is helpful as an example of a good polyglot setup within a Java IDE.
I don't use leiningen - Nothing against lein at all - it's very nice and ideal in a pure Clojure/CLI world. But I've found that pure Maven is nicer to work with in a polyglot Java+Clojure environment with an IDE since the tool integration is so much better. Also from an ecosystem / audience / community perspective if you want people from the Java world to be able to build your source you are going to cause a lot less confusion if you just use Maven directly.
Here is my setup:
Eclipse 4.2 as main IDE
Counterclockwise Eclipse plugin - very good, takes care of REPL, Clojure editing etc.
Maven used to manage all projects (I use the built in Eclipse Maven integration mostly, but occasionally use the CLI version as well)
cljunit used to enable JUnit tests to run on Clojure parts of the project
Github / Travis CI used for SCM and Continuous integration, accessed using the built-in EGit team provider in Eclipse
In terms of actually how I manage / set up the project itself:
I configure everything with Maven, using standard Maven directory layout. Polyglot Java+Clojure Projects typically have both src/main/java and src/main/clojure
Clojure is just a Maven dependency, like any other Java library.
I make the Clojure source directories into resource directories in the Maven setup. This means that the .clj files get bundled in any jars and can be loaded / run dynamically at runtime.
I usually make the entry point on the Java side with a public static void main(...) as usual, but call quite quickly into the Clojure code. See this blog post on calling Clojure from Java.
Finally some coding tips for polyglot Java+Clojure
I find that Java is better for low level data structures, libraries and algorithms, while Clojure is better for integrating things together and "glue" code.
Clojure calling Java is usually easier / more elegant than the other way round. Also it makes sense since you generally want the dependencies to flow that way (higher level code calling lower level code)
If you make all your Java classes immutable, they play very nicely in a Clojure world with minimal effort.
Sometimes it is worth making one or more of your Java classes implement some of the Clojure interfaces, particularly clojure.lang.IFn for example. This way your Java objects can act as first class functions in Clojure code.
Here's an example project that mixes Java and Clojure source:
https://github.com/mikera/enlight
I also wrote a small library (clojure-utils) that includes some example code for calling Clojure from Java, which you may find useful.
Despite your tone about leiningen, I recommend looking at it. Leiningen supports Java compilation, so combining java and clojure sources in one project isn't a problem.
The Counterclockwise plugin, the clojure plugin for Eclipse, can work with leiningen project files (project.clj). So within Eclipse you have dependency management and java compilation all handled for you by defining the right things in project.clj, without the need to install leiningen separately or execute commands from the command line.
In the project.clj set :java-source-paths, like for example:
:java-source-paths ["src/main/java"]
In package src/main/java put a class Foo:
package main.java;
public class Foo {
public static final String value = "Michiel";
}
Somewhere in a clojure source file define this function and "Michiel" will be printed when it is called:
(defn foo
"I don't do a whole lot."
[]
(println (main.Foo/value)))
Further reading:
http://leiningen.org/
http://code.google.com/p/counterclockwise/
You could also try the framework "Funky". It will completly seperate you Clojure and Java code . Just have a look at https://github.com/windler/Funky
I'm currently creating a personal (maybe public) java terminal. I want to create a command that will create + compile a Java file on execution, except I'm not too sure on how to actually do this. Is it possible? Or am I just dreaming?
You could also use Groovy - it's quite handy if you just want to compile and run a line or two of Java code from within your application. The application may be in regular Java, with Groovy used only for compilation of the dynamically generated code. Whichever solution you choose, be careful, as executing user input as code can lead to security issues (vulnerability to injection attacks).
compile a Java file
See the STBC. It uses the JavaCompiler to compile the code in the text area.
I agree with #eee's comment that javax.script is probably a very nice fit for your project, script code is easier to deal with than Java code. I've successfully used it in the past for a plugin API, I don't remember having had any problems to get it up and running.
Most projects that I know of that compile real Java at runtime use the Eclipse compiler to do so. The Java 6 javac can be accessed completely programmatically as well. I've never used either of these myself. These two and some other compilers can be accessed via Commons-JCI if desired.
There's plenty of closely related questions on SO, just to name some of them:
Java and Clojure with Leiningen
What is the best way to do Java development in Emacs?
Best java tools for emacs
The difference of my questions is that I want to know how to make working with Java as easy as with Clojure right from Emacs within mixed Clojure/Java Leiningen project.
What I mean is possibility to (in order of priority):
compile Java code right from Emacs (note: it is possible to compile it from the shell with lein javac)
use code completion for Java (tools like JDEE provide this feature, but it is separate tool that doesn't concern Leiningen project setup)
debug Java code
Check out the Emacs LSP project. It provides support for Java.
I want to try auto complete but I haven't gotten to it yet.
Given the way leiningen/Clojure work anyway, I'm not sure how easy you could make compilation. When you compile Java code, don't you have to restart the repl/application for the JVM to reload it anyway?
Malabar-mode is abandonware but I think it's shooting for the level of functionality that you want. It seems to me that a coprocess-based autocompletion engine should be feasible with CEDET, leiningen, and swank-clojure, but I have a day job and side work...
Probably not what you wanted exactly, but this should handle bullet 1 for ya:
(defun lein-javac (&optional PROJECT-DIR)
(interactive)
(let ((output-buffer (progn
(with-output-to-temp-buffer "*lein-javac*" nil )
(select-window (get-buffer-window "*lein-javac*"))
(read-only-mode 'toggle)
(window-buffer)) ))
(shell-command (concat "cd " (or PROJECT-DIR default-directory)
" && lein javac &") "*lein-javac*")))
In Eclipse, using the CCW plug-in, I want to load a clojure file into a REPL. The problem is that I have an import statement for one of my own java classes, but apparently it is not in my classpath.
(ns my-clj-ns
(:import [alg.gen Enumerator]))
Do I have to make jars out of every class that I want use/test in a Clojure REPL?
Currently, trying to load my clj into a REPL results in an error:
"Load file in Clojure REPL" did not complete normally. Please see the log for more information.
java.lang.NullPointerException
Any help would be greatly appreciated.
You can let leiningen compile these for you using,
:javac-options {:destdir "classes/"}
:java-source-path "src/main/java" ; location of Java source
options or manually compile them and move the class files to the classes/ directory. No need to create a jar.
When you're in the ccw repl, you can hit alt-e to see the stack trace. If you're getting a NullPointerException, I don't think its a classpath issue.
Your code looks fine to me.
I suspect the issue is with your Eclipse Java Build Path, which determines what Eclipse includes in the classpath for your application.
In particular, if your Java class is in a separate project, you will need to either add that project to the build path (right click on project / Properties / Java Build Path / Projects) or package it as a jar.
When you start to have more sophisticated build requirements, you may also want to start looking at Maven to handle this kind of thing for you. Maven is a pain to learn / set up in the first place but it pays of in the long run.
Leiningen is also a great tool to use but I personally don't use it for the following reasons:
It is great on the command line, but doesn't integrate so nicely with an Eclipse workflow
Maven is more widely used and better supported in the Java world
There is really nice guide if you want to learn how to do this.
https://github.com/technomancy/leiningen/blob/master/doc/MIXED_PROJECTS.md
But in gist, have a project definition like the following for Java source code.
(defproject megacorp/superservice "1.0.0-SNAPSHOT"
:description "A Clojure project with a little bit of Java sprinkled here and there"
:source-paths ["src/clojure"]
:java-source-paths ["src/java"])