Classnotfoundexception exception is reported when reflection is used in bundle.

Source: Internet
Author: User

Tag: osgi bundle reflection

Source code download

User-model:

User-model/User:

/**  * @author wumingkun * @version 1.0.0 * @Description */package com.demo.user.user_model;/** * @author wumingkun * */public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String toString() {return "User [id=" + id + ", name=" + name + "]";}}


User-modle/POM. xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.user</groupId><artifactId>user-model</artifactId><version>0.0.1-SNAPSHOT</version><packaging>bundle</packaging><name>user-model</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions></instructions></configuration></plugin></plugins></build></project>



User-service:

Userservice

/** *  * @author wumingkun * @version 1.0.0 * @Description */package com.demo.user.user_service;/** * @author wumingkun * */public class UserSerivce {public void add(Object obj){System.out.println(obj+" add....");}}

Pom. xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.demo.user</groupId>  <artifactId>user-service</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>bundle</packaging>  <name>user-service</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.10</version>      <scope>test</scope>    </dependency>  </dependencies>  <build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions></instructions></configuration></plugin></plugins></build></project>



User-Action:

Activator

/*** @ Author wumingkun * @ version 1.0.0 * @ Description */package COM. demo. user. user_action; import Java. lang. reflect. method; import Org. osgi. framework. bundleactivator; import Org. osgi. framework. bundlecontext; import COM. demo. user. user_service.userserivce;/*** @ author wumingkun ***/public class activator implements bundleactivator {/* (non-javadoc) * @ see Org. osgi. framework. bundleactivator # Start (Org. osgi. framework. bundlecontext) */Public void start (bundlecontext context) throws exception {try {class clz = Class. forname ("com. demo. user. user_model.user "); object OBJ = clz. newinstance (); Method setidmethod = clz. getmethod ("setid", new class [] {Int. class}); setidmethod. invoke (OBJ, 1); Method setnamemethod = clz. getmethod ("setname", new class [] {string. class}); setnamemethod. invoke (OBJ, "Zhang San"); userserivce Serivce = new userserivce (); Serivce. add (OBJ);} catch (exception e) {e. printstacktrace () ;}}/* (non-javadoc) * @ see Org. osgi. framework. bundleactivator # Stop (Org. osgi. framework. bundlecontext) */Public void stop (bundlecontext context) throws exception {}}


</pre>pom.xml<pre name="code" class="html"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.user</groupId><artifactId>user-action</artifactId><version>0.0.1-SNAPSHOT</version><packaging>bundle</packaging><name>user-action</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><dependency><groupId>org.osgi</groupId><artifactId>org.osgi.core</artifactId><version>4.2.0</version></dependency><dependency><groupId>com.demo.user</groupId><artifactId>user-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-Activator>com.demo.user.user_action.Activator</Bundle-Activator></instructions></configuration></plugin></plugins></build></project>



Analysis:

From user-action/POM. XML, we can see that it only depends on user-service. In the activator. Java file, the import part is as follows:

import java.lang.reflect.Method;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import com.demo.user.user_service.UserSerivce;

Com. Demo. User. user_model not imported,

View manifest. MF

Manifest-Version: 1.0Bnd-LastModified: 1405153337259Build-Jdk: 1.5.0_22Built-By: AdministratorBundle-Activator: com.demo.user.user_action.ActivatorBundle-ManifestVersion: 2Bundle-Name: user-actionBundle-SymbolicName: com.demo.user.actionBundle-Version: 0.0.1.SNAPSHOTCreated-By: Apache Maven Bundle PluginExport-Package: com.demo.user.user_action;version="0.0.1.SNAPSHOT";uses: ="org.osgi.framework"Import-Package: com.demo.user.user_service;version="[0.0,1)",org.osgi.fr amework;version="[1.5,2)"Tool: Bnd-2.1.0.20130426-122213

It only imports com. Demo. User. user_service and org. osgi. Framework

When running bundle, an error is reported:

java.lang.ClassNotFoundException: com.demo.user.user_model.User


Solution

In Maven-bundle-plugin user-action/POM. XML, add all external packages used by bundle.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.user</groupId><artifactId>user-action</artifactId><version>0.0.1-SNAPSHOT</version><packaging>bundle</packaging><name>user-action</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><dependency><groupId>org.osgi</groupId><artifactId>org.osgi.core</artifactId><version>4.2.0</version></dependency><dependency><groupId>com.demo.user</groupId><artifactId>user-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-Activator>com.demo.user.user_action.Activator</Bundle-Activator><Import-Package> com.demo.user.user_service,org.osgi.framework,com.demo.user.user_model</Import-Package></instructions></configuration></plugin></plugins></build></project>

View manifest again

Manifest-Version: 1.0Bnd-LastModified: 1405154955217Build-Jdk: 1.5.0_22Built-By: AdministratorBundle-Activator: com.demo.user.user_action.ActivatorBundle-ManifestVersion: 2Bundle-Name: user-actionBundle-SymbolicName: com.demo.user.actionBundle-Version: 0.0.1.SNAPSHOTCreated-By: Apache Maven Bundle PluginExport-Package: com.demo.user.user_action;version="0.0.1.SNAPSHOT";uses: ="org.osgi.framework"Import-Package: com.demo.user.user_service;version="[0.0,1)",org.osgi.fr amework;version="[1.5,2)",com.demo.user.user_modelTool: Bnd-2.1.0.20130426-122213


Deployment and running:

User [ID = 1, name = Michael] add ....




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.