跳至主要内容

How to Ref Files in Jar

Recently, we are working on a project which will reference some config file used in Jar. In order to reference those file, we can’t use those files absolute path (because the packaged Jar may use in any computer which will invalid the path) or relative path as we run it in a IDE (for packaged Jar has different file structure with that in IDE.
So, Java provide a mechanism to reference files: getResource

Get Resource via Different Ways

By a simple search, we can see that there exists two main ways of doing so:

this.getClass().getResource("file");
this.getClass().getClassLoader().getResource("file");

And common questions arises: “what the differences between them?”

Let’s have a look at the following code snippet:

// get file of foo/bar/name
foo.bar.A.class.getResource("name")
// get file of `A's classloader cwd`/name 
foo.bar.A.class.getResource("/name")
// get file of `A's classloader cwd`/name 
foo.bar.A.class.getClassLoader().getResource("name")
// always get null
foo.bar.A.class.getClassLoader().getResource("/name")

Class.getResource can take a “relative” resource name, which is treated relative to the class’s package. Alternatively you can specify an “absolute” resource name by using a leading slash. Classloader resource paths are always deemed to be absolute.

From the source of class#getResource, we can verify above statement:

public java.net.URL getResource(String name) {
    name = resolveName(name);
    // name is changed to foo/bar/name
    ClassLoader cl = getClassLoader0();
    if (cl==null) {
        // means this class loaded by bootstrap classLoader
        return ClassLoader.getSystemResource(name);
    }
    return cl.getResource(name);
}

And from the source code, we can actually find the third way (Only if your resources are system or global resources):

// be careful about this way, if your class is not loaded by bootstrap
// classLoader, it will return null, which is very common in application
// run in container, e.g. running in Tomcat, or Spring Boot.
ClassLoader.getSystemResource("name")
ClassLoader.getSystemResource("/name")

Pitfalls of getFile

The method above all return a URL, which have a method getFile which says ‘Gets the file name of this URL’. But this is kind of pitfall if we use it. It will work in IDE when we debug it, but fails when we package it into Jar.

The reason is simple, class loader resources are not java.io.File objects when they are in a Jar archive. In essence, the Jar archive is a compressed file like zip, so a single file in it can’t be taken as a File if it is still compressed.

Or Choose getResourceAsStream

If we look at the interface ofClass and ClassLoader more closely, we can notice that they provide more utility related to resources.
If we are only interested in the content of file, we usually find the URL less useful than an InputStream, so we prefer to use getResourceAsStream instead of getResource.

InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);

More

It’s the end of blog, but not the use of resources. Resources can be other forms other than file (image, audio etc), and we may find more usage of resources as the following scenario, which will be left for further study:

  • An applet loaded from the Internet using multiple HTTP connections.
  • An applet loaded using JAR files.
  • A Java Bean loaded or installed in the CLASSPATH.
  • A “library” installed in the CLASSPATH.

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (