dealsystem.java

来自「J2EE eclipse 下开发数据库一个插件」· Java 代码 · 共 42 行

JAVA
42
字号
/*
 * Created on 2003-4-9
 *
 */
package com.tanghan.util;

import java.net.URL;
import java.io.File;
/**
 * @author Jerry Tang
 * @version v0.1.0
 * @copyright  (C) 2003 Tanghan工作组
 *  */
public class DealSystem {
	/** 得到一个类的包的根路径
	 * @param cls 
	 * @return 根路径
	 */
	public static String getClassRootPath(Class cls){
		String porpoiseHome = null;
//		System.out.println(cls.toString());
		ClassLoader systemClassLoader =cls.getClassLoader();
		if (systemClassLoader == null) {
			System.out.println("ClassLoader error") ;
		}
		else {
			String className =cls.getName().replace('.', '/') + ".class";
			URL classResource = systemClassLoader.getResource(className);
			String fileComponent = classResource.getFile();
//			System.out.println(fileComponent);
			File dir=new File(fileComponent);
			String dirName=dir.toString();
			dirName = dirName.substring(0,(dirName.length()-className.length()));
			porpoiseHome =  dirName;			
		}
		if (porpoiseHome == null) {
			System.out.println("Unable to find the root path of class");
		}
		return porpoiseHome;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?