Code Java from Emacs with Eclim

Java development uses local IDEs such as IntelliJ IDEA and Eclipse for Windows and macOS, but I think that there are many people who develop scripting languages such as Node.js and Python as editors for Vim and Emacs. With Eclim, Java can be developed from the editor as well. If you build a development environment in a virtual machine in the cloud, you can do the same development at any time by connecting to SSH from the terminal without depending on local settings.

Virtual machine

Prepare a virtual machine in the cloud. This time, we will build a Java development environment on Ubuntu 16.04 LTS (Xenial Xerus).

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

Update the package.

$ sudo apt-get update && sudo apt-get dist-upgrade -y

Java

Install the Java 8 SDK. It does not have to be OpenJDK.

$ sudo apt-get install openjdk-8-jdk -y
$ java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2ubuntu1.16.04.3-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)

Eclipse Oxygen

Eclim connects to Eclipse and allows you to use some features of Eclipse from the editor. Download Eclipse Oxygen (4.7) released in June 2017.

$ wget http://ftp.yz.yamagata-u.ac.jp/pub/eclipse/technology/epp/downloads/release/oxygen/R/eclipse-java-oxygen-R-linux-gtk-x86_64.tar.gz
$ tar zxvf eclipse-java-oxygen-R-linux-gtk-x86_64.tar.gz
$ sudo mv eclipse /opt/

Emacs

Install Emacs24.

$ sudo apt-get install emacs24-nox emacs24-el -y
$ emacs --version
GNU Emacs 24.5.1

Cask

This is Cask for Emacs package management. Installation requires Git and Python.

$ sudo apt-get install git python -y
$ curl -fsSL https://raw.githubusercontent.com/cask/cask/master/go | python
$ echo 'export PATH="$HOME/.cask/bin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc

Prepare the following configuration file in the ~ / .emacs.d directory.

$ tree ~/.emacs.d
.emacs.d.bak/
├── Cask
├── init.el
└── inits
    ├── 00-keybindings.el
    ├── 01-menu.el
    ├── 02-files.el
    └── 08-eclim.el

