For a bit of background, I'm trying to set up a function that runs a java file in Neovim, I've tried using the following autocmd:
vim.api.nvim_create_autocmd(
{ "FileType" },
{
command = "map <buffer> <F5> :w<cr>:exec '!java' shellescape(@%,1)<cr>",
pattern = { 'java' },
group = RunFile,
}
)
which does work, however if I ask for input (effectively interrupting the program) it gives me an error.
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at com.mycompany.app.CoolApp.main(CoolApp.java:15)
shell returned 1
Now maybe there's an easier solution to my problem (if so please tell) but the solution I came up with is to start a new terminal using :terminal
and running an equivalent of java %
there.
trying that very literally map <buffer> <F5> :w<cr>:terminal 'java %'
gives me the error no such file or directrory
, which doesn't make sense to me because it perfectly gives me the path from where I opened neovim to the .java file (for the record it also doesn't work if I start from my home directory, or even from root), if I try doing things manually though (first :terminal, then typing java path/to/file.java) then it does work. I'm very confused, does anyone have any ideas?
:map <buffer> <F5> :w<CR>:terminal java %<CR>
work?