Package name from classname .class

Is there any way to know the package name of the class from its .class without using the Reflection API's since there we need to instantiate the class with its fully qualified name. Can anybody suggest a method to do it.

javap requires the name of fully qualified class, so
if you are not aware of it, this utility will not
work.
We cannot create an instance of the class, if we donot
know the full name of the class <including the package
name>
Both the above ways I think will not satisfy the
purpose.I'm having a hard time understanding why anyone would want to instantiate a class they don't know anything about, or how anyone could expect to do so.

Similar Messages

  • Getting Package Name From Class File Without Parsing File

    I am writing an application where I need to get the package name from a class file without parsing the class file itself.
    Basically, what happens is the user chooses a class file from anywhere in the file system. I then want to use reflection to get information about that class. To do that I need the fully qualified class name. I know that the package name is part or all of the path name but I don't know for sure which part it is. I don't want to parse the class file directly for the package name in case the class file spec is changed (that way I won't have to rewrite any code).
    Does Java have any way of getting this information without doing something stupid like gradually stripping off part of the pathname and trying it?

    The ClassLoader way seems to work fine, copy a class file to "A.class" and run this:import java.io.*;
    class ClassLoaderExample {
        public static void main(String[] args) throws Exception {
            class MyClassLoader extends ClassLoader {
                public Class load() throws IOException {
                    File f = new File("A.class");
                    byte[] classData = new byte[(int) f.length()];
                    new RandomAccessFile(f, "r").readFully(classData);
                    Class loaded = defineClass(null, classData, 0, classData.length);
                    resolveClass(loaded);
                    return loaded;
            Class c = new MyClassLoader().load();
            System.out.println(c);
            System.out.println(c.newInstance());
    }

  • How can I assign image file name from Main() class

    I am trying to create library class which will be accessed and used by different applications (with different image files to be assigned). So, what image file to call should be determined by and in the Main class.
    Here is the Main class
    import org.me.lib.MyJNIWindowClass;
    public class Main {
    public Main() {
    public static void main(String[] args) {
    MyJNIWindowClass mw = new MyJNIWindowClass();
    mw.s = "clock.gif";
    And here is the library class
    package org.me.lib;
    public class MyJNIWindowClass {
    public String s;
    ImageIcon image = new ImageIcon("C:/Documents and Settings/Administrator/Desktop/" + s);
    public MyJNIWindowClass() {
    JLabel jl = new JLabel(image);
    JFrame jf = new JFrame();
    jf.add(jl);
    jf.setVisible(true);
    jf.pack();
    I do understand that when I am making reference from main() method to MyJNIWindowClass() s first initialized to null and that is why clock could not be seen but how can I assign image file name from Main() class for library class without creating reference to Main() from MyJNIWindowClass()? As I said, I want this library class being accessed from different applications (means different Main() classes).
    Thank you.

    Your problem is one of timing. Consider this simple example.
    public class Example {
        public String s;
        private String message = "Hello, " + s;
        public String toString() {
            return message;
        public static void main(String[] args) {
            Example ex = new Example();
            ex.s = "world";
            System.out.println(ex.toString());
    }When this code is executed, the following happens in order:
    1. new Example() is executed, causing an object to constructed. In particular:
    2. field s is given value null (since no value is explicitly assigned.
    3. field message is given value "Hello, null"
    4. Back in method main, field s is now given value "world", but that
    doesn't change message.
    5. Finally, "Hello, null" is output.
    The following fixes the above example:
    public class Example {
        private String message;
        public Example(String name) {
            message = "Hello, " + name;
        public String toString() {
            return message;
        public static void main(String[] args) {
            Example ex = new Example("world");
            System.out.println(ex.toString());
    }

  • Bad package name of main-class, javafx 1.3, java 7u15

    Hello,
    I'm facing the same problem as what I read in this previous thread from someone else:
    Bad package name of main-class
    I originally tried to hijack it and rip ownership of that thread away for my own issue but a kind forum moderator has split my post away to let it stand on its own, as I should have done to begin with.
    My question is ...
    ... and I don't have any solution. I am using a javaFX app (1.3) which works perfectly with jre 1.6_11 and 1.7_11, but not with 1.7_15. The exception "Bad package name of main-class" is logged in the console.
    I read on the Internet that fixes in java 1.7_13 cause problems to all self-signed applet. So I signed mine with a trusted certificate delivered by my company, and I checked what pmgrundy said 2 posts before, but it didn't solve the problem.
    My jnlp file is corresponding to the example given in the official tutorial for applet deployment (http://docs.oracle.com/javafx/1.3/tutorials/deploy-applet/#configure), so the main targeted class is "com.sun.javafx.runtime.adapter.Applet".
    Do you have information that can help me in solving this problem ?
    Thanks in advance,
    Sébastien.

    Some additional information to the problem, if it can help :
    With JRE 6u43 : the applet is initialized and working properly. So the problem might not be due to the security fixes, as this version is including them.
    With JRE 7u15 & 7u17 : I tried to run the applet with the runtime v1.3.0_b412, then v1.3.1_b101a, but the problem is the same.
    When tracing is enabled through the Java Control Panel, the following lines are displayed at the end of the log :
    basic: passing security checks; secureArgs:true, allSigned:false
    basic: continuing launch in this VM
    preloader: Delivering: DownloadEvent[type=verify,loaded=1, total=1, percent=100]
    preloader: Start progressCheck thread
    basic: exception: Bad package name of main-class.
    ExitException[ 3]java.lang.SecurityException: Bad package name of main-class
         at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
         at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
         at java.lang.Thread.run(Unknown Source)
    preloader: Delivering: ErrorEvent[url=null label=Bad package name of main-class cause=Bad package name of main-class
    preloader: Stop progressCheck thread
    ui: Show default error panel
    security: Reset deny session certificate storeI think that the lines preceding those lines are about the validation of the different jars included, using the certificates. When the certificates are not installed on the user workstation, 2 dialog boxes are displayed during the applet validation, so as to ask the users if the applet should be run (and the JavaFX runtime). The certificates are well displayed as trusted. Then, after acceptance, the error occurs.
    Edited by: sebadavi on Mar 13, 2013 5:07 AM

  • Package Name from Calls

    My problem is this. I have the following 3 packages.
    layer1.layer2a
    layer1.layer2a.layer3
    layer1.layer2b
    I have a interface in layer2a that is either public to everyone or private just to that package. I need for layer2b NOT to have access to it but for all packages that start with layer1.layer2a to have access to it. So package layer1.layer2a.layer3 would have access to it and package layer1.layer2b would not.
    I thought that if I could get the calling classes package name I could return a null value if the package name does not start with layer1.layer2a, however the below code always returns null.
    this.getClass().getPackage(); //returns null
    this.getClass().getDeclaringClass() //returns null
    All of my classes definetly have package names so I'm not sure why the first line returns null. And yes I know that the first line will not return the calling classes package. I was just testing it.
    Can anyone help me with this? If you have a better solution to my problem please post.
    Below is my code example.
    package layer1.layer2a;
    public class DataFile {
         private String userId="test";
         public String getUserId(){
    //System.out.println("package name = "+this.getClass().getPackage());
    //System.out.println("calling class is = "+this.getClass().getDeclaringClass());
              return userId;
    -----------------------calling class---------------------------
    package layer1.layer2a;
    public class MyTest{
         public void callDataFile(){
              DataFile df = new DataFile();
              String s = df.getUserId();          
    System.out.println("s = "+s);          
    }

    So package layer1.layer2a.layer3 would have access to it and package layer1.layer2b would not.Not possible.
    There is no hierarchial relationship between packages. Given the following packages:
    A.B
    A.B.C
    A.Z
    The package A.B.C has the same relationship to A.B as it does to A.Z. There is no way to give expose something to one package without exposing it to all other packages.
    Solutions:
    -Look at the design again. Maybe it can be refactored to provide a different solution.
    -Just expose it, and document that the interface should only be used in a certain way.

  • How get controls name from a class private data?

    How can i get the names of the controls inside a class private data?
    I am using Actor Framework and trying to create a method tha will be executed when launch the actor. This method need a list o all the control names inside the class data to search for the initial value inside a configuration file (config.ini), the key on the configuration file will be the name of the control.
    Thanks.
    Solved!
    Go to Solution.

    You are already making the overriding method just because you have to write to the Bundle By Name.  And then how are you going to handle all of the data types the keys could be.  You are making things more difficult than it should be for really very little benefit.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How do I call methods with the same name from different classes

    I have a user class which needs to make calls to methods with the same name but to ojects of a different type.
    My User class will create an object of type MyDAO or of type RemoteDAO.
    The RemoteDAO class is simply a wrapper class around a MyDAO object type to allow a MyDAO object to be accessed remotely. Note the interface MyInterface which MyDAO must implement cannot throw RemoteExceptions.
    Problem is I have ended up with 2 identical User classes which only differ in the type of object they make calls to, method names and functionality are identical.
    Is there any way I can get around this problem?
    Thanks ... J
    My classes are defined as followes
    interface MyInterface{
         //Does not and CANNOT declare to throw any exceptions
        public String sayHello();
    class MyDAO implements MyInterface{
       public String sayHello(){
              return ("Hello from DAO");
    interface RemoteDAO extends java.rmi.Remote{
         public String sayHello() throws java.rmi.RemoteException;
    class RemoteDAOImpl extends UnicastRemoteObject implements RemoteDAO{
         MyDAO dao = new MyDAO();
        public String sayHello() throws java.rmi.RemoteException{
              return dao.sayHello();
    class User{
       //MyDAO dao = new MyDAO();
       //OR
       RemoteDAO dao = new RemoteDAO();
       public void callDAO(){
              try{
              System.out.println( dao.sayHello() );
              catch( Exception e ){
    }

    >
    That's only a good idea if the semantics of sayHello
    as defined in MyInterface suggest that a
    RemoteException could occur. If not, then you're
    designing the interface to suit the way the
    implementing classes will be written, which smells.
    :-)But in practice you can't make a call which can be handled either remotely or locally without, at some point, dealing with the RemoteException.
    Therefore either RemoteException must be part of the interface or (an this is probably more satisfactory) you don't use the remote interface directly, but MyInterface is implemented by a wrapper class which deals with the exception.

  • Issue with renaming the package name from "Oracle" to "oracle" in windows

    Hi all,
    I created a project in which initially I created all the jsffs in a package Oracle/communications. In one of the jsff pages(a.jsff) I have include tag <jsp:directive.include file="/*Oracle*/communications/b.jsff"/>
    But later I refactored the code and renamed the package to oracle/communications and I modified the include tag to <jsp:directive.include file="/*oracle*/communications/b.jsff"/>
    But after that change also when I deploy the ear and try to access the a.jsff, I am getting the file not found exception.
    Root cause of ServletException.
    oracle.jsp.parse.JspParseException:
    /adflib/oracle/communications/a.jsff: Line # 19, <jsp:directive.include file="/Oracle/communications/b.jsff" xmlns:jsp="http://java.sun.com/JSP/Page"/>
    Error: Include file not found: /Oracle/communications/b.jsff
    Do I need to change any other configuration file?
    The same ear when deployed in linux is working absolutely fine.
    Thanks in advance,
    Vamsi

    Windows doesn't know the difference between upper case and lowercase file names. So renaming Oracle to oracle will not change the file name.
    To change the file name you have to rename it from Oracle to xxoracle and then to oracle.
    Timo

  • Where is the Package Name and Class Name for Android to Facebook SSO?

    Where is the Package Name and Class Name for Android to Facebook SSO?
    I am trying to fill these fields http://developers.facebook.com/docs/mobile/android/build/#ref for Facebook but i can't figure out what to place in Android Package Name and Andorid Class Name,
    same thing for the Android Key Hash, I followed this guide http://www.skoobalon.com/blog/2012/06/13/getting-the-android-key-hash-for-facebook-from-an -air-app/ but I am not sure if it is the right thing to do.
    I was thinking maybe the Pakage Name is air.com.myname.myappname (where com.myname.myappname is also the id used in the application.xml) and the Class name is  air.com.myname.myappname.MyDocumentClass ?
    Is there an official guide somewhere?
    Also do you guys know if there is somewhere a library we can use within AIR to do SSO on Facebook from Andorid?

    For what it's worth, I don't think a WiFi connection would be reliable enough. You wouldn't want your picture to freeze just because a neighbour has turned on their baby monitor. WiFi uses the same frequencies as a myriad of other devices, as well as all your neighbours' WiFi.

  • Any Function Module/Class to get Program's Package Name??

    Hi Guys,
    Are there any Function Modules/Classes that return the Package name of any given Program?
    I have had a look but have been unable to find any...
    thanks,
    C
    PS POINTS WILL BE REWARDED

    Hi,
    1)u can get Package name from SE93
    go to SE93>give transaction code>display
    2)you can also get from
    go to t-code>system>status>d.click on program name>goto-->attributes
    Thanks
    Ankur Sharma

  • Full package name?

    All,
    I am very new to Javadoc. I would like to know how (if possible) to eliminate the full package name from any descriptions. For example, if I have a method that returns a Hashtable, the Javadoc that gets generated looks like this:
    public static java.util.Hashtable newMethod()
    I would like it to look like this:
    public static Hashtable newMethod()
    I'm using the Eclipse tool, and that is actually calling Javadoc and passing the appropriate parms (v1.4).
    I'm new to Javadoc, so I don't know if the generated method (with the package name) is preferred. I've also read some of the forum's comments on how to get started with this. I'm attempting to consolidate the lengthy Javadoc documentation, and some of the comments I've read have been helpful.
    Thanks,
    Van Williams

    The default should display the package name only
    for classes not in the "current" package.
    For 1.4, use "-noqualifier all" to prevent the package
    name from appearing ahead of the class name
    for all packages. This is further described at:
    http://java.sun.com/j2se/1.4/docs/tooldocs/windows/javadoc.html#noqualifier
    You can reduce the confusion this may cause
    by using -linkoffline to create links to
    classes not being documented, such as to
    java.util.HashTable in your example. This
    is further described at:
    http://java.sun.com/j2se/1.4/docs/tooldocs/windows/javadoc.html#linkoffline
    -Doug Kramer
    Javadoc team

  • Full package name needed when using FindClass?

    When loading a class for use by the invocation interface, do I need to use the full package name of the class to be loaded, or just the class name?
    For example, I have a program called MyProg. It is part of package MyProg, and when I run it, I have to do so using java MyProg.MyProg.
    When loading the class, I've tried both of the following:
    cls = (*env)->FindClass(env, "MyProg\\MyProg");
    cls = (*env)->FindClass(env, "MyProg");Neither one works. Anyone know for sure which one it shoulf be? I suspect that there is a classpath issue coming into play as well, but that's another post. :)

    Thanks! Just to clarify, this is on a Win32 machine.

  • Package name when the doman name contains "-"

    Recently I'm working for a project for a company whose domain name like www.my-donkey.com. By convention, the Java package name should start with com.my-donkey.xxx, unfortunately the hypen is not allowed in the package name.
    What is the standard Java convention to address this?

    Remember that the package naming convention is exactly that: a convention. It isn't a rule, nothing checks them and you don't have to follow it. It was just an idea that someone came up with to make it less likely that package names from different organisations would collide. Go with Kaj's idea, package names don't have any legal ramifications or anything

  • JDeveloper Addin - Get Package Name

    I'm creating a new Wizard addin for JDeveloper that basically does the same thing as creating a new java file, however it will create the JavaSourceNode with content from a template using my companies coding standards.
    My question / problem is that I can't figure out how to get the package name from JDeveloper to display in my JDialog similar to how creating a new java file works.
    When creaing a new java file, you can select a package and click new and the package name that displays in the dialog is that package name.
    Can any one suggest how to get the package name in the same manner that creating a new java file works?

    Here is the code that I came up with that I think fits with creating a new java file;
    public String getPackageName(Context context) {
    JProject jProject = (JProject)context.getProject();
    String packageName = null;
    // Get a selected element
    Element element = context.getElement();
    // If element is not NULL and is of instance of PackageFolder then
    // set the packageName to the selected package.
    if ((element != null) && (element instanceof PackageFolder)) {
    // Set the packageName to the package that was selected
    packageName = element.getShortLabel();
    } else {
    // If no package was selected then try to get the first package
    // entry of the <defaultPackages> element of the jpr. If there
    // are no packages then just use the default jpr package
    // name.
    if (!(jProject.getDefaultPackages().isEmpty())) {
    packageName = (String)jProject.getDefaultPackages().get(0);
    } else {
    packageName = jProject.getDefaultPackage();
    } // end if not empty
    } // end if
    return packageName;
    } // getPackageName
    The plugin that you suggested is very cool, but a little different that what I'm trying to do.

  • How can i get all java class names from a package using reflection?

    hi,
    can i get all classes name from a package using reflection or any other way?
    If possible plz give the code with example.

    You can't, because the package doesn't have to be on the local machine. It could be ANYWHERE.
    For example, via a URLClassLoader (ie from the internet) I could load a class called:
    com.paperstack.NobodyExpectsTheSpanishInquisitionI haven't written it yet. But I might tomorrow. How are you going to determine if that class is in the package?
    This subject comes up a lot. If you want to do something a bit like what you're asking for (but not quite) there are plenty of threads on the subject. Use google and/or the forum search facility to find them.
    But the answer to your question, as you asked it, is "you can't do that".

Maybe you are looking for

  • Possible to update gui with new thread AND halt program execution?

    Hello, my problem is the following: I have a JButton that performs a sql query and retrieves information from a database, while this is done I would like to update a JLabel with information that the query is in progress. I have got this working but t

  • Frustrated with DATA TAGS (JDEVELOPER TEAM)

    I have been trying to delete a record from an HTML form using data tags and unfortunately I am unable to make any progress. It keeps telling me that I got to use either "rowkey" or "rowkeyparam" and when I try to use it, it gives me a jsp exception.

  • Date validation before submission

    I have a form that requires the user to enter a date. It is a required field, but now they are asking for some additional validation. Im using Access, DWMX and ASP. What i need is some sort of validation that the date that is entered into the field i

  • Licensing and packaging for Netweaver with focus on CE.

    Hello As far as I know SAP always agree licensing with the SAP Customers itself. But is there a possibility to find out at least how they package Neatweaver. For example which standard package they have and what does it include. Our Customer has the

  • Facing crw32.exe error in crystal report because of page size of mainreport

    I have developed a crystal with main report which includes a subreport in the details section. Main report is for header details of invoice and subreport is for line item details. Page design is like report header page header details a- subreport det