ʻInit.eluses [init-loader](https://github.com/emacs-jp/init-loader) to split the file. Please use other than~ / .emacs.d / inits / 08-eclim.el` as you like.

~/.emacs.d/init.el


(require 'cask "~/.cask/cask.el")
(cask-initialize)

(require 'init-loader)
(setq init-loader-show-log-after-init nil)
(init-loader-load "~/.emacs.d/inits")

Describes the package to be installed on Cask. Eclim and company-emacs-eclim for Java development /company-emacs-eclim.el) is installed.

el:~/.emacs.d/Cask


(source gnu)
(source melpa)

(depends-on "cask")
(depends-on "init-loader")

;; java
(depends-on "eclim")
(depends-on "company-emacs-eclim")

Eclim settings follow the README. Use company-mode for pop-up dialogs and code completion.

~/.emacs.d/inits/08-eclim.el


(require 'eclim)

;; enable eclim-mode globally
(setq eclimd-autostart t)
(global-eclim-mode)

;; Eclipse installation
(custom-set-variables
  '(eclim-eclipse-dirs '("/opt/eclipse/eclipse"))
  '(eclim-executable "/opt/eclipse/eclim"))

;; Displaying compilation error messages in the echo area
(setq help-at-pt-display-when-idle t)
(setq help-at-pt-timer-delay 0.1)
(help-at-pt-set-timer)

;; Configuring company-mode
(require 'company)
(require 'company-emacs-eclim)
(company-emacs-eclim-setup)
(global-company-mode t)

The following are not required, but are frequently used settings in Emacs. Change the backspace keybindings with C-h.

~/.emacs.d/inits/00-keybindings.el


(define-key global-map "\C-h" 'delete-backward-char)
(define-key global-map "\M-?" 'help-for-help)

Hides the Emacs menu.

~/.emacs.d/inits/01-menu.el


(menu-bar-mode 0)

Delete blanks at the end of lines, do not make backups, tab settings, etc.

~/.emacs.d/inits/02-files.el


(when (boundp 'show-trailing-whitespace)
      (setq-default show-trailing-whitespace t))

(add-hook 'before-save-hook 'delete-trailing-whitespace)

(setq backup-inhibited t)
(setq next-line-add-newlines nil)
(setq-default tab-width 4 indent-tabs-mode nil)

(setq default-major-mode 'text-mode)

Finally, run the cask command to install the package.

$ cd ~/.emacs.d
$ cask install

Eclim

Virtual framebuffer Xvfb for headless use of Eclipse, which requires an X server, as described in Installing on a headless server Install (: //www.x.org/releases/X11R7.7/doc/man/man1/Xvfb.1.xhtml).

$ sudo apt-get install xvfb build-essential -y

Go to the Eclipse directory and install Eclipse.

$ cd /opt/eclipse
$ wget https://github.com/ervandew/eclim/releases/download/2.7.0/eclim_2.7.0.jar
$ java -Dvim.files=$HOME/.vim -Declipse.home="/opt/eclipse" -jar eclim_2.7.0.jar install

Start Xvfb and eclimd.

$ Xvfb :1 -screen 0 1024x768x24 &
$ DISPLAY=:1 /opt/eclipse/eclimd -b

Sample project

Create a sample project from the archetype of maven-archetype-quickstart and check the operation of Eclim. First, install Maven from SDKMAN!.

$ curl -s get.sdkman.io | /bin/bash
$ source ~/.sdkman/bin/sdkman-init.sh
$ sdk install maven
...
Setting maven 3.5.0 as default.

Run mvn archetype: generate in a directory of your choice. The Eclipse configuration file is also created with mvn eclipse: eclipse.

$ mkdir ~/java_apps && cd ~/java_apps
$ mvn archetype:generate \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false \
  -DgroupId=com.example \
  -DartifactId=spike
$ cd spike
$ mvn eclipse:eclipse

Start Emacs.

$ emacs

Import the project generated from the archetype into Eclipse.

M-x eclim-project-import

You will be asked for the Project Directory in the minibuffer, so specify the project directory.

Project Directory: ~/java_apps/spike/

Added date to Hello world!.

~/java_apps/spike/src/main/java/com/example/App.java


package com.example;
import java.util.Date;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        Date date = new Date();
        System.out.println( "Hello World!: " + date.getMinutes());
    }
}

Type up to date.get and you'll see suggestions in the pop-up and minibuffer. Select get Minutes with the cursor and press enter.

eclim.png

When I save the file with C-x C-s, I get ʻEclim reports 0 errors, 1 warnings.. The getMinutes` is underlined and if you move the cursor there you will get an error message in the minibuffer. Since it is Hello world, I will ignore it here.

eclim2.png

If you display the Java file with the main method in the buffer and execute ʻeclim-run-class`, the compilation and the main method will run.

M-x eclim-run-class
eclim3.png

Recommended Posts

Code Java from Emacs with Eclim
Execute Java code from cpp with cocos2dx
Java build with mac vs code
Execute packaged Java code with commands
Work with Google Sheets from Java
Sample code using Minio from Java
What I learned from doing Java work with Visual Studio Code
Prepare Java development environment with VS Code
Call Java library from C with JNI
API integration from Java with Jersey Client
Link Java and C ++ code with SWIG
Getting Started with Java Starting from 0 Part 1
[JaCoCo (Java Code Coverage)] Use with NetBeans
[Java] Flow from source code to execution
Script Java code
Java code TIPS
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
Java character code
Generate source code from JAR file with JD-GUI of Java Decompiler project
Static code analysis with Checkstyle in Java + Gradle
[Java] Set the time from the browser with jsoup
[Android] Read from QR code image with zxing
[Java] Explanation of Strategy pattern (with sample code)
Text extraction in Java from PDF with pdfbox-2.0.8
Make Java code coverage more comfortable with Jacoco 0.8.0
Use Matplotlib from Java or Scala with Matplotlib4j
Using Gradle with VS Code, build Java → run
Clean up findViewById from source code with DataBindingLibrary
Try debugging a Java program with VS Code
Build a Java development environment with VS Code
Call Java from JRuby
Build Java development environment with VS Code on Mac
Getting started with Java programs using Visual Studio Code
Build VS Code + WSL + Java + Gradle environment from scratch
Lombok with VS Code
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Change seats with java
Install Java with Ansible
Eval Java source from Java
Call a method with a Kotlin callback block from Java
Comfortable download with JAVA
Why can I develop Java with Visual Studio Code?
Access API.AI from Java
Switch java with direnv
I tried calling Java / Objective-C native code from Flutter
From Java to Ruby !!
[Note] Create a java environment from scratch with docker
Download Java with Ansible
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
Let's scrape with Java! !!
[Environment construction] Build a Java development environment with VS Code!
Read temperature / humidity with Java from Raspberry Pi 3 & DHT11
Build Java with Wercker
Build Java program development environment with Visual Studio Code
Endian conversion with JAVA
How to decompile apk file to java source code with MAC