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?
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
javacoffers: the classnames to compile can be specified in a file. You simply have to pass the name of the file tojavacwith the@prefix.If you can create a list of all the
*.javafiles in your project, it’s easy:sources.txtfile 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.xmlfile that describes how to build the software:you can compile the whole software by running the following command:
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.
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:eclipsecommand). 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 🙂