Java: How can I compile an entire directory structure of code ?
You have to know all the directories, or be able to use wildcard ..
javac dir1/*.java dir2/*.java dir3/dir4/*.java dir3/dir5/*.java dir6/*src/*.java
javac option to compile all java files under a given directory recursively
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 :-)
Compiling Java files in sub-directories
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! :-)
How to compile and run with this folder structure
Lets say you have your code files in
[someDirectory]
|
+-[lib]
| |
| +-someLib.jar
| +-someOtherLib.jar
| +-...
|
+--[src]
|
+-[net]
|
+-[name]
|
+-[one]
|
+-[two]
|
+-[main]
|
+-Main.java <- code you want to compile
and execute
then if your console is in
someDirectory>
you can compile it with
someDirectory>javac -cp "lib\*" src\net\name\one\two\main\Main.java
but this will produce Main.class
file in same directory as Main.java
so to execute code from net.name.one.two.main.Main
class you would need to include src
directory to classPath because this directory contains package that Main
class is placed, so you would need to use command
someDirectory>java -cp "src;lib\*" net.name.one.two.main.Main
But it is good practice to separate class files from source files. To do this you can add -d
(directory) parameter while compiling pass directory which should have compiled class files. So first create your classes
directory at the same level as src
directory and execute
someDirectory>javac -d "classes" -cp "lib\*" src\net\name\one\two\main\Main.java
and now to be able to execute your Main
class instead creating confusion by src
directory to classPath you will have to add classes
directory which is more intuitive.
someDirectory>java -cp "classes;lib\*" net.name.one.two.main.Main.java
Compile source file to a different directory?
Yup, absolutely - use the -d
option to specify the output directory:
javac -d bin src/foo/bar/*.java
Note that the directory you specify is the root of the output structure; the relevant subdirectories will be created automatically to correspond to the package structure of your code.
See the javac documentation for more details.
In this case you'd need to issue one javac
command to compile the formulae, and another to compile the problems though - potentially using the formulae bin
directory as part of the classpath when compiling the problems.
(You might want to consider using a single source structure but different packages, mind you. You should also consider using an IDE to hide some of this complexity from you - it ends up getting tiresome doing all of this by hand, even though it's not actually hard.)
Compile java from multiple directories
It won't compile in one go.Actually,your src
directory doesn't contain any .java file,SO it won't be done in that way!
I am afraid that you'll have to do it by changing your path under src
folder to do the same.
You'll have to perform for each chess
,color
,game
,etc. directories to achieve the same.
So,change path at each run or go as advised in the comments to achieve compilation of all the java files.
OR
As proposed by David Ehrmann in your comment,you can do it by compiling in one go using javac $(find . -name '*.java')
.
It will compile all .java files under your present directory(src(.)
).
Compiling java files in all subfolders?
Use a build tool such as Ant or Maven. Both lets you manage dependencies in a much better way than can be accomplished using e.g. the find
UNIX tool. Both And and Maven also lets you define custom tasks to be performed in addition to compilation. Maven furthermore comes with conventions for managing external dependencies in remote repositories, as well as conventions for running unit tests and features that support continuous integration.
Even if you just need to compile your source files once in a while, you'll probably find that setting up a simple Ant build.xml
file can be a big time saver in the end.
Finally, most of the popular IDE and code editor applications has some kind of integration with Ant build scripts, so you can run all the Ant tasks from within the editor. NetBeans, Eclipse, IDEA and more also has built-in support for Maven.
Read this first, if you're new to Ant. Below is the example build file from the link:
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Once you're familiar with Ant, you'll find it easier to move to Maven.
How to create a directory and sub directory structure with java?
You can use File.mkdir() or File.mkdirs() to create a directory. Between the two, the latter method is more tolerant and will create all intermediate directories as needed. Also, since I see that you use "\\" in your question, I would suggest using File.separator for a portable path separator string.
Related Topics
Are "While(True)" Loops So Bad
Convert from List<Completablefuture> to Completablefuture<List>
Why Does String.Valueof(Null) Throw a Nullpointerexception
Differencebetween 'Java', 'Javaw', and 'Javaws'
Java Conditional Compilation: How to Prevent Code Chunks from Being Compiled
The Meaning of Noinitialcontextexception Error
Synchronizing on an Integer Value
Filter Values Only If Not Null Using Lambda in Java8
How to Redirect to Login Page When Session Is Expired in Java Web Application
Is Httpsession Thread Safe, Are Set/Get Attribute Thread Safe Operations
How Is Hashcode() Calculated in Java
Why Are the Rsa-Sha256 Signatures I Generate with Openssl and Java Different
Unit Testing a Class with a Java 8 Clock
Convert an Integer to an Array of Digits
Get the Index of a Pattern in a String Using Regex
Looking for an Example for Inserting Content into the Response Using a Servlet Filter
Swing on Osx: How to Trap Command-Q
What Is "The Inverse Side of the Association" in a Bidirectional JPA Onetomany/Manytoone Association