How to compile all files in directory to *.class files?
3 Answers
Well, this seems pretty obvious, so I may be missing something
javac *.java
(With appropriate library references etc.)
Or perhaps:
javac -d bin *.java
to javac create the right directory structure for the output.
Were you looking for something more sophisticated? If so, could you give more details (and also which platform you're on)?

- 1,421,763
- 867
- 9,128
- 9,194
-
-
@Tom: Find that would depend on the operating system. It's dead easy on Unix, using find. Harder on Windows. – Jon Skeet Feb 09 '11 at 19:20
-
4*so I may be missing something*: Yes, one should not be inside the package directory when compiling, since then classes not explicitely named on the command line can't be found (i.e. these in other packages and not included in the compiler's bootstrap path). `javac super/package/dir/*.java` would be the right way to do it (maybe adding `-d`, `-cp`, and/or `-sourcepath`). – Paŭlo Ebermann Jul 04 '11 at 23:16
-
This is probably incomplete, because I get "`javac: file not found: *.java`" on Windows 7. – Tomáš Zato Feb 12 '16 at 15:58
-
@TomášZato: That just means you haven't got any `.java` files in the current directory. It's hard to compile source code when there aren't any source files... – Jon Skeet Feb 12 '16 at 16:00
-
@JonSkeet the files were in subdirectories. I really expected `javac` to be able to find them recursively. – Tomáš Zato Feb 12 '16 at 16:09
-
what if .java files are in different packages under `src` folder? – Asif Mushtaq Sep 28 '17 at 18:35
-
@UnKnown: Then you specify all the files in all the relevant directories. – Jon Skeet Sep 28 '17 at 18:36
Yet another way using "find" on UNIX is described here:
http://stas-blogspot.blogspot.com/2010/01/compile-recursively-with-javac.html
The following two commands will compile all .java files contained within the directory ./src
and its subdirectories:
find ./src -name *.java > sources_list.txt
javac -classpath "${CLASSPATH}" @sources_list.txt
First, find
generates sources_list.txt
, a file that contains the paths to the Java source files. Next, javac
compiles all these sources using the syntax @sources_list.txt
.

- 21,002
- 4
- 67
- 80
-
Consider improving your post since your answer is essentially a link. See: [Are answers that just contain links elsewhere really “good answers”?](http://meta.stackexchange.com/q/8231/156620) and [Why is linking bad?](http://meta.stackexchange.com/q/7515/156620) – Jul 05 '11 at 04:28
-
2Nice solution. You skip the temporary file by using /dev/stdin though. ie. `find ./src -name *.java | javac -classpath "${CLASSPATH}" @/dev/stdin` – Dunes Jun 26 '14 at 11:21
Here's a code fragment that I use to build an entire project where, as usual, source files are in a deeply nested hierarchy and there are many .jar files that must go into the classpath (requires UNIX utilities):
CLASSPATH=
for x in $(find | grep jar$); do CLASSPATH="$CLASSPATH:$x"; done
SRC=$(find | grep java$)
javac -cp "$CLASSPATH" $SRC

- 13,904
- 5
- 39
- 57