0

I would like to be able to compile all java files in all subdirectories from what I'm working in.

I know how to compile all java files in the same directory, but how do I do it for all subdirectories (and subdirectories of those, etc.)?

user473973
  • 681
  • 2
  • 12
  • 23
  • Which IDE or build tool are you using? I wouldn't suggest you use the command line unless you like doing things the hardest way possible. (In which case you are not alone) – Peter Lawrey Sep 08 '13 at 16:37
  • I am using the command line. – user473973 Sep 08 '13 at 16:40
  • I use maven and an IntelliJ. When I want to run a program, I don't need to think about which files/directories need to be re-compiled, I just press `Run` to run or `Debug` to debug. – Peter Lawrey Sep 08 '13 at 16:43
  • Instead of writing individual solutions, I'd provide you the link to another **stackoverflow** post that answers it well. http://stackoverflow.com/questions/6623161/javac-option-to-compile-recursively – Sureshkumar Saroj Sep 08 '13 at 16:50

2 Answers2

0

try using ant script, give the base dir of source file to srcdir

<target name="compile">
    <javac srcdir="src" destdir="build/classes"/>
</target>
upog
  • 4,309
  • 6
  • 30
  • 59
0

There are multiple ways of doing this.

Using Javac

You have to know all the directories beforehand, or be able to use wildcard. The command goes this way:

javac dir1/*.java dir2/*.java dir3/dir4/*.java dir3/dir5/*.java dir6/*src/*.java

(OR)

If you can create a list of all the *.java files in your project, it's easy:

Linux

$ find -name "*.java" > sources.txt
$ javac @sources.txt

Windows

> dir /s /B *.java > sources.txt
> javac @sources.txt

Using a build tool

It is always better to use a tool that was designed to build software.

Using Ant

If you create a simple build.xml file that describes how to build the software:

<project default="compile">
    <target name="compile">
        <mkdir dir="bin"/>
        <javac srcdir="src" destdir="bin"/>
    </target>
</project>

you can compile the whole software by running the following command:

$ ant

Now that you know all the ways (at least, most ways), the choice is yours! :-)

VictorCreator
  • 714
  • 1
  • 7
  • 15
  • Thanks! So would `javac */.java` work? That would only work for one directory level though. – user473973 Sep 08 '13 at 16:43
  • edited the answer with more information. If the answer helped, please upvote, and "accept as answer". It helps newbies like me @stackoverflow :-) – VictorCreator Sep 08 '13 at 16:47
  • For multiple directories, I would suggest you use the ant script. However, if you want to keep things simple, you can use the "find" or "dir" option (i've mentioned those in my answer). That works for all subdirectories! – VictorCreator Sep 08 '13 at 16:49
  • Thanks. Although I'm trying to run this all from a C++ file using `system()` calls. Thanks for your help though. – user473973 Sep 08 '13 at 16:50