Since Java 10 as I recall, we can run Java source files directly from the command line:
bash
$ java Math.java
The JVM compiles and executes the file automatically.
However, in zsh
, tab autocompletion with java does not list .java files—just .class, .jar, or other artifacts (perhaps .war and maybe others).
Minimal reproducible example:
Create a simple Java source file:
// Math.java
public class Math {
public static void main(String[] args) {
System.out.println("Hello from Math!");
}
}
Open a zsh shell and try tab-completing after typing:
$ java [ press <TAB> ]
Only Math.class or Math.jar appear. Math.java doesn’t.
Questions:
- Is it possible to configure zsh (or its completion system) so .java files are autocompleted with java?
EDIT:
I tried to the what in his answer. However, no luck. First, I downloaded the updated _java
file and I placed it where it was installed, ie.
❯ pacman -Ql zsh |grep java
zsh /usr/share/zsh/functions/Completion/Unix/_java
zsh /usr/share/zsh/functions/Completion/Unix/_java_class
I placed it under /usr/share/zsh/functions/Completion/Unix/_java
, replacing the original file. Then I logged out and then logged it. It hasn't fixed the issue. PLease have a look at the following screenshot:
In ~/src
I have a few *.java
files:
❯ ls *.java
NoNPE.java OpenPortScanner.java
I run oh-my-zsh
. To test whether this causes issues, I run zsh
in the following way: zsh -f
so that no config file was loaded. It turns out that there seems to be autocomplete functionality for *.java
files as know I see folders and *.java
files being suggested. In other words, oh-my-zsh
seems to be the problem. How can I fix that?
1 Answer 1
That's not what I observe. For me, java Tab
offers Math
(not Math.class
) where there's a Math.class
in the current working directory or $CLASSPATH
, and Math.jar
only on java -jar Tab
.
It's true the _java
completer from zsh 5.9, released in May 2022, the latest release as of writing, doesn't offer .java
file completions to the java
command. It was last modified in 2019 before java
commands accepting .java
files were common-place.
But the offering of .java
files in completion of the java
command was later added in February 2024 so will be available in the next release of zsh.
To get it in older releases, you can always pull the _java
completer from the development version and add it to a directory in your $fpath
(or replace the one from the zsh installation).
Note that bash
doesn't have programmed completion itself, but there's a separate bash-completion
project which you might be using where completion of .java
files for the java
command was added in May 2024. That completer is very rudimentary compared to zsh's.
-
1Many thanks for this. Could you have a look at my updated question?menteith– menteith2025年08月14日 09:02:43 +00:00Commented Aug 14 at 9:02
You must log in to answer this question.
Explore related questions
See similar questions with these tags.
zsh -f
, you don't get the advanced completion. You still need to run at leastautoload compinit; compinit
.echo $fpath
somewhere under your home directory. 2. When you change (as opposed to adding) a completion file, you need to arrange to regenerate the cache, see unix.stackexchange.com/questions/535509/… .