150

Use Vim as a Java IDE | SpaceVim

 6 years ago
source link: https://spacevim.org/use-vim-as-a-java-ide/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

SpaceVim

Blogs » Use Vim as a Java IDE

This tutorial introduces you to SpaceVim as a Java environment, by using the lang#java layer, you make SpaceVim into a great lightweight Java IDE.

Each of the following sections will be covered:

This tutorial is not intended to teach you Java itself.

If you have any problems, feel free to join the SpaceVim gitter chatting room for general discussion.

Enable language layer

lang#java layer is not loaded by default in SpaceVim, to use SpaceVim for java, you need to enable this layer in SpaceVim configuration file. Press SPC f v d to open SpaceVim configuration file, and add following section:

[[layers]]
  name = "lang#java"

Language server

To enable language server protocol support, you may need to enable lsp layer.

[[layers]]
  name = "lsp"
  filetypes = [
    "java"
  ]
  [layers.override_cmd]
    java = [
    "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=NONE",
    "-noverify",
    "-Xmx1G",
    "-jar",
    "D:\\dev\\jdt-language-server-latest\\plugins\\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar",
    "-configuration",
    "D:\\dev\\jdt-language-server-latest\\config_win",
    "-data",
    "C:\\Users\\Administrator\\.cache\\javalsp"
    ]

You need to replace D:\dev\jdt-language-server-latest\plugins\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar with the actual name of the org.eclipse.equinox.launcher jar

The configuration flag can point to either:

  • config_win, for Windows
  • config_mac, for MacOS
  • config_linux, for Linux

The data flag value should be the absolute path to the working directory of the server. This should be different from the path of the user’s project files (which is sent during the initialize handshake).

Code completion

javacomplete2 which has been included in lang#java layer provides omnifunc for java file and deoplete source. with this plugin and autocomplete layer, the completion popup menu will be opened automatically.

code complete

Code outline

The default outline plugin is tagbar, and the key binding is F2. This key binding will open an outline sidebar on the left.

java outline

To fuzzy find outline in current buffer, you need to enable a fuzzy find layer, for example denite layer, then press Leader f o:

java fuzzy outline

Rename symbol

To rename java symbol, you need to enable lsp layer for java. The default key binding for rename symbol under the cursor is SPC l e.

rename java symblo

Javadoc hovers

The default key binding to get doc of cursor symbol is SPC l d or K:

javadoc

Syntax lint

checkers layer provides asynchronous linting feature, this layer use neomake by default. neomake support maven, gradle and eclipse project. it will generate classpath automatically for these project.

lint-java

within above picture, we can see the checkers layer provides following feature:

  • list errors and warnings in quickfix windows
  • sign error and warning position on the left side
  • show numbers of errors and warnings on statusline
  • show cursor error and warning information below current line

Import packages

There are two kind features for importing packages, import packages automatically and manually. SpaceVim will import the packages after selecting the class name on popmenu. Also, you can use key binding SPC l i to import the class at the cursor point. If there are more than one class, a menu will be shown below current windows.

import class

Jump to test file

SpaceVim uses vim-project to manager the files in a project, you can add a .projections.json to the root of your project with following content:

{
  "src/main/java/*.java": {
    "alternate": "src/test/java/{dirname}/Test{basename}.java"
  },
  "src/test/java/**/Test*.java": { "alternate": "src/main/java/{}.java" }
}

with this configuration, you can jump between the source code and test file via command :A

jump-test

Code running

Base on JavaUnite, you can use SPC l r c to run current function or use SPC l r m to run the main function of current Class.

run-main

Code formatting

Code formatting is provided by format layer, which is loaded by default. The default format engine is neoformat, it will run google’s java formatter asynchronously. The key binding for formatting current file is SPC b f. To use this feature, you need to download the google’s java formatter jar, and set the path of this jar file in layer option.

[[layers]]
  name = 'lang#java'
  java_formatter_jar = 'path/to/google-java-format.jar'
format-java

you need to install jdk9 which provide a build-in tools jshell, and SpaceVim use the jshell as default inferior REPL process:

