定位所用的class
方案
为解决类冲突,我们可以使用下述的方案定位一个class所在的位置
ClassName.class.getResource("").getPath();
获取ClassName所在的位置,即使它是在一个jar包中;如果所在jar包添加了安全保护,会获取失败。
ClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile();
获取ClassName所在jar的位置,如果所在jar被安全保护,则获取失败。
DEMO
1 package cn.j2se.junit.classpath;
2
3 import static org.junit.Assert.*;
4
5 import java.io.UnsupportedEncodingException;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.junit.Test;
10
11 public class ClassPathTest {
12 private static final Log logger = LogFactory.getLog(ClassPathTest.class);
13 @Test
14 public void testClassPath() {
15 String classPath = ClassPathTest.class.getResource("").getPath();
16 logger.info("the classpath of ClassPathTest is:[" + classPath + "]");
17
18 assertEquals(1, 1);
19 }
20
21 @Test
22 public void testJarPath() throws UnsupportedEncodingException {
23 // System.class.getResource("") == null
24 String lfClassPath = LogFactory.class.getResource("").getPath();
25 logger.info("the classpath of LogFactory is:[" + lfClassPath + "]");
26
27 // System.class.getProtectionDomain().getCodeSource() == null
28 String lfJarPath = LogFactory.class.getProtectionDomain().getCodeSource().getLocation().getFile();
29 logger.info("the jar path of LogFactory is:[" + lfJarPath + "]");
30 assertEquals(1, 1);
31 }
32 }
测试结果:
1 [INFO ] 2015-08-25 14:26:30 CST
2 the classpath of ClassPathTest is:[/D:/develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/bin/cn/j2se/junit/classpath/]
3
4 [INFO ] 2015-08-25 14:26:30 CST
5 the classpath of LogFactory is:[file:/D:/develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/lib/commons-logging-1.1.1.jar!/org/apache/commons/logging/]
6
7 [INFO ] 2015-08-25 14:26:30 CST
8 the jar path of LogFactory is:[/D:/develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/lib/commons-logging-1.1.1.jar]
定位所用的class的更多相关文章
- LBS基站定位
LBS基站定位(Location Based Service,简称LBS)一般应用于手机用户,它是基于位置的服务,通过电信.移动运营商的无线电通讯网络(如GSM网.CDMA网)或外部定位方式(如GPS ...
- web开发如何使用高德地图API(一)浏览器定位
说两句: 以下内容除了我自己写的部分,其他部分在高德开放平台都有(可点击外链访问). 我所整理的内容以实际项目为基础希望更有针对性的,更精简. 点击直奔主题. 准备工作: 首先,注册开发者账号,成为高 ...
- 商汤提出解偶检测中分类和定位分支的新方法TSD,COCO 51.2mAP | CVPR 2020
目前很多研究表明目标检测中的分类分支和定位分支存在较大的偏差,论文从sibling head改造入手,跳出常规的优化方向,提出TSD方法解决混合任务带来的内在冲突,从主干的proposal中学习不同的 ...
- NMEA0183
NMEA简介 NMEA是全国海洋电子协会(National Marine Electronics Association):国际海上电子协会(National Marine Electronics A ...
- GCC编译器原理(一)------GCC 工具:addr2line、ar、as、c++filt和elfedit
1.3 GCC 工具 1.3.1 binutils 工具集 工具 描述 addr2line 给出一个可执行文件的内部地址,addr2line 使用文件中的调试信息将地址翻译成源代码文件名和行号. ar ...
- 玩转X-CTR100 l STM32F4 l U-Blox NEO-6M GPS卫星定位-nmealib解码库移植解码
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 扩展GPS ...
- 目标跟踪ObjectT综述介绍
此文也很详细:http://blog.csdn.net/maochongsandai110/article/details/11530045 原文链接:http://blog.csdn.net/pp5 ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
- C#调用百度高精度IP定位API通过IP获取地址
API首页:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip 1.申请百度账号,创建应用,获取密钥(AK) http://lbsyu ...
随机推荐
- 【Android】完善Android学习(四:API 3.1)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...
- 用reduce实现简单的pipe
function pipe(src, ...fns){ return fns.reduce(function(fn1, fn2){ return fn2(fn1) }, src); } undefin ...
- How to write educational schema.
Sometimes, writing such educational schemas could be of much use, and creating such docs can be bene ...
- TDD随想录
TDD随想录 谨以本文献给TDD的开创者与传播者 本文纯属个人经历,如有雷同纯属巧合 我从不觉得自己是一个好的程序员,甚至可能连合格都谈不上,不过在内心深处我却渴望着在编程这件事上获得成功. 可惜每次 ...
- jq_常用方法
//获取兄弟元素 $('.class').siblings() 当前元素所有的兄弟节点 $('.class').prev() 当前元素前一个兄弟节点 $('.class').prevaAll() 当前 ...
- ew做socks5代理
这个工具和之前讲过的xxoo类似.链接:https://www.cnblogs.com/nul1/p/8883271.html https://zhuanlan.zhihu.com/p/3282215 ...
- Python模块学习 - Functools
Functools模块 Higher-order functions and operations on callable objects,看这个标题我都是懵逼的,这都是啥啥啥啊,赶紧拿出百度翻译:可 ...
- 前端—css
css css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一.css的四种引入方式: 1.行内式 ...
- JS中的日期内置函数
用JS中的日期内置函数实现在页面显示:“今天是:2013年9月26日14:32:45”. var date=new Date(Date.parse('9/26/2013 14:32:45')); ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...