JAVA 在包里搜索所有的类
来源:互联网 发布:苹果手机下安卓软件 编辑:程序博客网 时间:2024/06/05 12:44
今天突然想到可不可以动态的去加载添加的类,这样可以省去以后添加类时候修改的麻烦。借鉴前人的
部分经验得到如下代码:
import java.io.File;import java.io.FileInputStream;import java.util.ArrayList;import java.util.List;import java.util.jar.JarEntry;import java.util.jar.JarInputStream;public class FindPackageClass {public static void main(String[] args) {// String ss="cn.yyzx.test.TestC";// try {// Thread a=(Thread) Class.forName(ss).newInstance();// a.start();// } catch (Exception e) {// e.printStackTrace();// }List<String> cls = getClassInPackage("cn.yyzx.msgsend");for (String s : cls) {System.out.println(s);}}public static List<String> getClassInPackage(String pkgName) {List<String> ret = new ArrayList<String>();String rPath = pkgName.replace('.', '/') + "/";try {for (File classPath : CLASS_PATH_ARRAY) {if (!classPath.exists())continue;if (classPath.isDirectory()) {File dir = new File(classPath, rPath);if (!dir.exists())continue;for (File file : dir.listFiles()) {if (file.isFile()) {String clsName = file.getName();clsName = pkgName+ "."+ clsName.substring(0, clsName.length() - 6);ret.add(clsName);}}} else {FileInputStream fis = new FileInputStream(classPath);JarInputStream jis = new JarInputStream(fis, false);JarEntry e = null;while ((e = jis.getNextJarEntry()) != null) {String eName = e.getName();if (eName.startsWith(rPath) && !eName.endsWith("/")) {ret.add(eName.replace('/', '.').substring(0,eName.length() - 6));}jis.closeEntry();}jis.close();}}} catch (Exception e) {throw new RuntimeException(e);}return ret;}private static String[] CLASS_PATH_PROP = { "java.class.path","java.ext.dirs", "sun.boot.class.path" };private static List<File> CLASS_PATH_ARRAY = getClassPath();private static List<File> getClassPath() {List<File> ret = new ArrayList<File>();String delim = ":";if (System.getProperty("os.name").indexOf("Windows") != -1)delim = ";";System.out.println(System.getProperty("os.name"));for (String pro : CLASS_PATH_PROP) {String[] pathes = System.getProperty(pro).split(delim);for (String path : pathes) {System.out.println(path);ret.add(new File(path));}}return ret;}}
用JDK测试没问题,但到TOMCAT上却无法加载项目的类路径。代码要做修改,先做以上备份。
问题出在 System.getProperty("java.class.path")无法获得项目路径
最后为了简化起见,在web.xml中配置项目路径 然后不搜索系统类
//servlet
public void init(ServletConfig config) throws ServletException {String path = config.getInitParameter("cpath");try {List<String> channel = FindPackageClass.getClassInPackage("cn.yyzx.msgsend", path);for (String ss : channel) {Runnable nc = (Runnable) Class.forName(ss).newInstance();new Thread(nc).start();}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}this.init();}
public class FindPackageClass {public static void main(String[] args) {List<String> cls = getClassInPackage("cn.yyzx.msgsend", "");for (String s : cls) {System.out.println(s);}}public static List<String> getClassInPackage(String pkgName, String path) {List<String> ret = new ArrayList<String>();String rPath = pkgName.replace('.', '/') + "/";try {File classPath = new File(path);if (classPath.isDirectory()) {File dir = new File(classPath, rPath);for (File file : dir.listFiles()) {if (file.isFile()) {String clsName = file.getName();clsName = pkgName + "." + clsName.substring(0, clsName.length() - 6);ret.add(clsName);}}} else {FileInputStream fis = new FileInputStream(classPath);JarInputStream jis = new JarInputStream(fis, false);JarEntry e = null;while ((e = jis.getNextJarEntry()) != null) {String eName = e.getName();if (eName.startsWith(rPath) && !eName.endsWith("/")) {ret.add(eName.replace('/', '.').substring(0,eName.length() - 6));}jis.closeEntry();}jis.close();}} catch (Exception e) {throw new RuntimeException(e);}return ret;}}
http://www.linuxidc.com/Linux/2010-05/26011.htm
- JAVA 在包里搜索所有的类
- java 查找包里的所有类名
- Java 包与包之间的访问 (类在不同包里)
- 搜索局域网里的所有IP地址
- java中如何在eclipse里查看一个接口的所有实现类
- java中如何在eclipse里查看一个接口的所有实现类?
- java得到一个包的所有类
- Java 获取包下的所有类
- JAVA里的各种包
- java中如何在eclipse里查看一个接口的所有实现类或一个类的子父级关系?
- java中如何在eclipse里查看一个接口的所有实现类或一个类的子父级关系?
- 在内核里查看所有的快捷键
- android内部搜索控件下的子控件。比如:在某个VIEW对象里(LinearLayout)中 循环所有控件
- [小技巧] Excel 里搜索所有的 Sheet
- 在table里动态获取tr里的所有数据
- 怎样在 CentOS 里下载 RPM 包及其所有依赖包
- 怎样在 CentOS 里下载 RPM 包及其所有依赖包
- 怎样在 CentOS 里下载 RPM 包及其所有依赖包
- C++数据结构--直接插入排序
- SiteMesh框架的用法。。。。。
- FatMouse's Speed-HDU-动态规划
- Ubuntu常见问题汇总
- expriment : 在MASM工程中使用Unicode字符串
- JAVA 在包里搜索所有的类
- 原生JavaScript中获取元素索引的函数
- Python基础04 运算
- <s:select>和<s:doubleselect>的一般使用
- 更改git默认编辑器:change git default editor to "gedit" or "Sublime Text 2"
- 图的邻接矩阵表示法及顶点入度、出度的计算方法
- 换肤,页面要刷新吗
- Python基础05 缩进和选择
- 关于virtualbox安装增强功能包的总结