Skip to content

Java Dev Environment Setup

Alex Hoffman edited this page Nov 17, 2023 · 24 revisions

Build Tool

Gradle is the build tool that is being used for the Opt4J/OpenDSE DSE.

Creating Wrapper

From the TensorDSE/DSE folder run gradle6 wrapper to create the wrapper binary gradlew.

IDE Integration

VS Code

Please note this if installing from the AURs. One will also need to install aur/code-marketplace.

Java Extension Pack

Click the Extensions view on the left hand side or press Ctrl+Shift+X. Search for and install Extensions Pack for Java and Gradle Tasks.

Default java version

You will need to be using JDK11 for this project, set java.import.gradle.java.home to the JDK11 location (for me it was /usr/lib/jvm/java-11-openjdk) for JDK11 to be used by default by Gradle. You will have to restart VS Code for this to take affect in VS Code.

Error from wrong version

If you're getting an error like

bad class file: /home/alxhoff/.gradle/caches/modules-2/files-2.1/com.github.SDARG.opt4j/opt4j-core/-SNAPSHOT/aa19f1fdb939b42dc1d52f86fd8e87169e8626b0/opt4j-core--SNAPSHOT.jar(org/opt4j/core/config/Icons.class) class file has wrong version 55.0, should be 52.0 

This is a result of using java-8-openjdk for example instead of java-11-openjdk. On arch run sudo archlinux-java set java-11-openjdk to set java 11. Check which version you are running with archlinux-java status.

Opening Project

Firstly open the DSE folder from File->Open Folder. Note you will not have a good time if you open the main repository folder in VSCode, as this will not trigger VSCode to recognize the Java project.

Building project

On the left select the Gradle Elephant, then navigate to TensorDSE->build->build.

Running and Debugging

The working directory needs to be changed to the TensorDSE folder (the one inside the DSE folder). To do this add "cwd": "${workspaceRoot}/TensorDSE" to the launch.json, which might need to be created first.

Select TensorDSE->Tasks->application->run from the Gradle menu.

Debugging

Click the bug icon next to run from the Gradle menu.

Eclipse IDE

For development in the Eclipse Java IDE is being used, available from the AURs as aur/eclipse-java.

The build.gradle file is the file specifying the dependencies for the build and it can be integrated into Eclipse to replace the default build method.

Importing Project

To use Gradle, one must import the project as a Gradle project when importing into Eclipse.

file->import->Gradle->Existing Gradle Project

select the Mapping DSE->TensorDSE folder and then Next.

Select Override workspace settings and then set the Specific Gradle Version to 6.9

Click Finish on import wizard and the project should then be imported and ready to go.

Building

Use the Gradle Tasks menu

(Neo)vim

Dependencies and Installation

# editor, java and build system
sudo pacman -S neovim gradle jdk-openjdk
# java eclipse language server (AuR)
paru -S jdtls

Debugger

A bit confusing because java is so painful, but it is what it is.

# needed to build java-debug binary
sudo pacman -S maven npm
mkdir ~/Programs && cd ~/Programs
git clone https://github.com/microsoft/java-debug           # ew
cd java-debug
./mvnw clean install
cd ~/Programs
git clone https://github.com/microsoft/vscode-java-test.git # ew
npm install && npm run build-plugin

Configuration

  1. Plugins

Exemplified using packer.nvim

-- treesitter
use { "nvim-treesitter/nvim-treesitter",
      config = function() require('plugins.treesitter') end,
      run = ":TSUpdate", }

-- debugger
use { "mfussenegger/nvim-dap",
      config = function() require('plugins.debugger') end,
  }

-- java specific lsp and debugger integration
use { "mfussenegger/nvim-jdtls", requires = {"mfussenegger/nvim-dap"} }
  1. Add java to your treesitter configuration.
require('nvim-treesitter.configs').setup({
	highlight = {
        enable = true,
        additional_vim_regex_highlighting = false
    },
	ensure_installed = {
    "c",
    .
    .
    .
    "java", --  just append it to the list
	}
})
  1. java lsp and debugging configuration.
-- sudo pacman -S gradle jdk-openjdk maven npm
-- paru -S jdtls
-- git clone https://github.com/microsoft/java-debug
-- ./mvnw clean install
-- git clone https://github.com/microsoft/vscode-java-test.git
-- npm install && npm run build-plugin

local home = os.getenv('HOME')
local root_markers = {'gradlew', '.git'}
local root_dir = require('jdtls.setup').find_root(root_markers)

local jar           = vim.fn.glob("/usr/share/java/jdtls/plugins/org.eclipse.equinox.launcher_*.jar")
local jdtls_config  = '/usr/share/java/jdtls/config_linux'
local bundles       = {
        vim.fn.glob(home .. "/Programs/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar"),
        vim.fn.glob(home .. "/Programs/vscode-java-test/java-extension/com.microsoft.java.test.plugin/target/*.jar"),
        vim.fn.glob(home .. "/Programs/vscode-java-test/java-extension/com.microsoft.java.test.runner/target/*.jar"),
        vim.fn.glob(home .. "/Programs/vscode-java-test/java-extension/com.microsoft.java.test.runner/lib/*.jar"),
}

local workspace_dir = home .. '/workspace'

local config = {
  cmd = {
    'java',

    '-Declipse.application=org.eclipse.jdt.ls.core.id1',
    '-Dosgi.bundles.defaultStartLevel=4',
    '-Declipse.product=org.eclipse.jdt.ls.core.product',
    '-Dlog.protocol=true',
    '-Dlog.level=ALL',
    '-Xms1g',
    '--add-modules=ALL-SYSTEM',
    '--add-opens', 'java.base/java.util=ALL-UNNAMED',
    '--add-opens', 'java.base/java.lang=ALL-UNNAMED',

    '-jar', jar,

    '-configuration', jdtls_config,

    '-data', workspace_dir
  },

  settings = {
    java = {}
  },

  init_options = {
        bundles = bundles
  },
}

config.on_attach = function(client, bufnr)
    require('jdtls').setup_dap({ hotcodereplace = 'auto' })
    require('jdtls.dap').setup_dap_main_class_configs()
end

require('jdtls').start_or_attach(config)
vim.cmd("command! DapJavaConfig lua require('jdtls.dap').setup_dap_main_class_configs()")

Clone this wiki locally