Results 1 to 13 of 13
- 02-15-2009, 07:38 PM #1
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
[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:
I hope it's sufficiently clear to be understoodJava 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)
Last edited by raffaele181188; 02-16-2009 at 04:14 PM.
- 02-16-2009, 08:24 AM #2
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
well you can use
you can choose some of them. if that is your question about )))File("file.txt"),
JFileChooser or anchor.class.getResourceAsStream("file.txt")
- 02-16-2009, 10:13 AM #3
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
No, it's not my question. I mean something like
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 thatJava 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(); ... } }
- 02-16-2009, 03:03 PM #4
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?
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-16-2009, 03:20 PM #5
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
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 03:24 PM.
- 02-16-2009, 03:23 PM #6
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.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-16-2009, 03:37 PM #7
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
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
Then MyClass has to reach a file in resources directory. You could haveJava Code:/home/myname/java |__myname |__mypackage |__resources |__tools |__MyClass.class
But that won't work in some cases. Java API says:Java Code:File myResFilenm = new File("../resouces/image.jpeg");
OK?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.
- 02-16-2009, 03:38 PM #8
I won't swear this will work, but try Class.getCanonicalName().
- 02-16-2009, 03:42 PM #9
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
No, it doesn't work: getCanonicalName() returns, for example, java.lang.String
- 02-16-2009, 04:09 PM #10
OK, my bad. That'll teach me to guess. I've done this before, I just couldn't remember how. Here's some code.
Note that getSystemResource actually returns a URL object, which is often much more useful than a String.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; } }
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.
- 02-16-2009, 04:14 PM #11
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
Solved
OK, that's what I meant
I also found this
Java Tutorial for getting images
Thanks everyone
- 02-16-2009, 04:16 PM #12
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.
- 04-16-2009, 07:41 AM #13
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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
-
class and file name
By srikanth in forum New To JavaReplies: 7Last Post: 04-16-2009, 02:03 PM -
Help with bytecode in java
By coco in forum New To JavaReplies: 3Last Post: 04-16-2009, 08:48 AM -
differences between bytecode and executable code
By valery in forum New To JavaReplies: 2Last Post: 04-16-2009, 08:13 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM -
Convert my class file into a exe file
By carl in forum New To JavaReplies: 1Last Post: 08-05-2007, 08:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks