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 reource file:

/**
 * ClassLoaderGetSystemResource.java
 * Copyright (c) 2010, 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 1.7.0:

C:\herong\jvm>java ClassLoaderGetSystemResource

Find the location of logo.jpg...
URL: file:/C:/herong/jvm/logo.jpg

Find the location of Hello.class...
URL: null

Find the location of sun/security/pkcs11/P11Key.class...
URL: jar:file:/C:/Program%20Files/Java/jdk1.7.0_45/jre/lib/ext
   /sunpkcs11.jar!/sun/security/pkcs11/P11Key.class

Find the location of java/lang/String.class
URL: jar:file:/C:/Program%20Files/Java/jdk1.7.0_45/jre/lib/rt.jar
   !/java/lang/String.class

The test result tells me that:

Now run ClassLoaderGetSystemResource.java with a differnt "classpath" setting:

C:\herong\jvm>java -classpath MyZIP.zip;. ClassLoaderGetSystemResource

Find the location of logo.jpg...
URL: jar:file:/C:/herong/jvm/MyZIP.zip!/logo.jpg

Find the location of Hello.class...
URL: jar:file:/C:/herong/jvm/MyZIP.zip!/Hello.class

Find the location of sun/security/pkcs11/P11Key.class...
URL: jar:file:/C:/Program%20Files/Java/jdk1.7.0_45/jre/lib/ext
   /sunpkcs11.jar!/sun/security/pkcs11/P11Key.class

Find the location of java/lang/String.class
URL: jar:file:/C:/Program%20Files/Java/jdk1.7.0_45/jre/lib/rt.jar
   !/java/lang/String.class

This result is different than the previous one:

Comparing to the same test with JDK 1.6.0 in 2010, the behavior of class searching is not changed.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

ClassLoader Class - Class Loaders

 What Is Class Loader?

 What Is java.lang.ClassLoader Class?

 Accessing the ClassLoader of a Class

 JVM "-verbose:class" Option

 loadClass() Method - Loading Classes Explicitly

getSystemResource() Method - Finding Files

 Class Loading Problem - JAR Hell

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 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

 Outdated Tutorials

 References

 PDF Printing Version