Java - Jar (Startable|Runnable|Executable)

About

A (Startable|Runnable|Executable) jar-file is an jar archive of compiled classes that contains a manifest-file that point to the (start|main) class

Management

Execution

You start it with the following command:

java -jar myStartableJar.jar

Creation

Eclipse

and you get also an ant script

Ant

ant script

<?xml version="1.0" encoding="UTF-8"?>
<project name="My Project" default="dist" basedir=".">

	<description>
		Build a startable Jar
	</description>

	<!-- set global properties for this build -->
	<property name="src" location="src" />
	<property name="build.class.dir" location="classes" />
	<property name="build.jar.dir" location="dist" />

	<target name="init">

		<!-- Create the time stamp -->
		<tstamp />
		<!-- Create the build directory structure used by compile -->
		<mkdir dir="${build.class.dir}" />
		<mkdir dir="${build.jar.dir}" />

	</target>

	<target name="compile" depends="init" description="compile the source ">

		<!-- Compile the java code from ${src} into ${build.class.dir} -->
		<javac srcdir="${src}" destdir="${build.class.dir}" />

	</target>

	<target name="dist" depends="compile" description="generate the distribution">

		<!-- Create the distribution directory -->
		<mkdir dir="${build.jar.dir}" />

		<!-- Put everything in ${build.class.dir} into the MyProject-${DSTAMP}.jar file -->
		<jar destfile="${build.jar.dir}/${ant.project.name}-${DSTAMP}.jar" filesetmanifest="mergewithoutmain">
			<manifest>
				<attribute name="Main-Class" value="com.gerardnico.myMainClass" />
				<attribute name="Class-Path" value="." />
			</manifest>
			<fileset dir="${build.class.dir}" />
                        <!-- External jar if needed -->
			<zipfileset excludes="META-INF/*.SF" src="lib/javamail-1.4.5/mail.jar" />
			<zipfileset excludes="META-INF/*.SF" src="lib/jaf-1.1.1/activation.jar" />
		</jar>
	</target>

	<target name="clean" description="clean up">
		<!-- Delete the ${build} and ${dist} directory trees -->
		<delete dir="${build.class.dir}" />
		<delete dir="${build.jar.dir}" />
	</target>

</project>

Support

Error: Could not find or load main class myJar.jar

Error: Could not find or load main class myJar.jar

To be able to start a executable jar, use this command:

java -jar myJar.jar

and not

java myJar.jar

Powered by ComboStrap