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

Advertising

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=".">
 
	<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>

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
Advertising
lang/java/jar_runnable.txt · Last modified: 2018/04/06 10:57 by gerardnico