JavaRush /Java Blog /Random EN /Coffee break #36. 6 Useful Command Line Tools a Java Deve...

Coffee break #36. 6 Useful Command Line Tools a Java Developer Should Know. Jbang: A useful tool for testing Java libraries

Published in the Random EN group

6 Useful Command Line Tools Java Developers Should Know

Source: Technotification Java is one of the most popular programming languages. One of the reasons for such a high attention to this language is that the Java Runtime Environment allows you to run Java applications on different platforms. You can run and compile, including from the command line. Using command line tools, a developer can simplify the process of creating applications and performing many other tasks. Coffee break #36.  6 Useful Command Line Tools a Java Developer Should Know.  Jbang - Creating Shell Scripts for Java - 1Today I'm going to talk about six useful command line tools that a Java developer should know.

Java statistics aggregator (jstat)

The Java statistics aggregator or the jstat command is used when there is a problem with application startup speed. This is a utility tool that displays Java Virtual Machine (JVM) performance statistics. Use it when you need to know the heap size or the JRE's automatic garbage collection algorithm.

Java Dependency Manager (jdeps)

Jdeps is a Java class dependency analyzer useful for quickly determining the static dependencies of applications and libraries. Jdeps also offers options for a newer and preferred API that you can use to replace your existing version.

Java Compiler (javac)

Javac is one of the most useful command line tools for developers. It is part of the JDK from the first version and is included in the assembly of many popular IDEs. Javac is used to troubleshoot any issues that arise during the build and deployment process of the project.

Java profiler (javap)

It cancels the compilation, separates the class files and shows what's inside them. It is also called a disassembler (Java Class File Disassembler). You can use Javap to find out how a particular Java statement works. Also using the profiler you can see the methods available in the class if you don't have access to the source code.

Java Archive Utility (jar)

The Java Archive utility or jar command is used to create a compressed archive file. This is another useful tool that many developers use regularly. Jar can also come in handy when you want to compare one code release version with another, or provide an entry point for embedded JARs or other executables.

JVM Process Status Tool (jps)

This tool works independently of the operating system and provides a convenient way to determine the process ID (PID). Suppose you have run a program and want to link it to Java Memory-Map (jmap). To do this, you need a PID, and this is where jps comes in. In most cases, it is useful to use a combination of the "-mlv" flags. It prints out the main method arguments, the fully qualified package name arguments passed to the JVM.

Conclusion

These are just a few of the useful command line tools that every Java developer should know. Over time, almost all the tools used in creating software in Java have improved significantly. But many developers still rely on the command line for important tasks. Also, if you are a beginner, it is always recommended to have at least a basic understanding of popular command line tools. This will help you not only in your studies, but also in your career.

Jbang: A useful tool for testing Java libraries

Source: DZone Whenever a new Java library comes out, developers are excited to check it out. But sometimes it can be difficult due to problems with setting up the project in the IDE or Maven. Of course, classpaths can also be written on the command line, but for some lazy developers, this is too cumbersome. And this is where jbang comes to the rescue ! With it, problems with setting up a project to test a library or running a Java file will disappear! Coffee break #36.  6 Useful Command Line Tools a Java Developer Should Know.  Jbang - Creating Shell Scripts for Java - 2Of course, Java has JShell, but jbang seems to be better in many ways. Jbang is a neat little tool written by Max Rydahl Andersen (maxandersen) that:
  • allows you to run java files as a script anywhere - with Java 8+ or jshell;
  • helps to try out any Java API without having to install, configure or configure a project for Maven, Gradle or any other build system.
Short list of jbang features:
  • Supports Java 8 or higher, but Java 11 is recommended;
  • Dependency declarations via //DEPS group:artifact:versionor @Grabannotations;
  • Compile and run options with and ;//JAVAC_OPTIONS//JAVA_OPTIONS
  • Supports Windows, Linux and Mac;
  • Running a JavaFX application on Java 8 and above without the need for recoding;
  • And much more...
Now let's see what are the advantages of jbang. To do this, take a good library - Jsoup. It's a Java library that provides very handy APIs for getting URLs, extracting and manipulating HTML content using DOM methods and CSS selectors or, more simply, jQuery-like syntax for manipulating the DOM. First, we will need to run the class jbangusing jbang init hellojsoup.java. This will create hellojsoup.javaa file with hellojsoupa class and a main method. At the top of the file there are also commands for Unix !shebangand DEPSdeclaration. This declaration is a comment where we specify Gradle-like format dependencies - groupId:artifactId:version. In our case it will beorg.jsoup:jsoup:1.13.1. Here is the code that does a simple Wikipedia call and displays the headlines in the news section. This is a slightly modified version provided on the Jsoup site.
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.jsoup:jsoup:1.13.1
import static java.lang.System.*;
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;
public class hellojsoup {
        public static void main(String... args) throws Exception {
              Document doc = Jsoup.connect("https://en.wikipedia.org/").get();
              out.println(doc.title());
              Elements newsHeadlines = doc.select("#mp-itn b a");
              for (Element headline : newsHeadlines) {
                    out.println(String.format("%s\n\t%s", headline.attr("title"),                     headline.absUrl("href")));
          }
     }
}
To run the code, you will need to enter the following command, where run is an optional command.
jbang run hellojsoup.java
When you run this program, the headlines of the day will be displayed in the news section. At the time of this writing, the following titles have been received. Shell
Wikipedia, the free encyclopedia
2020 Twitter bitcoin scam
https://en.wikipedia.org/wiki/2020_Twitter_bitcoin_scam
2020 Armenian?Azerbaijani skirmishes
https://en.wikipedia.org/wiki/2020_Armenian%E2%80%93Azerbaijani_skirmishes
C/2020 F3 (NEOWISE)
https://en.wikipedia.org/wiki/C/2020_F3_(NEOWISE)
Portal:Current events
https://en.wikipedia.org/wiki/Portal:Current_events
Deaths in 2020
https://en.wikipedia.org/wiki/Deaths_in_2020
Wikipedia:In the news/Candidates
https://en.wikipedia.org/wiki/Wikipedia:In_the_news/Candidates
The great thing about jbang is how simple and lightweight it is. It is needed not just for testing libraries, but also as a tool that will be widely used in the production environment in the future, especially in microservices and serverless applications. No complicated server setup, just one single Java file that does the job just fine. Jbang supports vertx, undertow, JavaFX, resteasy, ratpack, all major IDEs including IntelliJ IDEA, and many other technologies. If you want to learn more about jbang and see its documentation, visit the tool's GitHub page . Jbang looks very promising, so I wouldn't be surprised if it appears in the JDK in the future!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION