VOOZH about

URL: https://www.javacodegeeks.com/2018/07/apache-ant-supports-single-file-source-programs.html

⇱ Apache Ant 1.9.13 and 1.10.5 released - Supports Java 11 single-file source programs - Java Code Geeks


We just released 1.9.13 and 1.10.5 versions of Apache Ant. As usual, you can download it from the Ant project download page.

Both these versions are mainly bug fix releases. The 1.10.5 version however has a new enhancement to the “java” task. As I blogged previously – Java 11 introduces a new feature where you can execute single-file Java programs without having to explicitly compile them first. Ant 1.10.5 release now supports this feature through a new “sourcefile” attribute in the “java” task. More about it can be found the manual of that task.

A simple usage example of this new feature of the “java” task is as follows:

<project default="launch-java" name="Java 11 - launch single-file source program">

 <target name="launch-java"
 description="Simple example of single-file source program execution,
 introduced in Java 11">

 <!-- Make sure Java 11 version is being used -->
 <condition property="java11">
 <javaversion atleast="11"/>
 </condition> 
 <fail unless="java11">Java 11 runtime version is necessary to run this example</fail> 

 <mkdir dir="${basedir}/javasource"/>
 <!-- Write out simple Java code into a file -->
 <echo file="${basedir}/javasource/HelloWorld.java">
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.io.BufferedWriter;
 public class HelloWorld {
 public static void main(String[] args) throws Exception {
 System.out.println("Hello world, " + args[0] + "!");
 }
 }
 </echo>
 <!-- launch the Java source file, using the "sourcefile" attribute -->
 <java sourcefile="${basedir}/javasource/HelloWorld.java" fork="true" failonerror="true" logerror="true">
 <arg value="Java 11"/>
 </java>
 </target>
</project>

As you’ll notice, the build file uses the “java” task to set the “sourcefile” attribute to point to a Java source file. The rest of the usage details of the “java” task, including passing arguments to the program, continue to remain the same as before.

When you run “ant” on this build file, you should see the following output:

 Hello world, Java 11!

Of course, you will need to use a Java 11 binary to run this against. You can get the early accessible Java 11 binary from here.

Published on Java Code Geeks with permission by Jaikiran Pai, partner at our JCG program. See the original article here: Apache Ant 1.9.13 and 1.10.5 released – Supports Java 11 single-file source programs

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Jaikiran Pai
Jaikiran Pai
July 17th, 2018Last Updated: July 17th, 2018
0 288 1 minute read

Jaikiran Pai

Jaikiran works for Red Hat and is a developer in the JBoss application server development team. He's one of the authors of "JBoss AS Getting Started" DZone RefCard. When he's not doing anything JBoss related, you can usually find him at JavaRanch, in his role as a Sheriff(Moderator). He blogs at jaitechwriteups
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz