Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Understanding Your Command Line Tools: Javap, Jdeps, and Jjs

DZone's Guide to

Understanding Your Command Line Tools: Javap, Jdeps, and Jjs

When examining your code, it's helpful to have a disassembler (javap), a dependency analyzer (jdeps), and to invoke the Nashorn engine (jjs). Here's how to use them.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

There are several command line tools available that make a developer's job pretty easy. Still, most of us find ourselves stuck with a relative few tools at our disposal. Here, I will show you three nice command line tools and their uses.

Javap

In short, javap is a class file disassembler. A disassembler is a program that converts machine code into low-level symbolic code. Since this command disassembles the Java class file, we'll call it our Java disassembler. This command basically tells us that what is inside the class file. This command is pretty useful when we work in a restricted zone and aren't allowed to get a decompiler.

Let's see how we can use javap.

The Java file below has three methods, including the main method:

public class JavapDemo {
  static JavapDemo demo = new JavapDemo();

  public static void main(String[] args) {
    demo.sayHi();
    demo.sayBye();
    demo.sayPrivateBye();
  }

  public void sayHi(){
    System.out.println("Hi");
  } 
  public void sayBye(){
    System.out.println("Bye");
  }
  private void sayPrivateBye(){
    System.out.println("Bye");
  }
}


Let's compile the above code using the javac command. Once the compilation is done, we can use the class file as an input to the javap command with various available options.

Let's use the -c option to disassemble the JavapDemo.class file:

Image title

You can see three public methods' information in the above snapshot, but private method info is not present.

To see private method info as well, you can use the -p option.

Image title


Jdeps

Most of the time, we keep adding JARs to our projects. Eventually, later on, it's common that a few of them are sitting idle, still going into production because there hasn't been a dependency check. In this case, jdeps is a nice tool to help with that.

This command line tool is very helpful to figure out Java class dependencies. It's known as the Java class dependency analyzer. This tool can be used with various available options.

In the example below, we'll analyze the Test.jar file at the package level:

Image title

If you want to analyze test.jar at the class level:

Image title


Jjs

Jjs is a command line tool to invoke the Nashorn engine. This can be used to interpret a list of script files or to run an interactive shell.

Here we will run the below JavaScript file:

TestFunction.js

function testFunction() {
    return 5;
};

print(testFunction());


Now let's run the command:

Image title

Running in an interactive shell, as below:

Image title

That's all! I hope this added a nice tool or two (or three) to your kit. Enjoy!

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,command line tools ,javap ,jdeps ,jjs ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}