Emacs lsp-java Project Settings

Update: 2023-2-25 use eglot instead(in the last of the article)

Create a java project with third libriaries support

lsp-java is an awesome package for lsp-mode to open a java project. However, after some testing, I found it has difficulty in importing third party libraries with pure java project(not use maven, Gradle). Moreover, it only has a limited ability to interact with a Gradle project. However, it has great support for maven project, which means we can use maven to import third party java libraries. This article will focus on import a local library by maven.

Initialize a project

1
2
mvn -B archetype:generate -DgroupId=PacMan -DartifactId=PacMan -Darchet
ypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

This command will create a folder named PacMan=(=artifactId), where there is folder called src, and its structure is like:

1
2
3
4
├── src
│   ├── main
│   │   └── java
│   │       └── PacMan(`groudId`)

If your groupId is "com.pacman.xx", then the directory structure may be like:

1
2
3
4
5
6
├── src
│   ├── main
│   │   └── java
│   │       └── com
                └── pacman
                    └── xx

In our example, you can write code in the pacman folder.

Specify java version:

edit pom.xml file in the project root directory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <!-- ... -->

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <!-- ... -->
</project>

Add third party libriary

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <!-- ... -->

  <dependencies>

    <!-- ... -->

    <dependency>
      <groupId>ucd.comp2011j</groupId>
      <artifactId>game-engine</artifactId>
      <version>1.00</version>
      <scope>system</scope>
      <systemPath>${basedir}/libs/COMP2011J.Game.Engine.jar</systemPath>
    </dependency>
  </dependencies>

  <!-- ... -->
</project>

Note that ${basedir} stands for the directory that pom.xml located.

Run Project:

create a Makefile in the project root directory:

1
2
3
4
5
6
7
8
all: compile

compile:
	mvn package
	java -cp "target/PacMan-1.0-SNAPSHOT.jar:<your third party libriary relative path>" PacMan.GameStart # <Package Name>.<Class Name>

clean:
	mvn clean

Use command make to run, and make clean to clean

In doom emacs, you can type <SPC>pR and enter make to run the project

Eglot

  1. make sure you have installed `jdtls`, and have a system java version >= 17
  2. to run the java project, you can use this `Makefile`

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
     # Makefile for c/c++ program
     #
     # ========================================
     # @author: Ziqi Yang<mr.ziqiyang@gmail.com>
     # @reference: https://makefiletutorial.com/#makefile-cookbook
     # ========================================
    
     SRC_DIR := src
     LIBS_DIR := libs
     BUILD_DIR := target
     ENTRY_POINT := Main
     ENTRY_FILE := $(SRC_DIR)/Main.java
     LIBS := $(shell find $(LIBS_DIR) -name "*.jar" | paste -sd ":" -)
     ARGS := 
    
    .PHONY: build run
     build:
       mkdir -p $(BUILD_DIR)
       javac -d $(BUILD_DIR) -cp .:$(LIBS) -sourcepath $(SRC_DIR) $(ENTRY_FILE)
    
     run:
       java -cp $(BUILD_DIR):$(LIBS) $(ENTRY_FILE) $(ARGS)