javac option to compile all java files under a given directory recursively





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







110















I am using the javac compiler to compile java files in my project. The files are distributed over several packages like this: com.vistas.util, com.vistas.converter, com.vistas.LineHelper, com.current.mdcontect.



Each of these packages has several java files. I am using javac like this:



javac com/vistas/util/*.java com/vistas/converter/*.java
com.vistas.LineHelper/*.java com/current/mdcontect/*.java


(in one line)



Instead of giving so many paths, how can I ask the compiler to compile recursively all the java files from the parent com directory?










share|improve this question




















  • 1





    You should really have a look at tools such as Ant or Maven.

    – Laurent Pireyn
    Jul 8 '11 at 10:30











  • This SO post might be useful stackoverflow.com/questions/864630/…

    – Gopi
    Jul 8 '11 at 10:30


















110















I am using the javac compiler to compile java files in my project. The files are distributed over several packages like this: com.vistas.util, com.vistas.converter, com.vistas.LineHelper, com.current.mdcontect.



Each of these packages has several java files. I am using javac like this:



javac com/vistas/util/*.java com/vistas/converter/*.java
com.vistas.LineHelper/*.java com/current/mdcontect/*.java


(in one line)



Instead of giving so many paths, how can I ask the compiler to compile recursively all the java files from the parent com directory?










share|improve this question




















  • 1





    You should really have a look at tools such as Ant or Maven.

    – Laurent Pireyn
    Jul 8 '11 at 10:30











  • This SO post might be useful stackoverflow.com/questions/864630/…

    – Gopi
    Jul 8 '11 at 10:30














110












110








110


40






I am using the javac compiler to compile java files in my project. The files are distributed over several packages like this: com.vistas.util, com.vistas.converter, com.vistas.LineHelper, com.current.mdcontect.



Each of these packages has several java files. I am using javac like this:



javac com/vistas/util/*.java com/vistas/converter/*.java
com.vistas.LineHelper/*.java com/current/mdcontect/*.java


(in one line)



Instead of giving so many paths, how can I ask the compiler to compile recursively all the java files from the parent com directory?










share|improve this question
















I am using the javac compiler to compile java files in my project. The files are distributed over several packages like this: com.vistas.util, com.vistas.converter, com.vistas.LineHelper, com.current.mdcontect.



Each of these packages has several java files. I am using javac like this:



javac com/vistas/util/*.java com/vistas/converter/*.java
com.vistas.LineHelper/*.java com/current/mdcontect/*.java


(in one line)



Instead of giving so many paths, how can I ask the compiler to compile recursively all the java files from the parent com directory?







java javac






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 20:57









sepp2k

300k39602617




300k39602617










asked Jul 8 '11 at 10:26









user496934user496934

1,40663148




1,40663148








  • 1





    You should really have a look at tools such as Ant or Maven.

    – Laurent Pireyn
    Jul 8 '11 at 10:30











  • This SO post might be useful stackoverflow.com/questions/864630/…

    – Gopi
    Jul 8 '11 at 10:30














  • 1





    You should really have a look at tools such as Ant or Maven.

    – Laurent Pireyn
    Jul 8 '11 at 10:30











  • This SO post might be useful stackoverflow.com/questions/864630/…

    – Gopi
    Jul 8 '11 at 10:30








1




1





You should really have a look at tools such as Ant or Maven.

– Laurent Pireyn
Jul 8 '11 at 10:30





You should really have a look at tools such as Ant or Maven.

– Laurent Pireyn
Jul 8 '11 at 10:30













This SO post might be useful stackoverflow.com/questions/864630/…

– Gopi
Jul 8 '11 at 10:30





This SO post might be useful stackoverflow.com/questions/864630/…

– Gopi
Jul 8 '11 at 10:30












9 Answers
9






active

oldest

votes


















191














I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).



Using Javac



If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that javac offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javac with the @ prefix.



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



# Linux / MacOS
$ find -name "*.java" > sources.txt
$ javac @sources.txt

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




  • The advantage is that is is a quick and easy solution.


  • The drawback is that you have to regenerate the sources.txt file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.


Using a build tool



On the long run it is 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




  • The advantage is that you are using a standard build tool that is easy to extend.


  • The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.


Using Maven



Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.





  • It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.


  • The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).


Using an IDE



Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.



They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the background so you can hunt down occasional errors like a ClassNotFoundException.



One additional note



For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple mvn eclipse:eclipse command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continuous integration server for example. Piece of cake :-)






share|improve this answer





















  • 4





    When using javac, it would be better to specify an output directory. find -name "*.java" > sources.txt && javac -d bin @sources.txt. Otherwise *.class files are saved to the directory where sources are.

    – Maksim Dmitriev
    Aug 14 '14 at 7:45








  • 1





    Absolutely true. Although in my opinion, if someone has just started to play around with javac, the concept of CLASSPATH, how to run code with java, how to deal with packages, which should be the root folder for running, etc. are usually not clear. Thus I omitted the output dir. Anyway, thanks for the suggestion!

    – rlegendi
    Aug 14 '14 at 8:25






  • 3





    For mac users coming across this, the find command is: find . -name "*.java" > sources.txt (note the .)

    – MrDuk
    Sep 21 '15 at 6:35











  • @MrDuk What does adding the "." do? Is it for searching within the current directory?

    – Brady Sheehan
    Nov 21 '15 at 22:54











  • @BradySheehan it will start to search from the given path. "." means start from the current dictionary. Note that you have to specify a path for find (in OS X)

    – Kris
    Jan 15 '16 at 12:45



















39














find . -name "*.java" -print | xargs javac 


Kinda brutal, but works like hell. (Use only on small programs, it's absolutely not efficient)






share|improve this answer





















  • 1





    If you use this consider find ... -print0 and xargs -0 ... instead to not break on spaces in filenames

    – sapht
    Feb 7 '18 at 19:01



















27














If your shell supports it, would something like this work ?



javac com/**/*.java 


