JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

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:

  • URL getResource(String name) - Finds the first resource with the given name with this ClassLoader
  • Enumeration<URL> getResources(String name) - Finds all the resources with the given name with this ClassLoader.
  • static URL getSystemResource(String name) - Finds the first resource of the given name with the System ClassLoader.
  • static Enumeration<URL> getSystemResources(String name) - Finds all resources of the given name with the System ClassLoader.

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 by Dr. Herong Yang, herongyang.com
 */
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:

  • Created logo.jpg file.
  • Created Hello.class file.
  • Copied logo.jpg into MyZIP.zip file.
  • Moved Hello.class into MyZIP.zip file.

First let's run ClassLoaderGetSystemResource.java with the default "classpath" setting:

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/jdk/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/jdk/jre/lib/rt.jar
   !/java/lang/String.class

The test result tells me that:

  • The System ClassLoader found logo.jpg in the default "classpath" - the current folder.
  • The System ClassLoader did not find Hello.class.
  • The parent ClassLoader, Extensions ClassLoader, of the System ClassLoader found sun/security/pkcs11/P11Key.class in a JAR file in the default extensions folder: <JAVA_HOME>/lib/ext.
  • The grand parent ClassLoader, Bootstrap ClassLoader, of the System ClassLoader found java/lang/String.class in a JAR file in the default system folder: <JAVA_HOME>/lib.

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_20080000/cod/MyZIP.zip!/logo.jpg

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

Find the location of sun/security/pkcs11/P11Key.class...
URL: jar:file:/C:/Program%20Files/Java/jdk/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/jdk/jre/lib/rt.jar
   !/java/lang/String.class

This result is different than the previous one:

  • The System ClassLoader found logo.jpg in MyZIP.zip specified in "classpath" instead of the current folder, because MyZIP.zip is listed before the current folder in the "classpath".
  • The System ClassLoader found Hello.class in MyZIP.zip specified in "classpath".

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 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 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 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

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
getSystemResource() Method - Finding Files