REPl-JAVA
Like • 1 Liked • 19 Comments Issue Page
  • HastyFish commented on Tue Apr 02 2019

    how to solution this problem
    [running] java -cp ".:/home/name/.cache/....." main.java
    Error: Could not find or load main class main
    Caused by: java.lang.ClassNotFoundException: main

  • wsdjeg commented on Tue Apr 02 2019

    It seems a issue with javaunite.vim, if you can reproduce this issue, please report bug to spacevim.

  • tiago-aguiar commented on Tue Apr 16 2019

    The checker always shows Error not found Symbol at my classes...

  • LuisJoseSanchez commented on Fri Jun 07 2019

    I can't even execute a simple HelloWorld. This is what I get:

    [Running] java -cp ".:/home/luisjose/.cache/vimfiles/repos/github.com/wsdjeg/JavaUnit.vim/bin:" HelloWorld


    Error: Could not find or load main class HelloWorld

    [Done] exited with code=1 in 0.151742 seconds

  • wsdjeg commented on Sat Jun 08 2019

    you need to save the file. checker layer will compile it.

  • LuisJoseSanchez commented on Sat Jun 08 2019

    Thank you. Now it works. I am used to the "autosave" functionality in Netbeans. It should be a nice improvement for SpaceVim to save the file before compilation if there are changes.

    ¡Thank you very much for your work! ¡SpaceVim is really cool!

  • LuisJoseSanchez commented on Sat Jun 08 2019

    As shown in this page, configuration layer refers to Eclipse files:

    [layers.override_cmd]
        java = [
        "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=NONE",
        "-noverify",
        "-Xmx1G",
        "-jar",
        "D:\\dev\\jdt-language-server-latest\\plugins\\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar",
        "-configuration",
        "D:\\dev\\jdt-language-server-latest\\config_win",
        "-data",
        "C:\\Users\\Administrator\\.cache\\javalsp"
        ]
    

    So I supose that I need to install Eclipse. I prefer not to install Eclipse in my computer. Is there an alternative configuration? Thank you.

  • wsdjeg commented on Sat Jun 08 2019

    You just need to download the language server, no need to install eclipse, I do not use it.

  • LuisJoseSanchez commented on Sat Jun 08 2019

    Ok. I manage to download "org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar". I also edit the path to make it accessible. But I can't find "config_linux" neither "javalsp".

  • adorogensky commented on Thu Sep 05 2019

    Where does .cache/javalsp come from? I'm installing on Linux

  • Worice commented on Mon Oct 28 2019

    I am using SpaceVim as Java IDE, just for fun. While configuring ISP, I found that the following windows based step:

    [[layers]]
      name = "lsp"
      filetypes = [
        "java"
      ]
      [layers.override_cmd]
        java = [
        "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=NONE",
        "-noverify",
        "-Xmx1G",
        "-jar",
        "D:\\dev\\jdt-language-server-latest\\plugins\\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar",
        "-configuration",
        "D:\\dev\\jdt-language-server-latest\\config_win",
        "-data",
        "C:\\Users\\Administrator\\.cache\\javalsp"
        ]
    

    How do I translate it on linux? Aside for the flag config_linux, I clearly cannot find D:\\..., C:\\... equivalency in my system, nor a folder such as jdt-language-server-latest. From Eclipse dedicated page, I am not able to deduce relevant information. Maybe because of my lack of experience. Thanks in advance for any hint, and keep up with the great work!

  • bmeyer0127 commented on Thu Mar 05 2020

    All I want is to be about to type "sout" + tab to get System.out.println();
    I suppose that is what what is mentioned as 'code completion'?

  • alexwareb9 commented on Mon Jun 15 2020

    Did you find a solution for this problem?

    how to solution this problem
    [running] java -cp ".:/home/name/.cache/....." main.java
    Error: Could not find or load main class main
    Caused by: java.lang.ClassNotFoundException: main

  • H-111-di commented on Wed Jul 22 2020 • edited

    I downloaded the lsp files and place it in the "RTE" folder and set up the spacevim like this:

    #Enable Java relevant layers
    [[layers]]
      name = "lang#java"
    
    [[layers]]
      name = "lsp"
      filetypes = [
      ┊ "java"
      ]
      [layers.override_cmd]
      ┊ java = [
      ┊ "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",
      ┊ "-noverify",
      ┊ "-Xmx1G",
      ┊ "-jar",
      ┊ "~/RTE/plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.1200.v20200508-1552.jar",
      ┊ "-configuration",
      ┊ "~/RTE/config_linux",
      ┊ "-data"
      ┊ ]

    But after this, I can't enable the lsp in the neovim...
    When I enter "space + l", nothing showed up.

    Thanks a lot if anybody can help me or give me any kind of hints...

  • 54cr4m3n70 commented on Mon Sep 07 2020

    why not coc-java?

  • ENate commented on Thu Sep 24 2020 • edited

    For Java on Spacevim:

    # Java
    [[layers]]
      name = "lang#java"
    
    [[layers]]
      name = "lsp"
      filetypes = [
        "java"
      ]
      [layers.override_cmd]
        java = [
    
    "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=NONE",
        "-noverify",
        "-Xmx1G",
    
    "-jar",
        "~/lsp/newlsp/repository/plugins/org.eclipse.equinox.launcher_1.5.700.v20200207-2156.jar"
            "-configuration",
            "~/lsp/newlsp/repository/config_linux"
            "-data",
            "~/.cache/javalsp"
    
    ] 
    

    It is important that you get org.eclipse.equinox.launcher_1.5.700.v20200207-2156.jar and ~/lsp/newlsp/repository/config_linux right (Note that versions may differ for the former). Usually both the config_linux and org.eclipse.equinox.launcher_1.5.700.v20200207-2156.jar are often available in same main folder (seeing from your snippet)! I used the ones from the latest version of eclipse Java EE. It seems you can get these files from an eclipse.org site or similar. It was a hassle to get it working at first, but when you do, you may never want to use any other IDE (because you get back your CPU/GPU) and enjoy the benefits of Spacevim as a Java IDE. Hope this helps.
    Note: Make sure you installed the proper version of pyvim (Use the healthcheck command to see your configs on Spacevim/neovim. Check your neovim (Spacevim for neovim) compatibility with Python2 or Python3. Conflicts can occur and cause a bit of headache. I had to uninstall Spacevim, neovim, vim a few times before deciding to configure everything to work using Python 3.6+ only.

  • Rishikesh01 commented on Fri Sep 10 2021

    Did you find a solution for this problem?

    how to solution this problem
    [running] java -cp ".:/home/name/.cache/....." main.java
    Error: Could not find or load main class main
    Caused by: java.lang.ClassNotFoundException: main

    Did you find the solution

  • CMOISDEAD commented on Sun Nov 28 2021

    Did you find a solution for this problem?

    how to solution this problem
    [running] java -cp ".:/home/name/.cache/....." main.java
    Error: Could not find or load main class main
    Caused by: java.lang.ClassNotFoundException: main

    Did you find the solution

    Did you find the solution?

Powered by Gitment

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK