Java - Jar (Startable|Runnable|Executable)

> Procedural Languages > Java

1 - 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


3 - Management

3.1 - Execution

You start it with the following command:

java -jar myStartableJar.jar

3.2 - Creation

3.2.1 - Eclipse

and you get also an ant script

3.2.2 - Ant

ant script

<?xml version="1.0" encoding="UTF-8"?>
<project name="My Project" default="dist" basedir=".">
		Build a startable Jar
	<!-- 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 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 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}/${}-${DSTAMP}.jar" filesetmanifest="mergewithoutmain">
				<attribute name="Main-Class" value="com.gerardnico.myMainClass" />
				<attribute name="Class-Path" value="." />
			<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" />
	<target name="clean" description="clean up">
		<!-- Delete the ${build} and ${dist} directory trees -->
		<delete dir="${build.class.dir}" />
		<delete dir="${build.jar.dir}" />

4 - Support

4.1 - 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