Package.getPackage in java returning null

后端 未结 1 1972
我在风中等你
我在风中等你 2021-01-07 20:44

I have some classes A, B, C in package com.abc

I have a Class Main in package com.pqr

Now I want to create a package object of the previous pacakge (abc).

相关标签:
1条回答
  • 2021-01-07 21:34

    Package.getPackage will only return a non-null value if the current ClassLoader is already aware of the package. Try this:

    Package pkg = Package.getPackage("com.abc");
    System.out.println(pkg);
    Class<A> a = A.class;
    pkg = Package.getPackage("com.abc");
    System.out.println(pkg);
    

    The first System.out will print 'null', the second will print the package name as the ClassLoader has then loaded a class from it.

    0 讨论(0)
提交回复
热议问题