DEV Community

Vuong
Vuong

Posted on

4 2

Java LSP in Sublime Text

We never end up with one software. Sublime Text 4 has been released some days ago, I try to find out a guideline to set up Java LSP. The purpose just for better reading and more freedom on Java projects (for example, I can use Go to definition).

Prerequisites

LSP Settings

Search LSP Settings, then apply below configuration to that file. This configuration is specified for Mac user.

// for MacOS

{
    "clients":
    {
        "jdtls": {
            "command": [
                "java",
                "--add-modules=ALL-SYSTEM",
                "--add-opens",
                "java.base/java.util=ALL-UNNAMED",
                "--add-opens",
                "java.base/java.lang=ALL-UNNAMED",
                "-Declipse.application=org.eclipse.jdt.ls.core.id1",
                "-Dosgi.bundles.defaultStartLevel=4",
                "-Declipse.product=org.eclipse.jdt.ls.core.product",
                "-Dfile.encoding=UTF-8",
                "-DwatchParentProcess=true",  // false on windows, true other OSs
                "-Xmx1G",
                "-XX:+UseG1GC",
                "-XX:+UseStringDeduplication",
                "-jar",
                "<path_to>/jdt-language-server-1.1.2-202105191944/plugins/org.eclipse.equinox.launcher_1.6.100.v20201223-0822.jar", // 1. replace the PATH/TO with your own 2. replace * with the file version
                "-configuration",
                "<path_to>/jdt-language-server-1.1.2-202105191944/config_mac", // 1. replace the PATH/TO with your own 2. choose the config folder based on the OS
                "-data",
                "<replace_your_$TMPDIR_here>/T/<project_base_name>/jdt_ws" // replace <TEMP_DIR> with the temp folder in your system. macOS: echo $TMPDIR
            ],
            "enabled": true,
            "languageId": "java",
            "scopes": ["source.java"],
            "syntaxes": ["Packages/Java/Java.sublime-syntax"]
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

// for Windows

{
    "clients":
    {
        "jdtls": {
            "command": [
                "java",
                "--add-modules=ALL-SYSTEM",
                "--add-opens",
                "java.base/java.util=ALL-UNNAMED",
                "--add-opens",
                "java.base/java.lang=ALL-UNNAMED",
                "-Declipse.application=org.eclipse.jdt.ls.core.id1",
                "-Dosgi.bundles.defaultStartLevel=4",
                "-Declipse.product=org.eclipse.jdt.ls.core.product",
                "-Dfile.encoding=UTF-8",
                "-DwatchParentProcess=false",  // false on windows, true other OSs
                "-Xmx1G",
                "-XX:+UseG1GC",
                "-XX:+UseStringDeduplication",
                "-jar",
                "<path_to>\\jdt-language-server-1.1.2-202105191944\\plugins\\org.eclipse.equinox.launcher_1.6.100.v20201223-0822.jar", // 1. replace the PATH/TO with your own 2. replace * with the file version
                "-configuration",
                "<path_to>\\.lsp-tools\\jdt-language-server-1.1.2-202105191944\\config_win", // 1. replace the PATH/TO with your own 2. choose the config folder based on the OS
                "-data",
                "<path_to>\\.lsp-tools\\jdt_ws" // replace <TEMP_DIR> with the temp folder in your system. macOS: echo $TMPDIR
            ],
            "enabled": true,
            "languageId": "java",
            "scopes": ["source.java"],
            "syntaxes": ["Packages/Java/Java.sublime-syntax"]
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

LSP Key Binding

For simple purpose, I just want to use Go to definition. Open LSP Key Binding then paste it

[
    // Go To Definition
    {
        "command": "lsp_symbol_definition",
        "args": {
            "side_by_side": false
        },
        "keys": [
            "ctrl+d"
        ],
        "context": [
            {
                "key": "lsp.session_with_capability",
                "operator": "equal",
                "operand": "definitionProvider"
            },
            {
                "key": "auto_complete_visible",
                "operator": "equal",
                "operand": false
            }
        ]
    },
]
Enter fullscreen mode Exit fullscreen mode

Thanks to

To complete this setup, I have read some useful articles which help me figure out the right way to do.

Troubleshooting

By Cmd+Shift+P and search for "LSP", you can see some kind of log panel to help you debug if something goes wrong.

It's done. Happy coding!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay