Four steps to a running application

來源:互聯網
上載者:User

package oata;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

After finishing the java-only step we have to think about our build process. We have to compile our code, otherwise we couldn't start the program. Oh - "start" - yes, we could provide a target for that. We should package our application. Now it's only one class - but if you want to provide a download, no one would download several hundreds files ... (think about a complex Swing GUI - so let us create a jar file. A startable jar file would be nice ... And it's a good practise to have a "clean" target, which deletes all the generated stuff. Many failures could be solved just by a "clean build".

By default Ant uses build.xml as the name for a buildfile, so our .\build.xml would be:

<project><target name="clean"><delete dir="build"/></target><target name="compile"><mkdir dir="build/classes"/><javac srcdir="src" destdir="build/classes"/></target><target name="jar"><mkdir dir="build/jar"/><jar destfile="build/jar/HelloWorld.jar" basedir="build/classes"><manifest><attribute name="Main-Class" value="oata.HelloWorld"/></manifest></jar></target><target name="run"><java jar="build/jar/HelloWorld.jar" fork="true"/></target></project>

Now you can compile, package and run the application via

ant compileant jarant run

Or shorter with

ant compile jar run

While having a look at the buildfile, we will see some similar steps between Ant and the java-only commands:

java-only Ant
md build\classes            javac            -sourcepath src            -d build\classes            src\oata\HelloWorld.java            echo Main-Class: oata.HelloWorld>mf            md build\jar            jar cfm            build\jar\HelloWorld.jar            mf            -C build\classes            .            java -jar build\jar\HelloWorld.jar            
<mkdir dir="build/classes"/>            <javac            srcdir="src"            destdir="build/classes"/>            <!-- automatically detected -->            <!-- obsolete; done via manifest tag -->            <mkdir dir="build/jar"/>            <jar            destfile="build/jar/HelloWorld.jar"            basedir="build/classes">            <manifest>            <attribute name="Main-Class" value="oata.HelloWorld"/>            </manifest>            </jar>            <java jar="build/jar/HelloWorld.jar" fork="true"/>            

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.