Working with Java from Emacs within Leiningen project - java

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*")))

Related

Clojure and Java interop in a real world

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

Writing a Java program in Xcode

I am trying to begin writing a Java program in Xcode 4.4 but, At the moment, I am stuck with the file extension .cpp, which I believe is for C++. Can some please tell me how to set up a .java file (or project, or whatever the term is)...? I am extremely new to programming, and to Xcode, so please keep all instructions /very/ simple.
I have created some Xcode Templates for Xcode 4 here
http://www.2shared.com/file/hExLjJ1X/Java_Xcode4_template2.html
To install the template in Mac, use Terminal command
mkdir -p ~/Library/Developer/Xcode/Templates/
unzip ~/Downloads/Java_Xcode4_template2.zip -d ~/Library/Developer/Xcode/Templates/
You can use command-B to build. To Run, you should use "Edit Scheme..." and change the Executable for the "Run" Scheme
If you're new to programming you might want to try this in a different way.
Better IDEs for Java are Netbeans and Eclipse. I've used Netbeans for Java and PHP as well. It behaves as you could expect from a modern IDE with a lot of possibilities for customizing it to your needs/preferences.
Xcode is great and has many useful tools but it's focused on objective-c and iOS environment.
Good luck!
I am using XCode currently.
Open a new project.
Click External Build System.
Name your file, using the extension .java.
Write $(TARGETNAME).java next to $(ACTION).
Open a new file (empty style).
Start writing your program.
XCode is not the best place for Java, but I use it since it is a developer tool for Apple and it has great syntax coloring and auto-indentation, etc.
TextEdit is another app (probably already there if you use Apple Mac), but no syntax coloring and fancy features. It is simpler to use, and always use Terminal (try to avoid using the XCode Build - the errors are hard to decipher and the process is complicated, also if you have any questions, few can answer it because most do not use XCode).
Good luck!

Fast, easy to maintain, and parallelizable way to build java code?

I'm working on a build system that has bad practices piled on other bad practices for a long time and I'm in the process of re-writing the build. The languages involved are C/C++, Fortran, Ada, and Java and for the time being I'm sticking with GNU style makefiles -- though we're considering other alternatives such as SCons.
With that said, I'm looking for some specific recommendations -- not recommendations of the form "use a different build system", etc.
Whoever wrote the particular makefile I'm looking at right now had planned on a sequential build of java code that looks something like this:
LIST_OF_JAVA_FILES = file1.java
LIST_OF_JAVA_FILES += file2.java
LIST_OF_JAVA_FILES += file3.java
...
LIST_OF_JAVA_FILES += fileN.java
all: ${LIST_OF_JAVA_FILES}
${LIST_OF_JAVA_FILES} : %.class : %.java
${JAVAC} ${CLASSPATH} ${<}
Provided you perform a build in serial, this works fine. However, as soon as dependencies come into the mix it becomes more problematic.
C/C++ compilers have options built-in for dependency generation ... does a similar facility exist for Java?
Is it necessary to use third party tools (like Jikes) to generate dependencies?
Typically speaking, if a team were using makefiles for building anything java related, is it more typical to call the java compiler one time listing out all .java files in one command-line -vs- one target for each .class file?
If this is more common, is this strictly for simplicity sake or is there a deeper reason?
note: I understand that for a sequential build this would be the faster option, but I want an answer to do empirical tests for parallel builds
We had similar issue in our project.
We decided to build all java code via ant and invoke ant from a makefile.
Unless you have a lot of JNI (c++ <----> java) dependencies this might work for you too.
Good luck.
Doing something like
javac -cp MY_CLASSPATH Main.java
suffices most of the time, as it resolves dependences automatically. Sometimes you may need to use more files on the command line, since some of them may get used without an explicit reference.
There are also ant and maven and ivy and whatever, but it's an overkill for something that simple.
It ended up being most expedient (and the norm for java development as a whole) to just specify all source files in a single build step and allow javac to resolve all the dependencies that way.

Java and Clojure with Leiningen

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

Any way to provide Eclipse auto error-discovery feature in Vim/Emacs?

I know that Vim/Emacs offers some enticing mouse-free capabilities for typing out programs. I have tried both and I love these facilities. But when coding Java, especially when using the libraries extensively, the auto-complete and error-highlighting features of Eclipse are too useful to be set aside.
Is there a way to combine the best of both worlds?
maybe something like this: Eclim ?
Vim JDE (script) does this for vi, Eclim tries to make vi do everything Eclipse does in that sense, but also requires you to run Eclipse in headless mode while you're using vi.
For Emacs, you can try JDEE.
Good luck!
I think you have to go the other way, and put Emacs/VIM into Eclipse.
in Vim you have :make and :cn with :cN to go through compiler errors/warnings, but its not real time with auto spelling fixes like Eclipse.

Categories

Resources