paip兼容windows与linux的java类根目录路径的方法



1.只有 pathx.class.getResource("")或者pathx.class.getResource(".")才是在linux,widdows下获取的路径一致。。都是class的实现路径..不过没方法获得root path..

其他的都不一致---widdows得到class root, linux却是/root/。。。

比如class.getResource("/")以及class.getClassLoader().getResource



2.只好从中截取路径了。。

但是在com下的路径截取却得到pinyin*.jar的奇怪路径,估计java的bug。。com.attilax.io这个路径走好着了.

public static String classPath()

    {

        //URL u=new URL()

        //jeoig linux hamyar cheng leig ***.jar! le ..

    //    return new File(  classpathGetter.class.getResource("").getPath()).getParent() ;  

        String path = pathx.class.getResource("").getPath();

        int index=path.lastIndexOf("/");

        String path_sub=path.substring(0, index-15);

        return path_sub;



    }



    作者Attilax  艾龙,  EMAIL:1466519819@qq.com

来源:attilax的专栏

地址:http://blog.csdn.net/attilax





///////////////mmseg中的获取路径

RL url = Dictionary.class.getClassLoader().getResource("data");

却是可以的。。

但是使用 getResource(""); 却不行,得到的/root/ in linux..

getResource("com")也很离谱,居然是pinyin*.jar



////////////////在内jar 还是classes 的类都是一样的效果...



public class pathx {



    /**linux

     * file:/root/



file:/root/

null

file:/root/

-------1-------

file:/root/

null

file:/root/

-------2-------

file:/root/

null

file:/root/

----

file:/imServer/WebRoot/WEB-INF/classes/com/attilax/io/

file:/root/

file:/imServer/WebRoot/WEB-INF/classes/com/attilax/io/





------//////////////windows

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/

null

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/

-------1-------

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/

null

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/

-------2-------

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/

null

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/

----

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/com/attilax/io/

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/

file:/D:/workspace/imServer/WebRoot/WEB-INF/classes/com/attilax/io/



     * @param args

     */

    public static void main(String[] args) {

           System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));  

           System.out.println(Thread.currentThread().getContextClassLoader().getResource("/"));  

            

           System.out.println(Thread.currentThread().getContextClassLoader().getResource("."));  

            

           System.out.println("-------1-------");

            System.out.println(pathx.class.getClassLoader().getResource(""));

            System.out.println(pathx.class.getClassLoader().getResource("/"));

            System.out.println(pathx.class.getClassLoader().getResource("."));

            System.out.println("-------2-------");

            System.out.println(ClassLoader.getSystemResource(""));  

            System.out.println(ClassLoader.getSystemResource("/"));  

            System.out.println(ClassLoader.getSystemResource("."));

            System.out.println("----");

            System.out.println(pathx.class.getResource(""));  

            System.out.println(pathx.class.getResource("/"));

            System.out.println(pathx.class.getResource("."));

            

            System.out.println(pathx.class.getResource("").getPath());  



          

    }



}

paip兼容windows与linux的java类根目录路径的方法的更多相关文章

  1. Java文件夹操作,判断多级路径是否存在,不存在就创建(包括windows和linux下的路径字符分析),兼容Windows和Linux

    兼容windows和linux. 分析: 在windows下路径有以下表示方式: (标准)D:\test\1.txt (不标准,参考linux)D:/test/1.txt 然后在java中,尤其使用F ...

  2. linux下java调用.so文件的方法1: JNI

    摘自http://blog.163.com/squall_smile/blog/static/6034984020129296931793/ https://my.oschina.net/simabe ...

  3. Windows与Linux/Mac系统时间不一致的解决方法

    Windows与Linux/Mac系统时间不一致的解决方法 分类: linux2012-02-12 14:25 1691人阅读 评论(1) 收藏 举报 windowsubuntusystemlinux ...

  4. Java进阶(四十)Java类、变量、方法修饰符讲解

    Java进阶(四十)Java类.变量.方法修饰符讲解 Java类修饰符 abstract: 将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. final: 将一个类生命为最终(即非继承类) ...

  5. C#调用java类、jar包方法(转)

    一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . 其中tes ...

  6. linux查看java jdk安装路径和设置环境变量

    一:查看类型 windows: set java_home:查看JDK安装路径 java -version:查看JDK版本 linux: whereis java which java (java执行 ...

  7. C#调用java类、jar包方法

    一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . 其中tes ...

  8. 【转】C#调用java类、jar包方法

    原文地址:http://blog.csdn.net/black0707/article/details/5769366 一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录 ...

  9. linux 查看java的安装路径

    在linux下,如何找java的安装路径 han@ubuntu:/etc$ whereis java java: /usr/bin/java /usr/share/java /usr/lib/jvm/ ...

随机推荐

  1. [其他] 蒙特卡洛(Monte Carlo)模拟手把手教基于EXCEL与Crystal Ball的蒙特卡洛成本模拟过程实例:

    http://www.cqt8.com/soft/html/723.html下载,官网下载 (转帖)1.定义: 蒙特卡洛(Monte Carlo)模拟是一种通过设定随机过程,反复生成时间序列,计算参数 ...

  2. python-操作MySQL数据库

    1.安装MySQLdb? 为了用DB-API编写MySQL脚本,必须确保已经安装了MySQL.复制以下代码,并执行: #!/usr/bin/python # -*- coding: UTF-8 -*- ...

  3. java.lang.UnsupportedClassVersionError: xxx/xxxClass : Unsupported major.minor version 51.0

    完全参考自 http://www.cnblogs.com/xing901022/p/4172410.html 这种错误的全部报错信息: 1 java.lang.UnsupportedClassVers ...

  4. IOS-Appium 自动化测试——环境配置及模拟器、真机跑测试

    在MAC环境下配置IOS的appium的自动化测试环境,主要包含三个部分: 一.环境配置 1.安装homebrew(homebrew可以提供MAC OS无法提供的很多套件) ruby -e " ...

  5. 第三章 springboot + jedisCluster

    如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制.(具体使用方式:第九章 企业项目开发--分布式缓存Redis(1)  第十章 企业项目开发--分布式缓存Redis(2)) 如果 ...

  6. 项目评价及第五周PSP的发布

         5TH 各组作品ALPHA发布体会       1:俄罗斯方块组做了主要功能,这个项目我自己原来拿c语言做过一部分,感觉此游戏细节特别多,逻辑思维需要组织,以为我的水准来说,感觉做的挺好的. ...

  7. js资源

    http://www.oschina.net/p/ace-editor https://git.oschina.net/pandao/editor.md/blob/master/editormd.js ...

  8. Java泛型总结(转)

    本文是转载,原文链接:http://www.cnblogs.com/lwbqqyumidi/p/3837629.html 一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: ...

  9. 转自知乎:GitHub基本功能

    作者:Fadeoc Khaos 链接:https://www.zhihu.com/question/20070065 来源:知乎 著作权归作者所有Github的基本功能: Repository:你和我 ...

  10. div中嵌套div水平垂直居中

    div中嵌套一个居中的div有很多方法,如果不想调整边距,有一个简单的方法: <div align="center" style="width: 200px;hei ...