Linux下如何访问jar包同位置的文件(从jar包class文件访问)

youyouzhishen 2010-09-13 11:23:04
如题
...全文
836 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
txtdown0909 2012-01-13
  • 打赏
  • 举报
回复
URL url = TestClass.class.getProtectionDomain().getCodeSource().getLocation();
得到jar包的所在的绝对路径

得到中文路径的String字符串。
//这段代码我试过能用,但有待验证
// 将url路径转码,主要应用于汉字
String temp = "";
try {
temp = URLDecoder.decode(url.getFile(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
txtdown0909 2012-01-13
  • 打赏
  • 举报
回复
谢谢楼主,对我很有帮助
txtdown0909 2012-01-13
  • 打赏
  • 举报
回复

//得到jar包的位置。比如:c:\test.jar
public static String getJREPath(){
URL url;
// 得到jar程序的路径
url = FileUtil.class.getProtectionDomain().getCodeSource().getLocation();
// 将url路径转码,主要应用于汉字
String temp = "";
try {
temp = URLDecoder.decode(url.getFile(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
return temp;
}
//得到jar同目录的文件路径,参数是文件的名称
public static String getJARPathFile(String fileName){
return new File(getJREPath()).getParent()+File.separator+fileName;
}
youyouzhishen 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 huntor 的回复:]
如果是个不变的文件,加到jar文件里;
也可以把这个目录加入classpath
然后使用getClass().getClassLoader().getResource(...)/getResourceAsStream(...)
[/Quote]
比如说Linux一目录下有个jar包和一个properties文件,jar里的类需要解析properties文件,而properties文件可以被修改。
huntor 2010-09-13
  • 打赏
  • 举报
回复
如果是个不变的文件,加到jar文件里;
也可以把这个目录加入classpath
然后使用getClass().getClassLoader().getResource(...)/getResourceAsStream(...)
youyouzhishen 2010-09-13
  • 打赏
  • 举报
回复
和jar包在一个目录下的
huntor 2010-09-13
  • 打赏
  • 举报
回复
什么叫同位置?
请举个例子
youyouzhishen 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 phyerbarte 的回复:]
Java code

System.out.println(YourClass.class.getProtectionDomain().getCodeSource().getLocation());
[/Quote]
this.getClass().getResourceAsStream("/xxx.properties"),这样就可以了,谢谢大家。
phyerbarte 2010-09-13
  • 打赏
  • 举报
回复

System.out.println(YourClass.class.getProtectionDomain().getCodeSource().getLocation());
youyouzhishen 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 phyerbarte 的回复:]
你现在碰到的到底什么问题?properties文件读取路径确定不了?还是要引用的JAR引用不到?
[/Quote]
我的问题是“properties文件读取路径确定不了”,希望能帮忙解决,很急。
phyerbarte 2010-09-13
  • 打赏
  • 举报
回复
你现在碰到的到底什么问题?properties文件读取路径确定不了?还是要引用的JAR引用不到?
youyouzhishen 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 phyerbarte 的回复:]
引用的那个JAR包里的manifest添加Class-Path,后面跟上要引用的JAR包名,那么在使用时自动会引用这个JAR包进ClassPath,而properties文件的引用参考Java运行时的当前运行路径获得方式。

getClass().getClassLoader().getResource(...)/getResourceAsStream(...)
该方法可以读取JAR包内的文……
[/Quote]
能不能写个详细的用法,我试过
String path = ManagePage.class.getProtectionDomain().getCodeSource().getLocation().getPath();得到的是file:c:\documents and settings\...\local settings\..,而不是jar的路径
phyerbarte 2010-09-13
  • 打赏
  • 举报
回复
引用的那个JAR包里的manifest添加Class-Path,后面跟上要引用的JAR包名,那么在使用时自动会引用这个JAR包进ClassPath,而properties文件的引用参考Java运行时的当前运行路径获得方式。

getClass().getClassLoader().getResource(...)/getResourceAsStream(...)
该方法可以读取JAR包内的文件。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