简体   繁体   中英

How to create an executable jar file from terminal

How does one properly build a java .jar file out of java source code. After some research, it seems the build tool ant is among those considered a best practice for configuring the manor in which the .java source files are built into .class files and ultimately stored in a .jar file.

For my project folder, I essentially put my .java source code in src/classes/HelloWorld.java , I put a build.xml file in the root directory.

I seem to have everything working except for a directive in the build.xml file for specifying the program's entry point. As it stands now, it leads to:

<attribute name="Main-Class" value="com.tutorialspoint.util.FaxUtil"/> 

But I'd like it to lead to the main method in my src/HelloWorld.java file.

A more complete (but basic) tutorial for using ant is available at ant's website .

First off, you'll need a development environment setup with both javac for compiling .java files and also ant for scripting out build processes. For some operating systems it's as simple as running a command such as $ sudo apt-get install ant openjdk-7-jdk but others will want to search for something more specific for them.

Your directory structure should look something like this:

.
├── build.xml
├── README.md
├── src
    └── hello
        └── HelloWorld.java

Your Hello World source code should look like this (in HelloWorld.java)

// This line is important and tells java that this code is a package of 
// code named 'hello'
package hello; 

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

An adapted build.xml file would like this (though contains unused elements):

<?xml version="1.0"?>
<project basedir="." default="build">
  <property name="src.dir" value="src"/>
  <property name="web.dir" value="war"/>
  <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
  <property name="name" value="hello"/>

  <path id="master-classpath">
    <fileset dir="${web.dir}/WEB-INF/lib">
      <include name="*.jar"/>
    </fileset>
    <pathelement path="${build.dir}"/>
  </path>

  <target name="build" description="Compile source tree java files">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${web.dir}/WEB-INF/lib"/>
    <javac includeantruntime="false" destdir="${build.dir}" source="1.5" target="1.5">
      <src path="${src.dir}"/>
      <classpath refid="master-classpath"/>
    </javac>
  </target>

  <target name="clean" description="Clean output directories">
    <delete dir="war"/>
  </target>

  <target name="build-jar" depends="build">
    <jar destfile="${web.dir}/lib/hello.jar"
         basedir="${build.dir}"
         includes="**"
         excludes="**/Test.class">

      <manifest>
        <attribute name="Main-Class" value="hello.HelloWorld"/>
      </manifest>

    </jar>
  </target>

</project>

From there, it's as simple as building the project from the command line

$  ant clean
$  ant build-jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM