views:

46

answers:

2

I have simple java app that prints `hello world!' on console. It is packed in app.jar. Jar structure:

main/Hello.class - my main class with singe println method

META-INF/MANIFEST.MF

Manifest file contains following:

Manifest-Version: 1.0
Main-Class: main.Hello

Everything goes fine.

But when you have a dependency than troubles begin. I'm not sure but think in this case you have to put all libs to jar file. If I put them in META-INF/lib I must specify "Class-Path" in manifest. How "Class-Path" will look?

P.S There are some resembling questions but I haven't found appropriate answer.

+1  A: 

You don't have to specify anything special if you unpack the libraries and integrate them into your project. If you do this, you should have a "main" folder, and if you have org.apache.foo as an external library, you'll also have an "org" folder at the top level.

Borealid
That's extremely rough but sure it works. I want to include library jars in resulting jar in META-INF/lib folder so question is still open.
Jeriho
doesn't sound like good practice to me. I think the OP is right that it's better to put jars inside your jar and add them to the classpath.
Sanjay Manohar
+1  A: 

I tend to use an ANT build script to package my application and all necessary jar files. I find this makes life much easier once you've got it working properly.

build.xml file looks something like:

<project default="create_run_jar" name="Create Runnable Jar for MyProject">
    <!--ANT 1.7 is required -->
    <target name="create_run_jar">
        <jar destfile="my-runnable-jar.jar">
            <manifest>
                <attribute name="Main-Class" value="my.MainClass"/>
                <attribute name="Class-Path" value="."/>
            </manifest>
            <fileset dir="E:/path/to/my/project/bin"/>
            <fileset dir="E:/path/to/my/project/classes"/>
            <zipfileset src="E:/path/to/library/some-library.jar"/>
        </jar>
    </target>
</project>

Note that if you use Eclipse, you can simplly do File / Export... / Runnable jar file and it will do everything for you (including generating the ANT build.xml).

mikera