https://kotlinlang.org logo
Title
k

karelpeeters

09/10/2018, 5:20 PM
Why do you need this?
d

diesieben07

09/10/2018, 5:35 PM
The information is definitely in the class file (https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-4.html#jvms-4.7.10). However I don't think you can get at it using reflection or similar terms. You'd have to get the raw class file bytes and read it using something like ASM... Not very practical.
c

christophsturm

09/10/2018, 6:21 PM
thanks. i could just read the class file
n

Nikky

09/10/2018, 6:22 PM
try this:
val f = File(MyClass::class.java!!.getProtectionDomain().getCodeSource().getLocation().getPath())
e

evanchooly

09/10/2018, 6:25 PM
yeah, i was pretty sure that was in there somewhere.
c

christophsturm

09/10/2018, 6:32 PM
oh wow, thanks. i got it working by getting the path from the classfile, and then the filename from the stacktrace element
but that is much simpler
@Nikky i already do this, this gives me the path of the class file
anyway i got it working by taking the name from the stack trace element, so thanks everybody
f

fred.deschenes

09/10/2018, 7:40 PM
you can get it from the
.class
file as others mentioned above, but you'll have to either parse the .class file yourself or use a library that does it for you (Apache BCEL has that, I'm sure most other bytecode manipulation libs also do). BCEL implementation is here if you're curious : https://github.com/apache/commons-bcel/blob/trunk/src/main/java/org/apache/bcel/classfile/ClassParser.java
c

christophsturm

09/10/2018, 8:08 PM
interesting!