Java Program to Get the Package Name of a Class

You are currently viewing Java Program to Get the Package Name of a Class

Java program to get the package name of a class

// Java program to get the package name 
// of a class

public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class cls1 = Class.forName("java.util.Set");
    Class cls2 = Class.forName("java.lang.String");

    System.out.println("Package names: ");
    System.out.println(cls1.getPackage());
    System.out.println(cls2.getPackage());
  }
}

Output

Package names: 
package java.util
package java.lang

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply