Home » Java programming language

Java ClassLoader definePackage() method with example

ClassLoader Class definePackage() method: Here, we are going to learn about the definePackage() method of ClassLoader Class with its syntax and example.
Submitted by Preeti Jain, on November 28, 2019

ClassLoader Class definePackage() method

  • definePackage() method is available in java.lang package.
  • definePackage() method allows the class loader to define the package for its classes and package name defined in class loader must be unique.
  • definePackage() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • definePackage() method may throw an exception at the time of defining a package.
    IllegalArgumentException: This exception may throw when a package already exists in an existing package in this class loader or any one of its ancestors.

Syntax:

    protected Package definePackage(
        String pack_name , 
        String s_title, 
        String s_version, 
        String s_vendor, 
        String i_title, 
        String i_vendor, 
        String i_version, 
        URL seal_status);

Parameter(s):

  • String pack_name – represents the name of a package.
  • String s_title - represents the title specification.
  • String s_version – represents the version specification.
  • String s_vendor – represents the vendor specification.
  • String i_title - represents the title implementation.
  • String i_version – represents the version implementation.
  • String i_vendor – represents the vendor implementation.
  • URL seal_status – represents the sealing status (i.e. sealed when URL not null).

Return value:

The return type of this method is Package, it returns a newly created package.

Example:

// Java program to demonstrate the example 
// of Package definePackage () method of ClassLoader 

public class DefinePackageOfClassLoader extends ClassLoader {
    public static void main(String[] args) throws ClassNotFoundException {
        // Load a class
        Class cl = Class.forName("java.lang.String");
        // It returns the package of String class
        Package pack = cl.getPackage();

        // java.lang already exists in Java Library
        if (pack != null)
            System.out.println(pack.toString() + " already exists!!!!");
        else
            System.out.println("Ready to create a package by using definePackage()");
    }
}

class DefinePackage extends ClassLoader {
    // If package not exists then we can create a
    // package by using definePackage() method
    Package new_package = definePackage("includehelp.java", "Includehelp", "2.0.3", "Microsoft", null, null, null, null);
}

Output

package java.lang already exists!!!!


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.