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.
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
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.
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.
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
Recommended Posts