DEV Community

vuong ⛈️
vuong ⛈️

Posted on

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!

Top comments (0)