I'm attempting to retrieve the source code of any class (if available) from within a Java program for debugging purposes. Let's say I have the Class[_]
's reference to which I would like to retrieve the source code.
What I have attempted so far - in Scala:
val clazz = classOf[ClassDefinedInSeparateFile]
clazz.getProtectionDomain.getCodeSource.getLocation.toString + "/" +
clazz.getPackage.getName.replaceAll("\\.","/") + "/" + clazz.getSimpleName + ".scala"
- looks OK, the JAR is there and contains the .scala
file, but could not open using Source.fromFile(...)
."/" + clazz.getPackage.getName.replaceAll("\\.","/") + "/" + clazz.getSimpleName + ".scala"
- looks OK, but could not open using Source.fromInputStream(...)
Remarks:
.java
or .scala
files, therefore a decompiler is not necessary. (At least for the source code of the application, but not the dependencies. If a snippet is accessible of the application source code, that is enough - most exceptions are caught at the application level and relevant there.)Thanks.
You can use a decompiler to do so. One of the most major ones is JD-GUI. JD-Core is a library that reconstructs Java source code from one or more “. class” files.
The IDE's built-in Source Editor enables you to view, create, and edit your Java source code. Open the Source Editor window by double-clicking a node in the Projects window, Files window, or Navigator window. Alternatively, you can open the Source Editor by choosing File > New to create a new file.
In the standard Java development environment, Java source code, binaries, and resources are stored as files in a file system, as follows: Source code files are saved as . java files. Compiled Java binary files are saved as .
The source code file has file extension ". java". This is the file that is converted into the Java bytecode file, also called the class file. Everything that you physically code is "source code".
If the source is inside the jar, which is in classpath, you need to find out where it is exactly.
clazz.getName.replaceAll("\\.", "/") + ".scala"
is an ok guess, but: (1) source code may not be in the same place as the classes - there could be a prefix (like src/
or whatever), or it could even be in a different jar, and (2) scala classes do not have to be in files with the same name - you can have several classes in one file, the file can be called foo.scala
, some classes are generated on the fly etc. Also, a package is not always a directory in scala (it could be a package object for instance).
If you know the location inside the jar (and the jar is in the classpath), the way to open it is: clazz.getClassLoader.getResourceAsStream
... but, like I said above, the trick is to figure out the location. It is not easy (and there is no single standard way to do it).
Your best bet is indeed to use an IDE. I understand that you don't have it available in production environment, but you don't really need that. What you need is the production source code available on some machine where you have an IDE, and that you can achieve with a simple scp
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With