首页 > 代码库 > 解决Bundle中使用反射时,报ClassNotFoundException异常

解决Bundle中使用反射时,报ClassNotFoundException异常

源代码下载

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,"张三");
			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>



分析:

从user-action/pom.xml中可以看到它仅仅依赖了user-service,再看Activator.java文件中,import部份:

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,

查看MANIFEST.MF

Manifest-Version: 1.0
Bnd-LastModified: 1405153337259
Build-Jdk: 1.5.0_22
Built-By: Administrator
Bundle-Activator: com.demo.user.user_action.Activator
Bundle-ManifestVersion: 2
Bundle-Name: user-action
Bundle-SymbolicName: com.demo.user.action
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-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

它只导入了 com.demo.user.user_service及org.osgi.framework

当运行Bundle时就报错了:

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


解决办法

user-action/pom.xml的maven-bundle-plugin中,加入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>

再次查看MANIFEST

Manifest-Version: 1.0
Bnd-LastModified: 1405154955217
Build-Jdk: 1.5.0_22
Built-By: Administrator
Bundle-Activator: com.demo.user.user_action.Activator
Bundle-ManifestVersion: 2
Bundle-Name: user-action
Bundle-SymbolicName: com.demo.user.action
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-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_model
Tool: Bnd-2.1.0.20130426-122213


部署运行:

User [id=1, name=张三] add....