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(). AnimalFinder.java. The source code for MakeSound.java and AnimalFinder.java is as given below
MakeSound.java
package com.animal;
public interface MakeSound {
public void makesSound();
}
AnimalFinder.java
package com.animal;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
import org.reflections.Reflections;
public class AnimalFinder {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
System.out.println("checking....");
Reflections reflections = new Reflections();
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(Animal.class);
for (Class<?> class1 : annotated) {
Animal animal= class1 .getAnnotation(Animal.class);
if (animal.name().equals("cat")) {
Object customAnnot = class1.newInstance();
Method[] methodArr = class1.getDeclaredMethods();
for (Method method : methodArr) {
String methodName = method.getName();
if (methodName.equals("makesSound")) {
method.invoke(customAnnot);
}
}
}
}
}
}
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 like as shown below
package com.myanimaltest;
import com.animal.MakeSound;
import com.animal.Animal;
@Animal(key = "cat")
public class SampleAnimalTest implements MakeSound {
public void makesSound() {
System.out.println("Meowwww");
}
}
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. My expectation that I am able to get "Meowwww" printed
The pom.xml for the two project is given below
pom.xml (animal)
<project xmlns="http://ift.tt/IH78KX" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
<modelVersion>4.0.0</modelVersion>
<groupId>com.animal</groupId>
<artifactId>animal</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>animal</name>
<url>http://ift.tt/19pvvEY;
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
</dependencies>
<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>
</project>
pom.xml (animal-test)
<project xmlns="http://ift.tt/IH78KX" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myanimaltest</groupId>
<artifactId>myanimaltest</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>myanimaltest</name>
<url>http://ift.tt/19pvvEY;
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>animal-1.0</groupId>
<artifactId>animal-1.0</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/libs/animal-1.0.jar</systemPath>
</dependency>
<dependency>
<groupId>animal-1.0-jar-with-dependencies</groupId>
<artifactId>animal-1.0-jar-with-dependencies</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/libs/animal-1.0-jar-with-dependencies.jar</systemPath>
</dependency>
</dependencies>
</project>