Question

With all the help from you i was able to finish my first project in java. Now i want to create a jar and run the application (Java project - it is a plain console application which is having another project(console application) as a dependence) from jar.

I created a jar with eclipse by right click - export - created a jar. When i try to run this jar from my cmd, I have an error (below is the error I am geting)

no main manifest attribute, in AAA.jar

I Googled the error - most of them subjected to create the Manifest file. I created a manifest file like below in the project equal to src level

Manifest-Version: 1.0
Main-Class: com.Pacakename.mainclass
Class-Path: log4j-1.2.16.jar db2jcc.jar db2jcc_license_cu.jar

Then J tried run the jar again but this time it says no main method where i have a main method in the class

Please some one please explain a clear step's for creating the Manifest(it really help's me if you show me the folder structure of the place where we have Manifest file)

Was it helpful?

Solution

Lets assume you have following directory structure:

MyJavaProject
   |-src 
      |- com
          |- example
               |- Main.java

To compile such project in cmd line, (no external dependencies) you need to invoke commands

$ cd MyJavaProject
$ mkdir bin         //to separate *.class file from source files
$ javac -d bin src\com\example\Main.java

This will create Main.class file in bin directory. To pack it to *.jar file, you can: 1) create jar with binary files and specify Main class in cmd 2) create Manifes and embbed it to jar (I will focus on this one)

You should create META-INF directory under src and inside it create MANIFEST.mf file

Your manifest should look like this:

Manifest-Version: 1.0
Created-By: <Your info>
Main-Class: com.example.Main

Remember to add empty line at end on Manifest!!

In this case, you specify Manifest-Version attribute, Created-By attribute, and fully qualified name of main class to run in Main-Class attribute

To create Jar with this Manifest file and you binary file, invoke commands

$ cd bin
$ jar cfm MyJavaProject.jar ..\src\META-INF\MANIFEST.MF .

this will create new jar called MyJavaProject.jar and use your manifest

If you project depends on external classas or jar, add them to your classpath when compiling (-cp option) and add another line in Manifest

ClassPath: path/to/dependent/jars/jar.jar

Recompile it and create new jar and enjoy your Java quest :)

For more info on Manifest please see: docs

PS: Working with jars,amnifest from cmd line might seem ugly, but it can teach you some ava-like concepts. If you want to skip it, please consider using Apache Maven or Apache Ant

OTHER TIPS

If you are using maven I can recommend you to use the shade plugin to produce runnable jars with all dependencies (and manifest :-)) on-board.

Include this in your pom.xml:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <!-- your main class: -->
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

And then execute mvn clean package shade:shade to build a jar that you can run from the command line with java -jar jarfilename.jar.

If you are using eclipse you can use "Export" --> Runnable jar to achieve your goals. In that case you should have a valid run configuration for your project since eclipse uses this run configuration as a kind of blueprint for the runnable jar.

In this case eclipse will build the manifest for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top