Skip to content

Instantly share code, notes, and snippets.

@FauxFaux
Created January 24, 2019 09:31
Show Gist options
  • Save FauxFaux/6b12d792dc2ae6e12eb0801b590d2e6e to your computer and use it in GitHub Desktop.
Save FauxFaux/6b12d792dc2ae6e12eb0801b590d2e6e to your computer and use it in GitHub Desktop.
Java source to .class mapping
// Here's a .java file, named 'Yellow.java'
// Inside a package (and directory) named 'green'
package green;
// Here's a class named 'Blue'
class Blue {
// Here's a method named 'foo' inside 'Blue'
void foo() {
// here's an anonymous class declaration, extending java.lang.Runnable
new Runnable() {
// Here's a method named 'run' inside this anonymous class
public void run() {
}
};
}
}
# our source file is in a package (directory)
% find
.
./green
./green/Yellow.java
# compile, not including debug information (it's a release!)
% mkdir -p output && javac -d output -g:none green/Yellow.java
# we've written some .class files...
% find
.
./green
./green/Yellow.java
./output
./output/green
./output/green/Blue$1.class
./output/green/Blue.class
# put them in the jar
% jar cf prod.jar -C output .
# done!
% jar tf prod.jar
META-INF/
META-INF/MANIFEST.MF
green/
green/Blue$1.class
green/Blue.class

green/Blue.class contains green.Blue:

% javap -c output/green/Blue.class | head -n1
class green.Blue {

green/Blue$1.class contains green.Blue$1:

% javap -c 'output/green/Blue$1.class' | head -n1 
class green.Blue$1 implements java.lang.Runnable {

The path is irrelevant, it still knows what it is called:

% cp output/green/Blue.class Pink.class
% javap -c Pink.class
class green.Blue {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment