JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
getSystemResource() Method - Finding Files
This section provides a tutorial example on how to find resource files using the ClassLoader.getSystemResource() method. A resource file can an image file, a class file, or any file.
java.lang.ClassLoader also offers methods to help you find resource files:
These get resource methods are usually used to locate a file of images, audio or video data. But they can also be used to locate class files.
When a ClassLoader is called to find a resource file, it will call its parent ClassLoader to find the resource file first.
When the System ClassLoader is called to find a resource file, it will search for the file in all folders, ZIP files, or JAR files specified in the "classpath".
When you specify the resource name to a get resource method, you should use the relative path name like logo.jpg, Hello.class, or java/lang/String.class.
Here is tutorial example program showing you how to find a resource file:
/* ClassLoaderGetSystemResource.java * Copyright (c) HerongYang.com. All Rights Reserved. */ class ClassLoaderGetSystemResource { public static void main(String[] a) { java.io.PrintStream out = System.out; java.net.URL u = null; out.println(""); out.println("Find the location of logo.jpg..."); u = ClassLoader.getSystemResource("logo.jpg"); out.println("URL: "+u); out.println(""); out.println("Find the location of Hello.class..."); u = ClassLoader.getSystemResource("Hello.class"); out.println("URL: "+u); out.println(""); out.println( "Find the location of sun/security/pkcs11/P11Key.class..."); u = ClassLoader.getSystemResource( "sun/security/pkcs11/P11Key.class"); out.println("URL: "+u); out.println(""); out.println("Find the location of java/lang/String.class"); u = ClassLoader.getSystemResource("java/lang/String.class"); out.println("URL: "+u); } }
Before running this tutorial program, I prepared these files in the current folder:
First let's run ClassLoaderGetSystemResource.java using the default "classpath" setting with JDK 10:
Find the location of logo.jpg... URL: file:/C:/herong/logo.jpg Find the location of Hello.class... URL: file:/C:/herong/Hello.class Find the location of sun/security/pkcs11/P11Key.class... URL: jrt:/jdk.crypto.cryptoki/sun/security/pkcs11/P11Key.class Find the location of java/lang/String.class URL: jrt:/java.base/java/lang/String.class
The test result confirms that the JVM found all resources, .jpg files and .class files in the default "classpath" (the current directory), or in the "jrt" file.
If I want the JVM to load the logo.jpg file and the Hello.class from MyZIP.zip file, I need to use "-classpath" option to tell the JVM to search in MyZIP.zip first:
herong> java -classpath MyZIP.zip;. ClassLoaderGetSystemResource Find the location of logo.jpg... URL: jar:file:/C:/herong/MyZIP.zip!/logo.jpg Find the location of Hello.class... URL: jar:file:/C:/herong/MyZIP.zip!/Hello.class Find the location of sun/security/pkcs11/P11Key.class... URL: jrt:/jdk.crypto.cryptoki/sun/security/pkcs11/P11Key.class Find the location of java/lang/String.class URL: jrt:/java.base/java/lang/String.class
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
►ClassLoader Class - Class Loaders
What Is java.lang.ClassLoader Class
Accessing the ClassLoader of a Class
loadClass() Method - Loading Classes Explicitly
►getSystemResource() Method - Finding Files
Class Loading Problem - JAR Hell
ClassChecker.java - Reports Class Loader
ClassChecker.java - Reports Class Locations
"superclass access check failed" Class Load Error
Class Loading Followed by Class Initialization
Class Class - Class Reflections
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations