14

I am using only one project to port lite and full versions for that I am just updating Manifest file package names.

My project structure is like this

My App
      |_src
          |_com.example.myapp
                                              |_MyClass.java

and my Manifest package name is different.

package="com.example.myapplite"

I am using one library project in that I want package names of classes that I am using in MyApp.

I have used this

        System.out.println("Package Name " + context.getPackageName());

But its giving me a Manifest file package name value ie. com.example.myapplite

I want MyClass.java package name. ie. com.example.myapp

Also I am aware about this

 MyClass mClass = new MyClass();
 Package pack = mClass.getClass().getPackage();

But I can't use this approach because I am using following approach to get class file in my library project

Class c = Class.forName("package_name.MyClass");

How to get package name of MyClass.java file programmatically?

Mac
  • 1,153
  • 2
  • 20
  • 34
  • 1
    Try use this: YourClass.class.getPackage().getName(); – Martin Feb 15 '13 at 07:05
  • It's not clear why you'd use `new MyClass().getClass()` (effectively) rather than just `MyClass.class`... or why that's not enough for you. Your explanation of using `Class.forName` doesn't really explain what you're trying to do. What do you *wish* you could do that `Class.getPackage()` doesn't do for you? – Jon Skeet Feb 15 '13 at 07:05
  • I am using library project, to use MyClass in my library project I need to access it Class.forName("package_name.MyClass"); because library project dosent aware about MyClass.java – Mac Feb 15 '13 at 07:09
  • Also I cant create object of MyClass before accessing it. – Mac Feb 15 '13 at 07:10

5 Answers5

12

Might Be helpfull

package org.kodejava.example.lang;
import java.util.Date;

public class ObtainingPackageName {
public static void main(String[] args) {
    //
    // Create an instance of Date class, and then obtain the class package
    // name.
    //
    Date date = new Date();
    Package pack = date.getClass().getPackage();
    String packageName = pack.getName();
    System.out.println("Package Name = " + packageName);

    //
    // Create an insatnce of our class and again get its package name
    //
    ObtainingPackageName o = new ObtainingPackageName();
    packageName = o.getClass().getPackage().getName();
    System.out.println("Package Name = " + packageName);
   }
} 
Vinay
  • 6,891
  • 4
  • 32
  • 50
Ayaz Ali Khatri
  • 483
  • 4
  • 13
  • I using library project so I cant create object of MyClass before accessing it. I am using Class.forName("package_name.MyClass"); to access that class for that I need package name. – Mac Feb 15 '13 at 07:12
  • 1
    Be aware that Class.getPackage() can return null if the class loader for the class does not register the package. This is typically done from within the ClassLoader by using ClassLoader.definePackage(). You can see this in action in for example URLClassLoader. If you're not installing a custom ClassLoader, you shouldn't have any problem as the default ClassLoader will define packages while loading classes. – Timmos Oct 02 '14 at 09:59
  • What's the output? – winklerrr Mar 24 '17 at 17:19
5

The Kotlin solution:

val packageName = this.javaClass.`package`?.name
Daniil Chuiko
  • 866
  • 8
  • 7
3
String packageName = this.getClass().getPackage().getName();

will work definitely.

Kaidul
  • 15,409
  • 15
  • 81
  • 150
1

try this,

    try {
        Class c = Class.forName("package_name.MyClass");

        Object o = c.newInstance();

        Package p = o.getClass().getPackage();

        System.out.println("Package Name :: " + p.getName());

    } catch (ClassNotFoundException e) {            
        e.printStackTrace();
    } catch (InstantiationException e) {            
        e.printStackTrace();
    } catch (IllegalAccessException e) {            
        e.printStackTrace();
    }
Darshit Chokshi
  • 589
  • 3
  • 13
  • This solution seems to be good, but is there a way to get the package name for this without creating a new instance? – Felipe Mosso Oct 02 '14 at 04:32
  • @FelipeMosso just use `.getClass().getPackage().getName()` on any instance or use `[YourClassName].class.getPackage().getName()` – winklerrr Mar 24 '17 at 17:21
1

Simple example of some class NotificationListener:

Timber.d(NotificationListener.class.getPackage().getName());
Timber.d(NotificationListener.class.getSimpleName());
Timber.d(NotificationListener.class.getName());

Output:

D/PermissionsUtil: com.example.receivers
D/PermissionsUtil: NotificationListener
D/PermissionsUtil: com.example.receivers.NotificationListener
Vlad
  • 7,997
  • 3
  • 56
  • 43