How can you obtain the file name and directory of a java source code within that actual code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can you obtain the file name and directory of a java source code within that actual code?

28th Sep 2020, 11:10 AM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
4 Answers
+ 6
Aleksandrs Your last comment was like an optical illusion for me. 😂 > "I'm pretty sure that's what he meant. What's the possible reason to look for .java files in runtime?" At first, I was thinking you were saying you believe the OP meant to ask how to "obtain the __class__ name" because it didn't make sense why anyone would want to know the file location for which the application is running from. Then, I immediately thought you were saying something to the effect of, "Ah... I now see what he meant. But why would he want that?" I think my first impression is more accurate... but, it was somewhat comical to me that I saw such ambiguity where it's likely clear to everyone else. 😉 Anyway... perhaps the OP wants to dump a log file to a path that's relative to the application. Or maybe he's just curious. Mind To Machine 💻🕆 That said, I do think you should be aware that you won't be able to "obtain the file name and directory" of the __java source code__ because the application doesn't run from the "source code". Rather, the application can only attempt to identify the path of the compiled binaries loaded into the running process. Also, I'm not sure if the path reflects the physical location of the linked jar files or the application process. My guess is it's the application process.
30th Sep 2020, 6:26 AM
David Carroll
David Carroll - avatar
+ 3
The jar file name is definitely weird. https://code.sololearn.com/cA64na6llC81/?ref=app ---- Output on Playground: ---- - Url: fil../Playground// - Protocol: file - Path: /usercode/ - File: /usercode/ --
30th Sep 2020, 1:34 AM
David Carroll
David Carroll - avatar
+ 1
Like this. It does not work in SoloLearn, but you can copy this code and try in your IDE or using command line. https://code.sololearn.com/cmEyOQIy6y4P/?ref=app
28th Sep 2020, 1:22 PM
Aleksandrs
Aleksandrs - avatar
0
I'm pretty sure that's what he meant. What's the possible reason to look for .java files in runtime?
30th Sep 2020, 5:04 AM
Aleksandrs
Aleksandrs - avatar