Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default [SOLVED] How to get the .class file which bytecode is in

    Hello folks! I have a question for you
    How could I get the filename of a class? For exemple if I had a class named Foo, how could I get the physical location of that class? The output should be something similar to:
    Java Code:
    Class mypackage.Foo is in:
    /home/myname/java/mypackage/Foo.class (if it is in the filesystem)
    /home/myname/download/myjar.jar    mypackage/Foo.class (if it is in a Jar)
    http://www.somedomain.com/classes/mypackage/Foo.class (on the net)
    I hope it's sufficiently clear to be understood
    Last edited by raffaele181188; 02-16-2009 at 05:14 PM.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    well you can use
    File("file.txt"),
    JFileChooser or anchor.class.getResourceAsStream("file.txt")
    you can choose some of them. if that is your question about )))

  3. #3
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    No, it's not my question. I mean something like
    Java Code:
    public class Foo {
       public static void main(String[] args){
          Bar b = new Bar();
    
          // Now, maybe using reflection, I'd like to know where to find the
          // Bar.class file, and get some information. For example (pseudo code)
          java.net.URL = Bar.class.getFileURL();
          ...
       }
    }
    But in the API for Class or ClassLoader I didn't find such a method. Well, I could use the $CLASSPATH and then the filesystem methods to serch in that path, but it would be very expensive and inelegant. I'm sure there's a standard API code to accomplish that

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    It would be possible to search your system files or jars I guess, but how would you just give a method a file and expect it to find its precise location on the web? Or does initializing Bar give it a URL field or something?
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  5. #5
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    I mean... If my ClassLoader can find the Class object stored in the .class file... why couldn't I get it??? If my JVM can instantiate a Bar object, then it MUST know the physical location of the .class file (or the jar). So I'm simply try to get the same information... I think reflection is the right way, but I can't find the API...
    Last edited by raffaele181188; 02-16-2009 at 04:24 PM.

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Oh... So that example class isn't the Bar class otherwise getting its location would be pointless.. What's really the point of finding the class file though, I can't imagine not knowing where it is if I'm using it, and if I didn't I could search for it.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    Suppose your class _MUST_ know where it is placed. For exemple i am in a package named myname.myapp
    Then MyClass (myname.mypackage.tools.MyClass) has to access the parent directory in which it is located. This is the directory tree
    Java Code:
    /home/myname/java
     |__myname
           |__mypackage
                   |__resources
                   |__tools
                        |__MyClass.class
    Then MyClass has to reach a file in resources directory. You could have
    Java Code:
    File myResFilenm = new File("../resouces/image.jpeg");
    But that won't work in some cases. Java API says:
    A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
    OK?

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I won't swear this will work, but try Class.getCanonicalName().

  9. #9
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    No, it doesn't work: getCanonicalName() returns, for example, java.lang.String

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    OK, my bad. That'll teach me to guess. I've done this before, I just couldn't remember how. Here's some code.
    Java Code:
    package test;
    
    import javax.swing.JOptionPane;
    
    public class ClassLocationTest {
    	public static void main(String[] args) {
    		JOptionPane.showMessageDialog(null, getClassLocation(String.class));
    	}
    	
    	public static String getClassLocation(final Class<?> pClass) {
    		final String location, name;
    		name = pClass.getName().replaceAll("\\.", "/") + ".class";
    		location = ClassLoader.getSystemResource(name).getPath();
    		return location;
    	}
    }
    Note that getSystemResource actually returns a URL object, which is often much more useful than a String.

    Also, I only returned the path portion of the URL, you can play around to figure out what you want. Use getExternalForm(), rather than toString(). toString() just calls getExternalForm().

    A couple of notes.

    The returned path is HTTP escaped, which means spaces and other special characters are replaced by %20 or whatever the code is for the special character.

    If the class is in a JAR file, as in the example code, the path will include the JAR file.

  11. #11
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default Solved

    OK, that's what I meant
    I also found this
    Java Tutorial for getting images

    Thanks everyone

  12. #12
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Please go to the top of this page, click Thread Tools and then in the drop-down menu, click Mark This Thread As Solved so know one comes here thinking there is still a question being asked.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  13. #13
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default Watermark in java class file

    Hi,

    I would like to know how to embed watermark in Java class file and decode that watermark class file to get back the watermark value.

    I will be appreciate if any one can provide the source code.

    I'hv tried many time to get the bytecode of the class file to embed the watermark value but failed. Should i used the class reader to read the class file but where i must to called the class file and how.

    I hope any one can help me immediately because i must to submit it next week.

    Thanks you
    Azura

Similar Threads

  1. class and file name
    By srikanth in forum New To Java
    Replies: 7
    Last Post: 04-16-2009, 03:03 PM
  2. Help with bytecode in java
    By coco in forum New To Java
    Replies: 3
    Last Post: 04-16-2009, 09:48 AM
  3. differences between bytecode and executable code
    By valery in forum New To Java
    Replies: 2
    Last Post: 04-16-2009, 09:13 AM
  4. Replies: 0
    Last Post: 05-13-2008, 07:16 AM
  5. Convert my class file into a exe file
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 09:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •