原创

使用 Neovim ide 运行 Java 文件

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I normally work on java projects and i recentely shifted to neovim from vscode, i have my lsp setup for java and everything but I cant run my files with just "java file.java" since i am using files from other classes. Is there any code runner type plugin that works perfectly in neovim specifically for java? For eg:

package com.khush.distributed.cache;

public class Main {
    public static void main(String[] args) {
        Cache cache = new Cache(4);
        cache.put("1", "1");
        cache.put("2", "2");
        cache.put("3", "3");
        cache.put("4", "4");
        cache.printList();
        System.out.println();
        System.out.println();
        
        cache.get("2");
        cache.printList();
        
        cache.put("5", "5");
        System.out.println();
        System.out.println();
        cache.printList();
    }
}

When i try to run this. it can't find the Cache class because obviously it is not compiled (ps: Cache class is in the same package)

Main.java:5: error: cannot find symbol
        Cache cache = new Cache(4);
        ^
  symbol:   class Cache
  location: class Main
Main.java:5: error: cannot find symbol
        Cache cache = new Cache(4);
                          ^
  symbol:   class Cache
  location: class Main
2 errors

[Process exited 1]

how can i fix this? here is my .lua config that i am using for running the code

return {
    "CRAG666/code_runner.nvim",
    config = function()
        require("code_runner").setup({
            filetype = {
                java = {
                    "cd $dir &&",
                    "javac $fileName &&",
                    "java $fileNameWithoutExt",
                },
                python = "python3 -u",
                typescript = "deno run",
                rust = {
                    "cd $dir &&",
                    "rustc $fileName &&",
                    "$dir/$fileNameWithoutExt",
                },
            },
        })
        vim.keymap.set("n", "<leader>r", ":RunCode<CR>", { noremap = true, silent = false })
        vim.keymap.set("n", "<leader>rf", ":RunFile<CR>", { noremap = true, silent = false })
        vim.keymap.set("n", "<leader>rp", ":RunProject<CR>", { noremap = true, silent = false })
    end,
}

apart from this plugin, i did try some other plugins aswell but i am not really that good with setting things up yet, Any help with this is appreciated, thank you.

正文到此结束
热门推荐
本文目录