-3

I am not trying to get the working directory or the .class file path, instead I want the path of the source code that is running.

something like "C:\Workspace\ProjectName...\src\test\java...\MyFile.java"

Any ideas?

Edit : There are answers on SO which tell how to obtain the absolute path of generated .class file, The purpose of this question is to know if there are ways to get location of original source file (.java file) dynamically. Thanks.

  • 4
    There is no `.java` file at runtime. Your question doesn't make sense. Please clarify your goal. – Sotirios Delimanolis Nov 30 '15 at 20:34
  • You do not run ".java". You run compiled ".class" files. – PM 77-1 Nov 30 '15 at 20:35
  • Stacktrace gives you the classname. Except for inner classes that should also be the filename. But I doubt an absolute path of source is retrievable from running code. – Fildor Nov 30 '15 at 20:35
  • @Fildor yes I am trying to retrieve the path of the source file. And not the compiled (.class) file – user3803413 Nov 30 '15 at 20:41
  • 1
    Hm. I am hesitant to say "impossible" but ... what makes you want to do this. I smell an x-y-problem. – Fildor Nov 30 '15 at 20:44
  • That information is lost. There is no relation between a `.class` file and a `.java` file that was used to generate it. – Sotirios Delimanolis Nov 30 '15 at 20:44
  • @SotiriosDelimanolis Thanks. – user3803413 Nov 30 '15 at 20:46
  • @Fildor That was the first step, I am trying to implement something like grep in my code, if I knew the parent directory(One level up), I could go search other java files in that directory for a particular string.I will run those files which has that string. – user3803413 Nov 30 '15 at 20:48
  • I do have a workaround though, but a complicated one. I was just curious if getting the path of a java file directly is possible. – user3803413 Nov 30 '15 at 20:52
  • That sounds pretty complicated. What do you want to achieve with this procedure? Is it just any string or will it be in method names, only? I suspect in the end reflection is what could be a solution for you. Not in the sense to do what you ask here, but in the sense of achieving your overall goal. – Fildor Dec 01 '15 at 07:59

1 Answers1

1

There is an optional attribute stored in the ".class" file that gives the filename (not pathname) for the source Java file. JVM spec 4.7.10.

The spec says:

"The string referenced by the sourcefile_index item will be interpreted as indicating the name of the source file from which this class file was compiled. It will not be interpreted as indicating the name of a directory containing the file or an absolute path name for the file; such platform-specific additional information must be supplied by the run-time interpreter or development tool at the time the file name is actually used."

This attribute is not exposed by java.lang.Class, but if you can (somehow) arrange for an exception to be instantiated while a method of the class is on the stack, the source filename may be obtainable by calling StackTraceElement.getFileName() ... modulo the disclaimers in the javadoc.

Alternatively, if you can locate the class file, it should be possible to decode it and extract the source filename. (You should be able to find an existing library 3rd-party to do the decoding / extraction.)

HOWEVER:

  • As stated above, what you get is a simple filename, not a pathname.
  • The name is not necessarily a real filename. It could be the name of something in a repository / database that contains the source. (According to the javadoc disclaimers.)
  • The attribute is optional which means that it may not be present at all.

All of that adds up to this: you probably should find another way to do whatever it is that you are trying to do.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216