-2

I have a Java program that uses an external library, whose location is specified through the classpath. I would now like to make the Java program into a standalone jar file (so I can use my IDE for other things whilst the program is running).

How do I turn my existing .java file into an executable jar file?

I am able to make a jar file that includes the class file, manifest file, and the jar file of the library (that was specified in the classpath), but that still appears to be wrong because I get class not found errors.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Exastify
  • 9
  • 1
  • 3
  • 1
    Details, details, details (in other words, we need them from you to be able to help) – KevinDTimm Feb 27 '12 at 13:10
  • possible duplicate of [Classpath including JAR within a JAR](http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar) – Andreas Dolk Feb 27 '12 at 13:15
  • .java file is just the source code. it cannot be executed. – qrtt1 Feb 27 '12 at 13:16
  • *"I am able to make a jar file that includes the class file, manifest file, and the jar file of the library.."* ***Don't*** do that. The library should be put in a location that is accessible to the Jar(1) and listed in the manifest of the main Jar by relative path. 1) One easy place is the same directory as the main Jar. The 'relative path' then just becomes 'the name'. – Andrew Thompson Feb 27 '12 at 13:21

2 Answers2

0

Ant script for you. What you missed was the classpath generation as a string for the jar task.

<target name="all">
    <property name="dir" value="yourProjectDir" />
    <property name="name" value="$yourProjectName" />

    <!-- clean -->
    <delete dir="temp/" />
    <mkdir dir="temp/bin/" />

    <!-- prepare libs -->
    <copy todir="temp/libs/"><fileset dir="${dir}/lbs/" /></copy>

    <!-- prepare classpath -->
    <pathconvert property="classpath" pathsep=" ">
        <path><fileset dir="temp/libs/" /></path>
        <chainedmapper><flattenmapper /><globmapper from="*" to="libs/*" /></chainedmapper>
    </pathconvert>

    <!-- compile -->
    <javac destdir="temp/bin/" srcdir="${dir}/src/" target="1.6" source="1.6" includeAntRuntime="false">
        <classpath>
            <pathelement location="temp/bin/" />
            <fileset dir="temp/libs/" />
        </classpath>
    </javac>

    <!-- jar -->
    <jar destfile="temp/${name}.jar" basedir="temp/bin/">
        <manifest>
            <attribute name="Main-Class" value="Main" />
            <attribute name="Class-Path" value="${classpath}" />
        </manifest>
    </jar>

    <!-- zip jar + libs -->
    <zip destfile="${name}-${version}.zip">
        <fileset dir="temp" includes="${name}.jar, libs/" />
    </zip>
</target>
Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
0

The following is the steps, how run stand alone App from command prompt. 1. Create a sample java file then save in particular location(like d:\sample\Hello.java. 2. open command prompt compile that java class, then create jar like Hello.jar file 3. then set classpath in Environment file(like D:\sample\Hello.jar ; 4. Now run your java class , it will work(d:sample>java Hello

java_dev
  • 125
  • 2
  • 3
  • 13