Java ClassLoader definePackage()方法与示例

ClassLoader类definePackage()方法 (ClassLoader Class definePackage() method)

  • definePackage() method is available in java.lang package.

    definePackage()方法在java.lang包中可用。

  • definePackage() method allows the class loader to define the package for its classes and package name defined in class loader must be unique.

    definePackage()方法允许类加载器为其类定义包,并且在类加载器中定义的包名称必须唯一。

  • 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()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • definePackage() method may throw an exception at the time of defining a package.

    definePackage()方法在定义包时可能会引发异常。

    IllegalArgumentException: This exception may throw when a package already exists in an existing package in this class loader or any one of its ancestors.

    IllegalArgumentException :当此类加载器或其任何祖先的现有包中已经存在一个包时,可能引发此异常。

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.

    字符串pack_name –表示软件包的名称。

  • String s_title - represents the title specification.

    字符串s_title-表示标题规范。

  • String s_version – represents the version specification.

    字符串s_version –表示版本规范。

  • String s_vendor – represents the vendor specification.

    字符串s_vendor –代表供应商规范。

  • String i_title - represents the title implementation.

    字符串i_title-表示标题实现。

  • String i_version – represents the version implementation.

    字符串i_version –表示版本实现。

  • String i_vendor – represents the vendor implementation.

    字符串i_vendor –表示供应商实现。

  • URL seal_status – represents the sealing status (i.e. sealed when URL not null).

    URL seal_status –表示密封状态(即,URL不为null时被密封)。

Return value:

返回值:

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

该方法的返回类型为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!!!!


翻译自: https://www.includehelp.com/java/classloader-definepackage-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值