You can run Java code using a build.xml file by writing an Apache Ant build script whose target compiles the sources with and then executes the compiled class with . Set the project’s basedir, declare a src folder and a build folder, give the task a classname and a classpath, and run ant run from the project root. The code runs on the JVM that Ant itself uses unless you point Ant at a different JDK. That one condition explains most surprises people hit: compile errors that mention a release version, UnsupportedClassVersionError at runtime, or a program that works in an IDE but fails under Ant.
Ant has been around since 2000 and remains a stable, dependency-light way to express “compile this, then run that.” It reads XML instead of Groovy or Kotlin, so there’s no build-language to learn, but also no shorthand. Everything below uses ordinary Ant tasks with no third-party libraries, and the examples target Java 17 and later, which is a reasonable default for new work in 2026. If your project already has a build.xml, skip to the section on running existing targets.
What build.xml is and when Ant is the right runner
A build.xml is the default build file for Apache Ant. Ant parses it, builds a graph of targets (named units of work), and executes whichever target you name on the command line, plus any targets it declares as dependencies. The file is plain XML with a root element, optional definitions, and one or more elements. There is no implicit convention like Maven’s src/main/java; you declare every directory yourself.
That’s the trade-off. Ant gives you exact control and almost no magic: you say where sources live, where classes go, which JARs are on the classpath, and which main class to launch. Maven and Gradle give you conventions and dependency resolution in exchange for adopting their model. Ant is a good fit when you want a small, readable script for a self-contained program, when you’re maintaining an older project that already uses Ant, or when the build steps are unusual enough that a conventional layout fights you. Maven’s exec:java or Gradle’s application plugin will be less typing for a standard project with external dependencies, because Ant doesn’t download libraries for you. You either vendor the JARs in a lib folder or add a resolver such as Ivy, which is outside this article’s scope.
Two Ant behaviors matter immediately. First, basedir defaults to the directory containing build.xml (more precisely, the directory Ant was started from, unless you set basedir explicitly), so relative paths resolve from the project root and you should always run ant from that root. Second, Ant’s task can run in-process using the JDK that launched Ant, or fork a separate compiler. In-process is faster; forked is more predictable when the compiler and runtime differ. The task likewise defaults to running in Ant’s own JVM, and you can set fork="true" to launch a separate process.
None of this requires a network connection or a particular IDE. A text editor, a JDK on PATH, and Ant installed are enough. Check that with ant -version and java -version; the JDK version printed by java -version is the runtime your program will use by default.
Prerequisites and a minimal project layout
Before writing the build file, confirm three things: Apache Ant is installed and on your PATH, a JDK (not just a JRE) is available so can find a compiler, and your project has a main class with a public static void main(String[] args) method. Ant needs the JDK because compilation is part of the build; a runtime-only Java installation will fail at the compile step with a message about no compiler being available.
A layout that keeps the build file obvious looks like this:
Keeping sources under src and output under build means a clean target can delete the output directory without touching your code. Avoid putting generated classes next to sources; it makes clean builds ambiguous and can hide stale .class files that mask compile errors.
Your Hello.java needs a package declaration matching its directory:
The package name matters because expects the fully qualified name, dots included, not a file path. com.example.Hello is correct; com/example/Hello or Hello.class will fail with a ClassNotFoundException even when compilation succeeded.
Writing build.xml: properties, compile target, run target
Start with a property block so paths and versions appear once. Then a compile target and a run target that depends on it. Here is a complete file that works with the layout above:
Four details in that file are worth understanding because they cause most first-run failures.
includeantruntime="false" keeps Ant’s own classes off the compile classpath. Ant warns about this setting when it’s omitted, and leaving it false avoids accidentally compiling against Ant internals you don’t intend to depend on.
release="17" compiles against the Java 17 API and produces class files for that version. The value must be supported by the JDK running Ant; a JDK 11 installation cannot honor release="17" and will fail. If you’re unsure what your JDK supports, omit release and let use its default, which is the running JDK’s version.
fork="true" on launches the program in a separate JVM. In-process execution is slightly faster, but a separate process isolates System.exit calls and JVM flags, and it makes it obvious that your program’s runtime is separate from Ant’s. When you fork, the classpath attribute or nested elements matter; in-process execution can also pick up Ant’s classpath, which occasionally masks a missing dependency.
passes a single command-line argument. Use for one argument and only when you deliberately want Ant to split the string on spaces. For file paths with spaces, prefer value.
Running the build and passing arguments
From the project root, ant with no target runs the default attribute, which is run here. ant run does the same explicitly, and ant compile stops after compilation, which is useful when you only want to check that sources build. ant clean deletes the output directory, and ant clean run performs a fresh compile and launch in one command.
Ant prints each target as it executes, so success looks like a line naming compile, a line naming run, a javac summary if anything was recompiled, then your program’s output, then BUILD SUCCESSFUL. If you see BUILD FAILED, Ant prints the failing target and the specific task and line, which is usually enough to locate the problem without a stack trace.
Passing arguments at invocation time requires a tiny amount of setup, because arguments are declared in the build file. The conventional pattern is a property with a default value that the command line can override:
Then ant run -Darg1=Alice prints Hello, Alice!. Command-line -D definitions override values defined in the file as long as the property hasn’t already been set earlier in execution, so define the default before the target that uses it. For variable-length argument lists, splits on whitespace, and is the correct choice for a path so it isn’t split.
Handling packages, resources, and external JARs
Once the program grows beyond one class, the same two directories still work, but two additions matter: resources and dependencies.
Resources (properties files, images, config) usually live alongside sources and need copying into the classes directory so they’re found on the classpath at runtime. Add a copy step to compile or a separate resources target that run depends on:
Because Ant doesn’t resolve Maven dependencies, third-party JARs go in lib and are placed on both the compile and runtime classpath. A element makes that explicit:
The is declared once and referenced by id, which prevents the compile and runtime classpaths from drifting apart. That drift is a classic bug: code compiles because a JAR is on the compile path but fails at runtime with NoClassDefFoundError because the runtime path omits it. If you see that error, compare the two classpath references first.
Diagnosing common failures
ClassNotFoundException even though compilation succeeded. Either the classname uses slashes or includes .class, or the directory holding the package root isn’t on the classpath. For com.example.Hello, ${classes.dir} must contain com/example/Hello.class.
UnsupportedClassVersionError at launch. The class files were compiled for a newer Java than the runtime launching them. This happens when uses a newer JDK than ’s forked process. Set release to match the target runtime, or point both tasks at the same JDK with jvm="${java.home}/bin/java" on or a fork reference to a specific executable.
javac errors about invalid release or missing compiler. Ant is running on a JRE or an older JDK. Run java -version; if it reports 8 or 11, either install a newer JDK and set JAVA_HOME, or lower the release value.
Arguments aren’t arriving. Check for splitting a single argument into several, and confirm the property override came before the target used it.
Nothing recompiles after a source change. Ant’s is incremental by default and can be fooled when timestamps are odd (restored backups, some file sync tools). ant clean run forces a full compile and is the right first check before suspecting anything else.
When a failure traces to a mismatch between the JDK Ant runs on and the JDK your project targets, that’s the stop condition for guessing. Confirm which JDK Ant picks up (ant -diagnostics prints it), set JAVA_HOME deliberately, and only then adjust release or fork settings. If your team uses a specific toolchain, ask for the supported JDK and Ant versions rather than tuning blindly.
Where Ant fits next to modern build tools
For a single-file or small project, the fifteen-line build file above is genuinely competitive: no downloads, no plugin graph, and ant run does exactly what it says. For projects with dozens of dependencies, test frameworks, and packaging steps, you’ll spend more time wiring classpaths than writing code, and Maven or Gradle will earn their overhead. A common middle path keeps Ant for legacy modules and introduces it only where a full build system is overkill.
One practical note on Ant’s continued maintenance: Ant has been in maintenance mode for years, receiving bug fixes rather than new features, and its documentation reflects that stable surface. That’s a reason to pin the Ant version in your team’s setup notes so a future upgrade doesn’t surprise a build. The tasks used above (mkdir, javac, java, copy, delete, path) are long-standing and unlikely to change.
If you need to check your work, the success criterion is narrow and verifiable: ant clean run from the project root prints BUILD SUCCESSFUL followed by your program’s output, with no warnings about includeantruntime, and ant -version reports the Ant release you expect. If all three hold, the build file is doing its job.