Find out the package value for class without package in Java

Description

The following code shows how to find out the package value for class without package.

getPackage() returns null for a class in the unnamed package

Example


//w  w w.j a v  a  2s  .c o m

    

public class Main {
  public static void main(String[] argv) throws Exception {

    Class cls = MyClass.class;
    Package pkg = cls.getPackage(); // null
    System.out.println(pkg);
  }
}
class MyClass{}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy