0

I am having two java projects, animal project and animal-test project. In animal project I am having an annotation interface called Animal like as shown below

Animal.java

package com.animal;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Animal{
    String name();
} 

Along with this I am having an interface MakeSound.java which has a method called makesSound().

Now I have build and created animal jar file including the dependencies, then I have imported animal.jar in my Second Project animal-test. I create a class called AnimalTest.java I now build and create a jar file animal-test.jar and when I run the jar file like java -jar animal-test.jar I got the following exception

no main manifest attribute, in animal-test.jar

Can anyone please help me on this.

Alex Man
  • 4,746
  • 17
  • 93
  • 178
  • Looks like the main class attribute is not present in the `META-INF/MANIFEST.MF` Can you check if the entry `Main-Class: com.animal.AnimalFinder` is present in the MANIFEST.MF file in the jar? – Jerin Joseph Jun 14 '17 at 16:44
  • How are you creating the jar? – Jerin Joseph Jun 14 '17 at 16:45
  • @JerinJoseph I am using` mvn clean install` – Alex Man Jun 14 '17 at 16:46
  • @JerinJoseph I am importing both jars, `animal-1.0.jar` and `animal-1.0-jar-with-dependencies.jar`. In `animal-1.0.jar` nothing is there but in `animal-1.0-jar-with-dependencies.jar` I have `Main-Class: com.animal.AnimalFinder` – Alex Man Jun 14 '17 at 16:49
  • You would need the manifest in the animal test jar too. Check my answer. – Jerin Joseph Jun 14 '17 at 16:51
  • @JerinJoseph let me try with that – Alex Man Jun 14 '17 at 16:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146686/discussion-between-alex-man-and-jerin-joseph). – Alex Man Jun 14 '17 at 16:52
  • sorry can't chat. – Jerin Joseph Jun 14 '17 at 16:54
  • @JerinJoseph I got two jars `animal-1.0.jar` and `animal-1.0-jar-with-dependencies.jar`. When I run `java -jar animal-1.0-jar-with-dependencies.jar` I got `Error: Could not find or load main class com.animal.AnimalFinder` – Alex Man Jun 14 '17 at 16:55
  • Basically, you would need the `META-INF/MANIFEST.MF` with the `Main-Class: com.animal.AnimalFinder` in the jar file you are trying to run. So, you would just need to add the plugin to the pom.xml which builds the jar. – Jerin Joseph Jun 14 '17 at 16:57
  • Ok. Now you have run into a different problem. – Jerin Joseph Jun 14 '17 at 16:58
  • As you've added the `animal-1.0.jar` as a local dependency. The classes under `animal-1.0.jar` will not be present inside `animal-1.0-jar-with-dependencies.jar`. Hence it is not able to find the `com.animal.AnimalFinder` – Jerin Joseph Jun 14 '17 at 16:59
  • You need to add the `animal-1.0.jar` to the class path when you run. – Jerin Joseph Jun 14 '17 at 17:00
  • Run it as `java -cp animal-1.0.jar -jar animal-test.jar` – Jerin Joseph Jun 14 '17 at 17:00
  • @JerinJoseph When I run `java -cp animal-1.0.jar -jar animal-test.jar` I got `no main manifest attribute, in animal-1.0.jar` – Alex Man Jun 14 '17 at 17:03

1 Answers1

2

You need to add the plugin to create the MANIFEST file in the pom.xml for animal-test too.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.animal.AnimalFinder</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Jerin Joseph
  • 1,087
  • 9
  • 17
  • Let me try with this – Alex Man Jun 14 '17 at 16:51
  • I got two jars `animal-1.0.jar` and `animal-1.0-jar-with-dependencies.jar`. When I run `java -jar animal-1.0-jar-with-dependencies.jar` I got `Error: Could not find or load main class com.animal.AnimalFinder` – Alex Man Jun 14 '17 at 16:57
  • Can you please help me on this https://stackoverflow.com/questions/44582091/one-jar-with-one-class-and-main-class-from-dependecy-jar – Alex Man Jun 16 '17 at 14:36