Introducing JCli – A command line tool for creating Java applications

Photo by Miguel Á. Padriñán on Pexels.com

Imagine you could create your java applications from command line using a tool.

Just like you would use ng tool for Angular and npm for node js.

This would save a lot of developer time.

With this in mind I experimented with creating a command line tool called “jcli” (java client) using Java and picocli library.

Below is what the tool is capable of :

First I installed the application in my local machine. You can install from here

After that, I added the installation path to ‘PATH’ environment variable of my Windows machine so that I can execute it from anywhere:

Also I have Java installed and included in the PATH variable to execute java commands from anywhere in my machine.

Let us explore how to use this tool now.

Let us create a java project with two files :

Producer.java

Consumer.java

The producer produces lollypops and the consumer consumes them.

We will declare the main method in the Consumer class so that when the project is run it will display what the consumer consumed on the console.

Here we go:

Creating a project template:

To create a new project template , run :

jcli --create-project lollypopproj

You can also use the short hand flag -new instead of --create-project:
jcli -new lollypopproj

This creates a new java project with src and out folders . src folder to keep the source files and out folder to keep the compiled files.

Creating a package:

Now let us create a package to put our source files.

You need to first navigate into the root folder of the project just created

To create a package use the below command from the project root folder:

jcli --create-package com.lollypop.proj

You can also use the shorthand -cp instead of –create-package:

jcli -cp com.lollypop.proj

Creating a class :

To create a class use the below command with the fully qualified class name (along with the package):

jcli --create-class com.lollypop.proj.Producer.java

You can also use the shorthand -cc instead of –create-class:

jcli -cp com.lollypop.proj.Producer.java

This creates a class like below :

You can edit the class from the root folder by executing the below command:

jcli --edit com.lollypop.proj.Producer.java

You can also use the shorthand -e instead of –edit:

jcli -e com.lollypop.proj.Producer.java

This opens the class in a notepad.You can then enter your code, save and close it.

Let us create the Consumer class as well:

Now our classes are created.

You can also create interfaces the same way using the below command:

jcli --create-interface com.lollypop.proj.MyLollypopInterface.java

Let’s compile them .

Compiling the project:

To compile the project use the below command:

jcli --compile

You can also use the shorthand -c instead of –compile

jcli -c

All the classes got compiled successfully inside out folder:

Now let’s execute the application

Running a project:

To run the project , use the below command (again from the project root folder):

jcli --run --main-class com.lollypop.proj.Consumer

You need to specify the main class using –main-class flag for the tool to identify the class to be run.

Note that the main class name doesn’t have .java or .class attached to it.

You can also use the shorthand -r instead of –run and -mc instead of –main-class:

jcli -r -mc com.lollypop.proj.Consumer

There we are , the lollypops are ready!

A java application created entirely on command line!

You can also delete classes , create any type of file using this tool.

To know the entire options type : jcli –help:

This project is rudimentary and doesn’t support adding libraries etc.

It will be available in future versions.

The tool even if not used alone , can be supplemented with any IDE’s you use , like Eclipse or IntelliJ.

Just open the terminal window and instead of creating packages, classes and interfaces by hand (which takes time), you can create them using jcli.

Creating a class in IntelliJ:

Compiling and executing:

In Eclipse , while creating the Java project choose the output folder as out (by default it is bin) and then you can use the commands :

Java looks cooler with a command line tool , isn’t it?

Download the project from here:

It is available only for Windows now.

You can install it and then add the installed path in your environment variable so that you can execute it from anywhere.

Also , please make sure you have Java installed in your machine and its path is included in the environment variable (as internally the tool uses java commands , it is important that the Java path is added to PATH variable as well)

I have made this open source and the code is available here:

https://github.com/vijaysrj/jcli

Please let me know if you find it useful in the comments.


Posted

in

by

Comments

One response to “Introducing JCli – A command line tool for creating Java applications”

  1. […] for creating Java applications. The implementation was quite easy but I felt the idea exciting ( here ). I created it and posted it on Day […]

Leave a Reply to Day 100 – Wrapping It Up – The Full Stack DeveloperCancel reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading