Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get source code of any class from within a Java program

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]

  1. 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(...).
  2. "/" + clazz.getPackage.getName.replaceAll("\\.","/") + "/" + clazz.getSimpleName + ".scala" - looks OK, but could not open using Source.fromInputStream(...)

Remarks:

  • There is no IDE available in production or staging environments.
  • In our setting JARs contain the source code .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.

like image 246
Dyin Avatar asked Dec 12 '18 18:12

Dyin


People also ask

Can we get Java code from class file?

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.

How can I see the code of a Java program?

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.

Where is source code stored in Java?

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 .

Is .Java or .class the source file?

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".


1 Answers

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.

like image 127
Dima Avatar answered Sep 27 '22 20:09

Dima