If your shell does not support **, then maybe



javac com/*/*/*.java


works (for all packages with 3 components - adapt for more or less).






share|improve this answer





















  • 2





    This works and is actually more of a direct answer to the question.

    – Daniel Macias
    Mar 20 '14 at 17:01











  • I have attempted to use this in command prompt on windows 10. Can someone confirm if it works on windows 10 or if I am doing it wrong please

    – Dan
    Dec 29 '15 at 12:11



















21














In the usual case where you want to compile your whole project you can simply supply javac with your main class and let it compile all required dependencies:



javac -sourcepath . path/to/Main.java






share|improve this answer
























  • Very simple method, does not rely on extra files

    – linquize
    Dec 27 '15 at 2:20











  • This is the best and the simplest one for people who don't have much time to learn Ant (like me)

    – Hamza Abbad
    Mar 27 '16 at 16:15











  • It's lazy unfortunately. If you don't touch Main.java, which you probably wouldn't just after creating your second file, nothing else gets compiler.

    – Tom Hawtin - tackline
    Oct 1 '18 at 22:23



















3














I would advice you to learn using ant, which is very-well suited for this task and is very easy to grasp and well documented.



You would just have to define a target like this in the build.xml file:



<target name="compile">
<javac srcdir="your/source/directory"
destdir="your/output/directory"
classpath="xyz.jar" />
</target>





