3

I have code like below sample. in that I am telling the class_name to get package name. instead of this method, i need another logic to get the package name withoud telling the class_name directly.

package smk.jsf.bean;

public class test {
public static void main(String[] args) {
    System.out.println(test.class.getPackage().getName());
//Is there any option like "this.class.getPackage().getName();" bz. i don't want use class_name direclty

}

}

Output : smk.jsf.bean

Thanks to everyone. Finally I got solution below

package smk.jsf.bean;

public class test {
public static void main(String[] args) {
String className = new Object(){}.getClass().getPackage().getName();        
System.out.println(className);

}
Karthikeyan Sukkoor
  • 968
  • 2
  • 6
  • 24
  • You need a class to know its package. In a `static` method, you **need** to: write the class which you need and call `class#...` **or** create an instance of the class and do `classInstance#getClass()#...`, being the former a *better* way. – Luiggi Mendoza Sep 27 '13 at 06:35
  • What's wrong with simply ?? `this.getClass().getPackage().getName()` – Suresh Atta Sep 27 '13 at 09:08
  • I am using static method in my project. so we can't use "this" key in it. – Karthikeyan Sukkoor Sep 30 '13 at 14:08

4 Answers4

1

Not sure it will suit you but try sun.reflect.Reflection.getCallerClass(). This class is present in JDK. It will return a Class instance of method caller. Then just getPackage(). It is really dangerous stuff but it lets you not to use the class name directly. Example usage - create a method String getPackageName() which will get caller class and return package name and call it from main.

Or you can throw any throwable, catch it and parse that throwable's stack trace to get the target package name (really sick way).

1

I have two approaches.

  • You can add a field public static final PACKAGE_INFO = "%package%"; to each file. Then traverse your source directory, read the line with the package package someName and replace the %package%

  • Use a dynamic approach at runtime. I wrote a little example program.

public class PackageExample {

    public static void main(String[] args) throws ClassNotFoundException {
        Example e = new Example();
        System.out.println(e.getPackage());
    }

}

interface MetaInformation {
    public String getPackage() throws ClassNotFoundException;
}

class InformationGatherer implements MetaInformation {
    public String getPackage() throws ClassNotFoundException {
        StackTraceElement[] ste = new Exception().getStackTrace();
        if (ste.length < 2)
            throw new IllegalStateException("StackTrace to small to determine package!");
        String clazz = ste[1].getClassName();
        Class<?> c = Class.forName(clazz);
        String package_ = "";
        Package p = c.getPackage();
        if (p != null)
            package_ = c.getPackage().getName();
        return package_;
    }
}

class Example implements MetaInformation {
    private InformationGatherer ig = new InformationGatherer();

    public String getPackage() throws ClassNotFoundException {
        return ig.getPackage();
    }
}
mike
  • 4,929
  • 4
  • 40
  • 80
0

Not sure if this helps but you can use reflection

http://docs.oracle.com/javase/tutorial/reflect/

Similar question :

See Can you find all classes in a package using reflection?

Community
  • 1
  • 1
urbiwanus
  • 713
  • 5
  • 21
-1

If it's a static method.No.

You cannot use this in a static context,since main is static method.

If it is not a static method,

String name = this.getClass().getPackage().getName();
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307