java获得执行jar的运行路径

来源:互联网 发布:电视mac是什么意思啊 编辑:程序博客网 时间:2024/04/29 15:49

题记

上一篇使用了一个叫fat-jar的插件来制作jar包,确实很方便。但我们更容易遇到另一个更为棘手的问题!如何得到jar包的运行路径?

如果没有这个路径,我们读取文件可能找不到路径,写文件可能写到别的目录里了!

而且,调试代码时我们需要eclipse里的命令行里运行,而不需要打包;最终发布时我们需要打成jar包!所以,这部分代码应该要支持以上两种形式。

一般执行jar包有下面两种方式:

1、cd 到包含该jar包的目录里,执行命令

cd E:\workspace4svn\Demorun\
java -jar  Demorun_fat.jar

2、直接加入绝对路径(比如把jar包直接拖入cmd窗口,就可以得到jar包的路径了)并执行命令

java -jar  E:\workspace4svn\Demorun\Demorun_fat.jar

所以,如果直接取相对路径就会得到两个结果,前者会是“E:\workspace4svn\Demorun\”,后者是"当前目录,如C:\"。


解决办法

下面以一个jar包和一个data/in.txt为例来说明下面两种方法:

方法一:

System.getProperty("java.class.path")

在eclipse里运行(不打包)的结果是: “E:\workspace4svn\Demorun\bin;E:\workspace4svn\Hello\Hello_fat.jar” (注意:Hello_fat.jar不是可执行的jar,是从外部引入的包)

在cmd里运行jar包的结果是:“E:\workspace4svn\Demorun\Demorun_fat.jar”

[小结] 

(1) 这种方法在eclipse中运行结果的意思是"Path used to find directories and JAR archives containing class files.",也就是结果包括了可执行.class文件的路径以及引入的jar包路径

(2) 打包成jar包后,结果是jar包执行的路径!而且可以识别中文!

(3) 过于System.getProperty的用法参见http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

参考代码如下:

[java] view plaincopyprint?
  1. import java.io.File;  
  2. import java.net.URL;  
  3. import java.net.URLDecoder;  
  4.   
  5. public class MyPath {  
  6.       
  7.     public static String getPath(){  
  8.         String filePath = System.getProperty("java.class.path");  
  9.         String pathSplit = System.getProperty("path.separator");//windows下是";",linux下是":"  
  10.           
  11.         if(filePath.contains(pathSplit)){  
  12.             filePath = filePath.substring(0,filePath.indexOf(pathSplit));  
  13.         }else if (filePath.endsWith(".jar")) {//截取路径中的jar包名,可执行jar包运行的结果里包含".jar"  
  14.               
  15.             //此时的路径是"E:\workspace\Demorun\Demorun_fat.jar",用"/"分割不行  
  16.             //下面的语句输出是-1,应该改为lastIndexOf("\\")或者lastIndexOf(File.separator)  
  17. //          System.out.println("getPath2:"+filePath.lastIndexOf("/"));  
  18.             filePath = filePath.substring(0, filePath.lastIndexOf(File.separator) + 1);  
  19.               
  20.         }  
  21.         return filePath;  
  22.     }  
  23. }  

方法二:『力荐』
ClassName.class.getProtectionDomain().getCodeSource().getLocation().getPath()

在eclipse里运行(不打包)的结果是: “/E:/workspace/Demorun/bin/”

在cmd里运行jar包的结果是:“/E:/workspace/Demorun/Demorun_fat.jar”

如果不加.getPath,则结果是"file:/E:/workspace/Demorun/bin/"和"file:/E:/workspace/Demorun/Demorun_fat.jar",也就是说结果前面多了个"file:"

[小结]

(1) 这种方法不支持中文,需要转化下。

(2) 特别注意结果中的"斜线",方法一是windows中路径的正确表示,用"\"分割;方法二里是用"/"来分割,不是正确的路径!所以需要用file.getAbsolutePath();转化!

(3) 结果差异不大,在eclipse中的结果是.MainClass所在目录,而jar包运行的结果是jar包的完整路径。很容易可以得出程序运行的目录!具体程序如下:

[java] view plaincopyprint?
  1. import java.io.File;  
  2. import java.net.URL;  
  3. import java.net.URLDecoder;  
  4.   
  5. public class MyPath {  
  6.     public static String getPath() {  
  7.         URL url = MyPath.class.getProtectionDomain().getCodeSource().getLocation();  
  8.         String filePath = null;  
  9.         try {  
  10.             filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         if (filePath.endsWith(".jar")) {// 可执行jar包运行的结果里包含".jar"  
  15.             // 截取路径中的jar包名  
  16.             filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);  
  17.         }  
  18.           
  19.         File file = new File(filePath);  
  20.           
  21.         // /If this abstract pathname is already absolute, then the pathname  
  22.         // string is simply returned as if by the getPath method. If this  
  23.         // abstract pathname is the empty abstract pathname then the pathname  
  24.         // string of the current user directory, which is named by the system  
  25.         // property user.dir, is returned.  
  26.         filePath = file.getAbsolutePath();//得到windows下的正确路径  
  27.         return filePath;  
  28.     }  
  29. }  

总结:

在实际使用过程中,只需要使用上述两种方法中的一种即可!!行文比较仓促,如有错误,欢迎指正!转载请声明出处:blog.csdn.net/whuslei

参考:

http://1985wanggang.blog.163.com/blog/static/776383320096166451898/

http://www.cherrot.com/2012/02/howto-get-directory-of-jar-archive.html#comment-317

http://www.zeali.net/entry/404
0 0
原创粉丝点击