简体   繁体   English

获取jar中的目录

[英]Get the directory inside the jar

How to get the directory inside the jar? 如何获取jar中的目录?

Let's say I have a jar called foo.jar, how can I make the jar be able to access a file that's like this foo.jar!/text.txt , whereas the class is like foo.jar!/randomPackage/Main.class ? 假设我有一个名为foo.jar的jar,如何让jar能够访问类似于foo.jar!/text.txt ,而类就像foo.jar!/randomPackage/Main.class

I'm able to do that while still in eclipse, but then when I export the program into a jar, it doesn't work. 我仍然可以在eclipse中这样做,但是当我将程序导出到jar中时,它不起作用。

You need to use, eg, java.lang.Class.getResourceAsStream() to access the file. 您需要使用例如java.lang.Class.getResourceAsStream()来访问该文件。 That will work either inside a jar or on the filesystem. 这将在jar或文件系统中工作。

You have 2 main options. 你有2个主要选择。

  1. this.getClass().getClassLoader().getResourceAsStream("some/pkg/resource.properties");

  2. this.getClass().getResourceAsStream.("/some/pkg/resource.properties");

I just posted this more full post on it recently, getResourceAsStream returns null 我最近在帖子上发布了这个更全的帖子, getResourceAsStream返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM