GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - temikfart/java-classloading: Here are examples of loading classes and modules using custom class loaders in Java 8 and Java 9+.

BranchesTags
Open more actions menu

Latest commit

 

History

45 Commits
45 Commits

Folders and files

NameName
Last commit message
Last commit date
.idea/runConfigurations
.idea/runConfigurations
 
 
gradle/wrapper
gradle/wrapper
 
 
img
img
 
 
java-17
java-17
 
 
java-8
java-8
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

Java Class Loading examples


This repository contains the code for all the examples from the article on Habr. Each example comes with its own README.md.

Each example demonstrates how class loaders are associated with classpath/modulepath. Examples using Java versions 8 and 17 are considered, where the main difference is the modular system that appeared in Java 9.

Examples:

  • Java 8
    • Manual Loading
    • Auto Loading
  • Java 17
    • Manual
      • One Module
        • Java 8 Style
        • Java 17 Style
      • Few Modules
        • Together
        • Separately
          • No Dependencies
          • No Dependencies (hack)
          • With Dependencies
    • Auto
      • One Module
        • Java 8 Style
        • Java 17 Style
      • Few Modules
        • Together

Used classes


Each example uses just a few classes for demonstration purposes. Below are the features of each of them.

class Main


A class with a main method, in which, depending on the example, either the simplest classes Cat or Dog are loaded, or instances of the simplest classes are created. At the end of the method, the talk method is called on an instance of the simplest class.

Between instantiation and calling the talk method, information about class loaders and/or the name of the modules to which the classes belong is printed.

class CustomClassLoader


A custom class loader that almost completely satisfies the delegation model, except that it first tries to load the class , and only then delegate to the parent loader.

Methods:

  • loadClass:
    1. First, an attempt is made to find a class among the loaded classes;
    2. Then an attempt is made to load the class yourself, if it is from the application;
    3. If the class is not from the application, then the loading is delegated to the ancestor.
  • findClass: Loads the bytecode of a class (if found) and defines it as a class.

If you try to load a class from the java.* package, the JVM will throw java.lang.SecurityException: Prohibited package name: java.*. To determine that the class is from an application, it checks that binary name begins with ru.ispras.

class Cat


The simplest class to load with a single talk method that prints the word "Meow" to stdout.

class Dog


The simplest class to load with a single talk method that prints the word "Woof" to stdout.

About

Here are examples of loading classes and modules using custom class loaders in Java 8 and Java 9+.

Topics

classloadersclasspathguidehabrjava-8java-9java-9-modulesmodule-apimodule-pathmodule-system

Resources

Readme
MIT license
Activity

Stars

3 stars

Watchers

1 watching

Forks

0 forks
Report repository

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.