share|improve this answer































    2














    I'm just using make with a simple makefile that looks like this:



    JAVAC = javac -Xlint:unchecked
    sources = $(shell find . -type f -name '*.java')
    classes = $(sources:.java=.class)

    all : $(classes)

    clean :
    rm -f $(classes)

    %.class : %.java
    $(JAVAC) $<


    It compiles the sources one at a time and only recompiles if necessary.






    share|improve this answer































      1














      javac command does not follow a recursive compilation process, so you have either specify each directory when running command, or provide a text file with directories you want to include:



      javac -classpath "${CLASSPATH}" @java_sources.txt





      share|improve this answer

































        1














        javac -cp "jar_path/*" $(find . -name '*.java')



        (I prefer not to use xargs because it can split them up and run javac multiple times, each with a subset of java files, some of which may import other ones not specified on the same javac command line)



        If you have an App.java entrypoint, freaker's way with -sourcepath is best. It compiles every other java file it needs, following the import-dependencies. eg:



        javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java



        You can also specify a target class-file dir: -d target/.






        share|improve this answer































          0














          I've been using this in an Xcode JNI project to recursively build my test classes:



          find ${PROJECT_DIR} -name "*.java" -print | xargs javac -g -classpath ${BUILT_PRODUCTS_DIR} -d ${BUILT_PRODUCTS_DIR}





          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6623161%2fjavac-option-to-compile-all-java-files-under-a-given-directory-recursively%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            9 Answers
            9






            active

            oldest

            votes








            9 Answers
            9






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            191














            I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).



            Using Javac



            If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that javac offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javac with the @ prefix.



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



            # Linux / MacOS
            $ find -name "*.java" > sources.txt
            $ javac @sources.txt

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




            • The advantage is that is is a quick and easy solution.


            • The drawback is that you have to regenerate the sources.txt file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.


            Using a build tool



            On the long run it is 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




            • The advantage is that you are using a standard build tool that is easy to extend.


            • The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.


            Using Maven



            Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.





            • It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.


            • The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).


            Using an IDE



            Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.



            They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the background so you can hunt down occasional errors like a ClassNotFoundException.



            One additional note



            For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple mvn eclipse:eclipse command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continuous integration server for example. Piece of cake :-)






            share|improve this answer





















            • 4





              When using javac, it would be better to specify an output directory. find -name "*.java" > sources.txt && javac -d bin @sources.txt. Otherwise *.class files are saved to the directory where sources are.

              – Maksim Dmitriev
              Aug 14 '14 at 7:45








            • 1





              Absolutely true. Although in my opinion, if someone has just started to play around with javac, the concept of CLASSPATH, how to run code with java, how to deal with packages, which should be the root folder for running, etc. are usually not clear. Thus I omitted the output dir. Anyway, thanks for the suggestion!

              – rlegendi
              Aug 14 '14 at 8:25






            • 3





              For mac users coming across this, the find command is: find . -name "*.java" > sources.txt (note the .)

              – MrDuk
              Sep 21 '15 at 6:35











            • @MrDuk What does adding the "." do? Is it for searching within the current directory?

              – Brady Sheehan
              Nov 21 '15 at 22:54











            • @BradySheehan it will start to search from the given path. "." means start from the current dictionary. Note that you have to specify a path for find (in OS X)

              – Kris
              Jan 15 '16 at 12:45
















            191














            I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).



            Using Javac



            If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that javac offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javac with the @ prefix.



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



            # Linux / MacOS
            $ find -name "*.java" > sources.txt
            $ javac @sources.txt

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




            • The advantage is that is is a quick and easy solution.


            • The drawback is that you have to regenerate the sources.txt file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.


            Using a build tool



            On the long run it is 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




            • The advantage is that you are using a standard build tool that is easy to extend.


            • The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.


            Using Maven



            Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.





            • It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.


            • The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).


            Using an IDE



            Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.



            They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the background so you can hunt down occasional errors like a ClassNotFoundException.



            One additional note



            For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple mvn eclipse:eclipse command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continuous integration server for example. Piece of cake :-)






            share|improve this answer





















            • 4





              When using javac, it would be better to specify an output directory. find -name "*.java" > sources.txt && javac -d bin @sources.txt. Otherwise *.class files are saved to the directory where sources are.

              – Maksim Dmitriev
              Aug 14 '14 at 7:45








            • 1





              Absolutely true. Although in my opinion, if someone has just started to play around with javac, the concept of CLASSPATH, how to run code with java, how to deal with packages, which should be the root folder for running, etc. are usually not clear. Thus I omitted the output dir. Anyway, thanks for the suggestion!

              – rlegendi
              Aug 14 '14 at 8:25






            • 3





              For mac users coming across this, the find command is: find . -name "*.java" > sources.txt (note the .)

              – MrDuk
              Sep 21 '15 at 6:35











            • @MrDuk What does adding the "." do? Is it for searching within the current directory?

              – Brady Sheehan
              Nov 21 '15 at 22:54











            • @BradySheehan it will start to search from the given path. "." means start from the current dictionary. Note that you have to specify a path for find (in OS X)

              – Kris
              Jan 15 '16 at 12:45














            191












            191








            191







            I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).



            Using Javac



            If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that javac offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javac with the @ prefix.



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



            # Linux / MacOS
            $ find -name "*.java" > sources.txt
            $ javac @sources.txt

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




            • The advantage is that is is a quick and easy solution.


            • The drawback is that you have to regenerate the sources.txt file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.


            Using a build tool



            On the long run it is 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




            • The advantage is that you are using a standard build tool that is easy to extend.


            • The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.


            Using Maven



            Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.





            • It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.


            • The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).


            Using an IDE



            Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.



            They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the background so you can hunt down occasional errors like a ClassNotFoundException.



            One additional note



            For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple mvn eclipse:eclipse command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continuous integration server for example. Piece of cake :-)






            share|improve this answer















            I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).



            Using Javac



            If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that javac offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javac with the @ prefix.



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



            # Linux / MacOS
            $ find -name "*.java" > sources.txt
            $ javac @sources.txt

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




            • The advantage is that is is a quick and easy solution.


            • The drawback is that you have to regenerate the sources.txt file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.


            Using a build tool



            On the long run it is 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




            • The advantage is that you are using a standard build tool that is easy to extend.


            • The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.


            Using Maven



            Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.





            • It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.


            • The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).


            Using an IDE



            Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.



            They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the background so you can hunt down occasional errors like a ClassNotFoundException.



            One additional note



            For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple mvn eclipse:eclipse command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continuous integration server for example. Piece of cake :-)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 26 '18 at 21:37

























            answered Jan 7 '12 at 12:02









            rlegendirlegendi

            8,23122944




            8,23122944








            • 4





              When using javac, it would be better to specify an output directory. find -name "*.java" > sources.txt && javac -d bin @sources.txt. Otherwise *.class files are saved to the directory where sources are.

              – Maksim Dmitriev
              Aug 14 '14 at 7:45








            • 1





              Absolutely true. Although in my opinion, if someone has just started to play around with javac, the concept of CLASSPATH, how to run code with java, how to deal with packages, which should be the root folder for running, etc. are usually not clear. Thus I omitted the output dir. Anyway, thanks for the suggestion!

              – rlegendi
              Aug 14 '14 at 8:25






            • 3





              For mac users coming across this, the find command is: find . -name "*.java" > sources.txt (note the .)

              – MrDuk
              Sep 21 '15 at 6:35











            • @MrDuk What does adding the "." do? Is it for searching within the current directory?

              – Brady Sheehan
              Nov 21 '15 at 22:54











            • @BradySheehan it will start to search from the given path. "." means start from the current dictionary. Note that you have to specify a path for find (in OS X)

              – Kris
              Jan 15 '16 at 12:45














            • 4





              When using javac, it would be better to specify an output directory. find -name "*.java" > sources.txt && javac -d bin @sources.txt. Otherwise *.class files are saved to the directory where sources are.

              – Maksim Dmitriev
              Aug 14 '14 at 7:45








            • 1





              Absolutely true. Although in my opinion, if someone has just started to play around with javac, the concept of CLASSPATH, how to run code with java, how to deal with packages, which should be the root folder for running, etc. are usually not clear. Thus I omitted the output dir. Anyway, thanks for the suggestion!

              – rlegendi
              Aug 14 '14 at 8:25






            • 3





              For mac users coming across this, the find command is: find . -name "*.java" > sources.txt (note the .)

              – MrDuk
              Sep 21 '15 at 6:35











            • @MrDuk What does adding the "." do? Is it for searching within the current directory?

              – Brady Sheehan
              Nov 21 '15 at 22:54











            • @BradySheehan it will start to search from the given path. "." means start from the current dictionary. Note that you have to specify a path for find (in OS X)

              – Kris
              Jan 15 '16 at 12:45








            4




            4





            When using javac, it would be better to specify an output directory. find -name "*.java" > sources.txt && javac -d bin @sources.txt. Otherwise *.class files are saved to the directory where sources are.

            – Maksim Dmitriev
            Aug 14 '14 at 7:45







            When using javac, it would be better to specify an output directory. find -name "*.java" > sources.txt && javac -d bin @sources.txt. Otherwise *.class files are saved to the directory where sources are.

            – Maksim Dmitriev
            Aug 14 '14 at 7:45






            1




            1





            Absolutely true. Although in my opinion, if someone has just started to play around with javac, the concept of CLASSPATH, how to run code with java, how to deal with packages, which should be the root folder for running, etc. are usually not clear. Thus I omitted the output dir. Anyway, thanks for the suggestion!

            – rlegendi
            Aug 14 '14 at 8:25





            Absolutely true. Although in my opinion, if someone has just started to play around with javac, the concept of CLASSPATH, how to run code with java, how to deal with packages, which should be the root folder for running, etc. are usually not clear. Thus I omitted the output dir. Anyway, thanks for the suggestion!

            – rlegendi
            Aug 14 '14 at 8:25




            3




            3





            For mac users coming across this, the find command is: find . -name "*.java" > sources.txt (note the .)

            – MrDuk
            Sep 21 '15 at 6:35





            For mac users coming across this, the find command is: find . -name "*.java" > sources.txt (note the .)

            – MrDuk
            Sep 21 '15 at 6:35













            @MrDuk What does adding the "." do? Is it for searching within the current directory?

            – Brady Sheehan
            Nov 21 '15 at 22:54





            @MrDuk What does adding the "." do? Is it for searching within the current directory?

            – Brady Sheehan
            Nov 21 '15 at 22:54













            @BradySheehan it will start to search from the given path. "." means start from the current dictionary. Note that you have to specify a path for find (in OS X)

            – Kris
            Jan 15 '16 at 12:45





            @BradySheehan it will start to search from the given path. "." means start from the current dictionary. Note that you have to specify a path for find (in OS X)

            – Kris
            Jan 15 '16 at 12:45













            39














            find . -name "*.java" -print | xargs javac 


            Kinda brutal, but works like hell. (Use only on small programs, it's absolutely not efficient)






            share|improve this answer





















            • 1





              If you use this consider find ... -print0 and xargs -0 ... instead to not break on spaces in filenames

              – sapht
              Feb 7 '18 at 19:01
















            39














            find . -name "*.java" -print | xargs javac 


            Kinda brutal, but works like hell. (Use only on small programs, it's absolutely not efficient)






            share|improve this answer





















            • 1





              If you use this consider find ... -print0 and xargs -0 ... instead to not break on spaces in filenames

              – sapht
              Feb 7 '18 at 19:01














            39












            39








            39







            find . -name "*.java" -print | xargs javac 


            Kinda brutal, but works like hell. (Use only on small programs, it's absolutely not efficient)






            share|improve this answer















            find . -name "*.java" -print | xargs javac 


            Kinda brutal, but works like hell. (Use only on small programs, it's absolutely not efficient)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 30 '11 at 20:00









            John Conde

            187k80376430




            187k80376430










            answered Nov 30 '11 at 20:00









            Matthieu RieglerMatthieu Riegler

            10.2k1066103




            10.2k1066103








            • 1





              If you use this consider find ... -print0 and xargs -0 ... instead to not break on spaces in filenames

              – sapht
              Feb 7 '18 at 19:01














            • 1





              If you use this consider find ... -print0 and xargs -0 ... instead to not break on spaces in filenames

              – sapht
              Feb 7 '18 at 19:01








            1




            1





            If you use this consider find ... -print0 and xargs -0 ... instead to not break on spaces in filenames

            – sapht
            Feb 7 '18 at 19:01





            If you use this consider find ... -print0 and xargs -0 ... instead to not break on spaces in filenames

            – sapht
            Feb 7 '18 at 19:01











            27














            If your shell supports it, would something like this work ?



            javac com/**/*.java 


            If your shell does not support **, then maybe



            javac com/*/*/*.java


            works (for all packages with 3 components - adapt for more or less).






            share|improve this answer





















            • 2





              This works and is actually more of a direct answer to the question.

              – Daniel Macias
              Mar 20 '14 at 17:01











            • I have attempted to use this in command prompt on windows 10. Can someone confirm if it works on windows 10 or if I am doing it wrong please

              – Dan
              Dec 29 '15 at 12:11
















            27














            If your shell supports it, would something like this work ?



            javac com/**/*.java 


            If your shell does not support **, then maybe



            javac com/*/*/*.java


            works (for all packages with 3 components - adapt for more or less).






            share|improve this answer





















            • 2





              This works and is actually more of a direct answer to the question.

              – Daniel Macias
              Mar 20 '14 at 17:01











            • I have attempted to use this in command prompt on windows 10. Can someone confirm if it works on windows 10 or if I am doing it wrong please

              – Dan
              Dec 29 '15 at 12:11














            27












            27








            27







            If your shell supports it, would something like this work ?



            javac com/**/*.java 


            If your shell does not support **, then maybe



            javac com/*/*/*.java


            works (for all packages with 3 components - adapt for more or less).






            share|improve this answer















            If your shell supports it, would something like this work ?



            javac com/**/*.java 


            If your shell does not support **, then maybe



            javac com/*/*/*.java


            works (for all packages with 3 components - adapt for more or less).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 8 '11 at 14:41









            Paŭlo Ebermann

            61.2k12125183




            61.2k12125183










            answered Jul 8 '11 at 10:31









            phtrivierphtrivier

            9,39233266




            9,39233266








            • 2





              This works and is actually more of a direct answer to the question.

              – Daniel Macias
              Mar 20 '14 at 17:01











            • I have attempted to use this in command prompt on windows 10. Can someone confirm if it works on windows 10 or if I am doing it wrong please

              – Dan
              Dec 29 '15 at 12:11














            • 2





              This works and is actually more of a direct answer to the question.

              – Daniel Macias
              Mar 20 '14 at 17:01











            • I have attempted to use this in command prompt on windows 10. Can someone confirm if it works on windows 10 or if I am doing it wrong please

              – Dan
              Dec 29 '15 at 12:11








            2




            2





            This works and is actually more of a direct answer to the question.

            – Daniel Macias
            Mar 20 '14 at 17:01





            This works and is actually more of a direct answer to the question.

            – Daniel Macias
            Mar 20 '14 at 17:01













            I have attempted to use this in command prompt on windows 10. Can someone confirm if it works on windows 10 or if I am doing it wrong please

            – Dan
            Dec 29 '15 at 12:11





            I have attempted to use this in command prompt on windows 10. Can someone confirm if it works on windows 10 or if I am doing it wrong please

            – Dan
            Dec 29 '15 at 12:11











            21














            In the usual case where you want to compile your whole project you can simply supply javac with your main class and let it compile all required dependencies:



            javac -sourcepath . path/to/Main.java






            share|improve this answer
























            • Very simple method, does not rely on extra files

              – linquize
              Dec 27 '15 at 2:20











            • This is the best and the simplest one for people who don't have much time to learn Ant (like me)

              – Hamza Abbad
              Mar 27 '16 at 16:15











            • It's lazy unfortunately. If you don't touch Main.java, which you probably wouldn't just after creating your second file, nothing else gets compiler.

              – Tom Hawtin - tackline
              Oct 1 '18 at 22:23
















            21














            In the usual case where you want to compile your whole project you can simply supply javac with your main class and let it compile all required dependencies:



            javac -sourcepath . path/to/Main.java






            share|improve this answer
























            • Very simple method, does not rely on extra files

              – linquize
              Dec 27 '15 at 2:20











            • This is the best and the simplest one for people who don't have much time to learn Ant (like me)

              – Hamza Abbad
              Mar 27 '16 at 16:15











            • It's lazy unfortunately. If you don't touch Main.java, which you probably wouldn't just after creating your second file, nothing else gets compiler.

              – Tom Hawtin - tackline
              Oct 1 '18 at 22:23














            21












            21








            21







            In the usual case where you want to compile your whole project you can simply supply javac with your main class and let it compile all required dependencies:



            javac -sourcepath . path/to/Main.java






            share|improve this answer













            In the usual case where you want to compile your whole project you can simply supply javac with your main class and let it compile all required dependencies:



            javac -sourcepath . path/to/Main.java







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 2 '15 at 11:32









            freakerfreaker

            465511




            465511













            • Very simple method, does not rely on extra files

              – linquize
              Dec 27 '15 at 2:20











            • This is the best and the simplest one for people who don't have much time to learn Ant (like me)

              – Hamza Abbad
              Mar 27 '16 at 16:15











            • It's lazy unfortunately. If you don't touch Main.java, which you probably wouldn't just after creating your second file, nothing else gets compiler.

              – Tom Hawtin - tackline
              Oct 1 '18 at 22:23



















            • Very simple method, does not rely on extra files

              – linquize
              Dec 27 '15 at 2:20











            • This is the best and the simplest one for people who don't have much time to learn Ant (like me)

              – Hamza Abbad
              Mar 27 '16 at 16:15











            • It's lazy unfortunately. If you don't touch Main.java, which you probably wouldn't just after creating your second file, nothing else gets compiler.

              – Tom Hawtin - tackline
              Oct 1 '18 at 22:23

















            Very simple method, does not rely on extra files

            – linquize
            Dec 27 '15 at 2:20





            Very simple method, does not rely on extra files

            – linquize
            Dec 27 '15 at 2:20













            This is the best and the simplest one for people who don't have much time to learn Ant (like me)

            – Hamza Abbad
            Mar 27 '16 at 16:15





            This is the best and the simplest one for people who don't have much time to learn Ant (like me)

            – Hamza Abbad
            Mar 27 '16 at 16:15













            It's lazy unfortunately. If you don't touch Main.java, which you probably wouldn't just after creating your second file, nothing else gets compiler.

            – Tom Hawtin - tackline
            Oct 1 '18 at 22:23





            It's lazy unfortunately. If you don't touch Main.java, which you probably wouldn't just after creating your second file, nothing else gets compiler.

            – Tom Hawtin - tackline
            Oct 1 '18 at 22:23











            3














            I would advice you to learn using ant, which is very-well suited for this task and is very easy to grasp and well documented.



            You would just have to define a target like this in the build.xml file:



            <target name="compile">
            <javac srcdir="your/source/directory"
            destdir="your/output/directory"
            classpath="xyz.jar" />
            </target>





            share|improve this answer




























              3














              I would advice you to learn using ant, which is very-well suited for this task and is very easy to grasp and well documented.



              You would just have to define a target like this in the build.xml file:



              <target name="compile">
              <javac srcdir="your/source/directory"
              destdir="your/output/directory"
              classpath="xyz.jar" />
              </target>





              share|improve this answer


























                3












                3








                3







                I would advice you to learn using ant, which is very-well suited for this task and is very easy to grasp and well documented.



                You would just have to define a target like this in the build.xml file:



                <target name="compile">
                <javac srcdir="your/source/directory"
                destdir="your/output/directory"
                classpath="xyz.jar" />
                </target>





                share|improve this answer













                I would advice you to learn using ant, which is very-well suited for this task and is very easy to grasp and well documented.



                You would just have to define a target like this in the build.xml file:



                <target name="compile">
                <javac srcdir="your/source/directory"
                destdir="your/output/directory"
                classpath="xyz.jar" />
                </target>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 8 '11 at 10:32









                JB NizetJB Nizet

                549k598971023




                549k598971023























                    2














                    I'm just using make with a simple makefile that looks like this:



                    JAVAC = javac -Xlint:unchecked
                    sources = $(shell find . -type f -name '*.java')
                    classes = $(sources:.java=.class)

                    all : $(classes)

                    clean :
                    rm -f $(classes)

                    %.class : %.java
                    $(JAVAC) $<


                    It compiles the sources one at a time and only recompiles if necessary.






                    share|improve this answer




























                      2














                      I'm just using make with a simple makefile that looks like this:



                      JAVAC = javac -Xlint:unchecked
                      sources = $(shell find . -type f -name '*.java')
                      classes = $(sources:.java=.class)

                      all : $(classes)

                      clean :
                      rm -f $(classes)

                      %.class : %.java
                      $(JAVAC) $<


                      It compiles the sources one at a time and only recompiles if necessary.






                      share|improve this answer


























                        2












                        2








                        2







                        I'm just using make with a simple makefile that looks like this:



                        JAVAC = javac -Xlint:unchecked
                        sources = $(shell find . -type f -name '*.java')
                        classes = $(sources:.java=.class)

                        all : $(classes)

                        clean :
                        rm -f $(classes)

                        %.class : %.java
                        $(JAVAC) $<


                        It compiles the sources one at a time and only recompiles if necessary.






                        share|improve this answer













                        I'm just using make with a simple makefile that looks like this:



                        JAVAC = javac -Xlint:unchecked
                        sources = $(shell find . -type f -name '*.java')
                        classes = $(sources:.java=.class)

                        all : $(classes)

                        clean :
                        rm -f $(classes)

                        %.class : %.java
                        $(JAVAC) $<


                        It compiles the sources one at a time and only recompiles if necessary.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Apr 3 '15 at 18:52









                        Edward DoolittleEdward Doolittle

                        3,4472920




                        3,4472920























                            1














                            javac command does not follow a recursive compilation process, so you have either specify each directory when running command, or provide a text file with directories you want to include:



                            javac -classpath "${CLASSPATH}" @java_sources.txt





                            share|improve this answer






























                              1














                              javac command does not follow a recursive compilation process, so you have either specify each directory when running command, or provide a text file with directories you want to include:



                              javac -classpath "${CLASSPATH}" @java_sources.txt





                              share|improve this answer




























                                1












                                1








                                1







                                javac command does not follow a recursive compilation process, so you have either specify each directory when running command, or provide a text file with directories you want to include:



                                javac -classpath "${CLASSPATH}" @java_sources.txt





                                share|improve this answer















                                javac command does not follow a recursive compilation process, so you have either specify each directory when running command, or provide a text file with directories you want to include:



                                javac -classpath "${CLASSPATH}" @java_sources.txt






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Feb 18 '15 at 4:03









                                Eric Leschinski

                                90.3k40331281




                                90.3k40331281










                                answered Jan 8 '14 at 15:30









                                gvalennciagvalenncia

                                12117




                                12117























                                    1














                                    javac -cp "jar_path/*" $(find . -name '*.java')



                                    (I prefer not to use xargs because it can split them up and run javac multiple times, each with a subset of java files, some of which may import other ones not specified on the same javac command line)



                                    If you have an App.java entrypoint, freaker's way with -sourcepath is best. It compiles every other java file it needs, following the import-dependencies. eg:



                                    javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java



                                    You can also specify a target class-file dir: -d target/.






                                    share|improve this answer




























                                      1














                                      javac -cp "jar_path/*" $(find . -name '*.java')



                                      (I prefer not to use xargs because it can split them up and run javac multiple times, each with a subset of java files, some of which may import other ones not specified on the same javac command line)



                                      If you have an App.java entrypoint, freaker's way with -sourcepath is best. It compiles every other java file it needs, following the import-dependencies. eg:



                                      javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java



                                      You can also specify a target class-file dir: -d target/.






                                      share|improve this answer


























                                        1












                                        1








                                        1







                                        javac -cp "jar_path/*" $(find . -name '*.java')



                                        (I prefer not to use xargs because it can split them up and run javac multiple times, each with a subset of java files, some of which may import other ones not specified on the same javac command line)



                                        If you have an App.java entrypoint, freaker's way with -sourcepath is best. It compiles every other java file it needs, following the import-dependencies. eg:



                                        javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java



                                        You can also specify a target class-file dir: -d target/.






                                        share|improve this answer













                                        javac -cp "jar_path/*" $(find . -name '*.java')



                                        (I prefer not to use xargs because it can split them up and run javac multiple times, each with a subset of java files, some of which may import other ones not specified on the same javac command line)



                                        If you have an App.java entrypoint, freaker's way with -sourcepath is best. It compiles every other java file it needs, following the import-dependencies. eg:



                                        javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java



                                        You can also specify a target class-file dir: -d target/.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Dec 4 '18 at 23:24









                                        Curtis YallopCurtis Yallop

                                        4,31332422




                                        4,31332422























                                            0














                                            I've been using this in an Xcode JNI project to recursively build my test classes:



                                            find ${PROJECT_DIR} -name "*.java" -print | xargs javac -g -classpath ${BUILT_PRODUCTS_DIR} -d ${BUILT_PRODUCTS_DIR}





                                            share|improve this answer






























                                              0














                                              I've been using this in an Xcode JNI project to recursively build my test classes:



                                              find ${PROJECT_DIR} -name "*.java" -print | xargs javac -g -classpath ${BUILT_PRODUCTS_DIR} -d ${BUILT_PRODUCTS_DIR}





                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                I've been using this in an Xcode JNI project to recursively build my test classes:



                                                find ${PROJECT_DIR} -name "*.java" -print | xargs javac -g -classpath ${BUILT_PRODUCTS_DIR} -d ${BUILT_PRODUCTS_DIR}





                                                share|improve this answer















                                                I've been using this in an Xcode JNI project to recursively build my test classes:



                                                find ${PROJECT_DIR} -name "*.java" -print | xargs javac -g -classpath ${BUILT_PRODUCTS_DIR} -d ${BUILT_PRODUCTS_DIR}






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Dec 4 '14 at 22:09









                                                Honza

                                                761913




                                                761913










                                                answered Mar 27 '14 at 7:44









                                                bishopthombishopthom

                                                41144




                                                41144






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6623161%2fjavac-option-to-compile-all-java-files-under-a-given-directory-recursively%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    To store a contact into the json file from server.js file using a class in NodeJS

                                                    Redirect URL with Chrome Remote Debugging Android Devices

                                                    Dieringhausen