方案

为解决类冲突,我们可以使用下述的方案定位一个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的更多相关文章

  1. LBS基站定位

    LBS基站定位(Location Based Service,简称LBS)一般应用于手机用户,它是基于位置的服务,通过电信.移动运营商的无线电通讯网络(如GSM网.CDMA网)或外部定位方式(如GPS ...

  2. web开发如何使用高德地图API(一)浏览器定位

    说两句: 以下内容除了我自己写的部分,其他部分在高德开放平台都有(可点击外链访问). 我所整理的内容以实际项目为基础希望更有针对性的,更精简. 点击直奔主题. 准备工作: 首先,注册开发者账号,成为高 ...

  3. 商汤提出解偶检测中分类和定位分支的新方法TSD,COCO 51.2mAP | CVPR 2020

    目前很多研究表明目标检测中的分类分支和定位分支存在较大的偏差,论文从sibling head改造入手,跳出常规的优化方向,提出TSD方法解决混合任务带来的内在冲突,从主干的proposal中学习不同的 ...

  4. NMEA0183

    NMEA简介 NMEA是全国海洋电子协会(National Marine Electronics Association):国际海上电子协会(National Marine Electronics A ...

  5. GCC编译器原理(一)------GCC 工具:addr2line、ar、as、c++filt和elfedit

    1.3 GCC 工具 1.3.1 binutils 工具集 工具 描述 addr2line 给出一个可执行文件的内部地址,addr2line 使用文件中的调试信息将地址翻译成源代码文件名和行号. ar ...

  6. 玩转X-CTR100 l STM32F4 l U-Blox NEO-6M GPS卫星定位-nmealib解码库移植解码

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 扩展GPS ...

  7. 目标跟踪ObjectT综述介绍

    此文也很详细:http://blog.csdn.net/maochongsandai110/article/details/11530045 原文链接:http://blog.csdn.net/pp5 ...

  8. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  9. C#调用百度高精度IP定位API通过IP获取地址

    API首页:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip 1.申请百度账号,创建应用,获取密钥(AK) http://lbsyu ...

随机推荐

  1. PHP日期时间操作

    一.设置时区 date_default_timezone_set('PRC'); 二.获取当前时间的 Unix 时间戳(格林威治时间 1970 年 1 月 1 日 00:00:00到当前时间的秒数)和 ...

  2. 【BZOJ3453】XLkxc [拉格朗日插值法]

    XLkxc Time Limit: 20 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定 k,a,n,d,p f(i ...

  3. 【NOIP】提高组2016 天天爱跑步

    [题意]n个点的树,有m个人同时开始走链,每一步花一秒,n个点都有观察员在ai秒观察,求每个观察员观察到的人数. [算法]树上差分(主席树||线段树合并) [题解]一个人的走链可以拆成u-lca和lc ...

  4. 【CODEVS】3546 矩阵链乘法

    [算法]区间DP [题解] 注意先输出右括号后输出左括号. f[i][i+x-1]=min(f[i][i+x-1],f[i][j]+f[j+1][i+x-1]+p[i]*p[j+1]*p[i+x]) ...

  5. 2008 APAC local onsites C Millionaire (动态规划,离散化思想)

    Problem You have been invited to the popular TV show "Would you like to be a millionaire?" ...

  6. java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 找不到jar包的问题,路径问题

    1.参考连接: https://blog.csdn.net/huangbiao86/article/details/6428608 折腾了一上午,找到了这错误的原因.哎……悲剧! 确认包已经被导入we ...

  7. vs 2015 插件 supercharger 破解方式

    亲测有效:效果如图 方法如下: 1.打开Supercharger的options; 2.点击Pricing & Registration 3.复制 license  然后再按Paste &am ...

  8. kimbits_USACO

    StringsobitsKim Schrijvers Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits ...

  9. 工具安装===Sublime Text-安装

    Sublime Text 是一款通用型轻量级编辑器,支持多种编程语言.有许多功能强大的快捷键(如 Ctrl+d),支持丰富的插件扩展.如果平时需要在不同编程语言间切换,那么它将会是一个,不错的选择. ...

  10. interrupted()和isInterrupted()比较+终止线程的正确方法+暂停线程

    interrupted():测试当前线程[运行此方法的当前线程]是否已经是中断状态,执行后具有将状态标志清除为false的功能. isInterrupted():测试线程对象是否已经是中断状态,但不清 ...