Over a million developers have joined DZone.

First Iteration — A Command-Line Application: Final

The final part in the command-line app series, looking at Java web technologies.

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Review Code To-Date

We are on our way to developing and exploring various technologies of web applications and web services, but we begin with a simple program that turns into a resuable component. Read the previous section here!

You can review all the code changes that we have created from previous articles, by going here: https://github.com/elicorrales/shape-calc-5/tree/master/shape-calc-p5

A Simple Command-Line App: Requirements

Let's finish our first iteration by building an app that will use the ShapeCalculatorService.

Some criteria (requirements):

  • Our app will be menu-driven.

  • We aren't going to worry too much about bad user-input.

  • We are not going to worry about suppressing any debug statements.

  • Whenever the app runs the calcuations, the Results will be formatted and categorized , and summed. (simple report)

  • A user can :

    • Select a Shape

    • Select a Calculation Type

    • Enter a Dimension

    • Repeat any of the above at any time

Project Setup

Since the idea or goal is to make the Shape Cacluator a re-usuable component, let's create a separate project for our command-line app, that will depend on the current project.

Image title

Open the POM of the shape-calc-app-1, and add the dependency:








There are few other things we need to add to the app project's POM.  Since this is a command-line app, let's try it on a real command-line, and not just inside Eclipse.

We want to create an executable .jar.

To deploy an app externally, perhaps we should stay away from using the IDE's wizards, and use Maven instead.

You can view the other required items in the POM here:


I executed a "shape-calc-app-1 -> Run as ... -> Maven install".

If you take a look at Eclipse's console output, you will notice that it copied some jar files.

Do a refresh (shape-calc-app-1 -> Refresh) , and then drill into the the project folder.

The important thing to note is the "jarDir", and that it contains everything you need to run this command-line app.

Deployment and Test

To deploy our project jar(s), do a right-click on the "jarDir", then "Export...", then:

Image title


Image title

Again, if you started with these articles from the beginning ("A Programming Exercise that Grew into a Web App Client and Web Service"), as an optional activity, I chose to use Cygwin as my command-line, rather than the Windows command-line.

That first article will explain the "why" of the directory path you see in Eclipse's export wizard.  You would replace "the.transporter" with your user name.

Finish the exporting, and we are (almost) ready to try out our app.

(If you chose not to go the cygwin route, you will have your own steps to executing the app)

Cygwin Auto-Complete

Let's open a Cygwin terminal window, which will drop us where we need to be, i.e., our "home" directory.  If you enter a long listing  "ls -l" (similar to "dir" in Windows), you should see a directory entry that contains what we exported.

Image title

Side-note: the "drwxr-xr-x" entry to the left of each directory item tells us that, for example, our "shape-calc-app-1" is a directory (d), and that the owner of that directory has read(r), write(w), and execute(x) permissions for that directory. It tells us that anyone in our "group" only has read and execute permissions, but not write permission.  And finally, it tells us that all other users have the same permissions as the group.

In order to run our app, we could do the following (try it).

Recall from our first article, I installed the JDK 1.8 at C:\jdk1.8.0_101.  In cygwin terms, this would be "/cygdrive/c/...", etc.

In the cygwin terminal, begin typing a "/" , then tab-tab. You will observe that our bash shell is helping us auto-complete. After you arrive at "/cygdrive/c/", type a "j", then tab, and so on until you arrive at the "java.exe".  (I chose the one in the "jre" directory of our JDK).

Image title

And then to the end of that line, add "-jar "  and then auto-complete to the executable jar we want to test:

Image title

Hit <ENTER>.

You should see several DEBUG statements fly by as the spring framework does its job, until finally we see the output of our app's starting code.  Hitting <ENTER> again, and you should see a menu:

Image title

And you can play with that.

Improve Our Experience

However, let's improve things a bit.

  1.  I don't want to keep having to always type that long line, just to get to the "java.exe".

  2. I don't want to see all those DEBUG statement.

So let's quit out of the app, and we're back at the command-prompt.

Simplify Command

Enter a "ls -la" .  Notice there's an "a".   That means "all". That means hidden files.  Files that begin with a period.

You should see a file called  ".bash_profile".   We need to edit that file.  You can figure out your own way to do that, or you can install "vi" via the cygwin package installer (same setup exe that you used to install cygwin), or maybe you could use gVim (see our first article).

I chose to use "vi", so I entered  "vi .bash_profile" at the prompt.

Once inside the file, I did a search for "bin".   In vi, you would type "/", which at the bottom of you window, enables a single-line command area of vi.  Specifically in this case, it is the search mode.  Then I typed "bin" after the "/", and hit <ENTER>.

Vi will take me to the first mention of "bin" in the file.  If there are other entries, then you can hit the "n" for "next-entry", and you can do "N" for "previous-entry".

We want to arrive at the following block:

Image title

In your case, that block may be commented out (has "#" at the start of each line).

You should remove the "#" of each line of that block.

What you now have is that whenever bash is helping you search for executables to run, it will also search in your local bin directory.

I will not go into how to edit in vi, other than to say you have to go into "insert" mode, and then "escape" out of it.

Go ahead and save and exit vi, by typing  ":x" (save and exit).

We are back at the prompt.   Now we need to make sure we have our own local "bin" directory.

And once we do, we are going to create a symbolic link (similar to Window short-cut) from "java" in our local bin, to the real java.exe.  Here are the steps:

  • mkdir bin <ENTER>

  • cd bin <ENTER>

  • ln -s /cygdrive/c/jdk1.8.0_101/jre/bin/java.exe java <ENTER>

However, for this to work, you will need to "exit <ENTER>" the cygwin terminal, and start it again. Now you should be able to type (below) instead of what we did before:

java -jar shape-calc-app-1/jarDir/shape-calc-app-1.jar

Reduce / Turn Off Loggging

Ok, let's remove all that annoying logging.

Open the shape-calc-app-1 project (and NOT the component shape-calc-p5 project), and add a logback.xml file in the resources:

Image title

The file contents should be something as so:

<?xml version="1.0" encoding="UTF-8"?>

<configuration debug="false">

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>

  <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>

  <root level="OFF">          
    <appender-ref ref="STDOUT" />
    <appender-ref ref="STDERR" />


Save the file, do a refresh of the project in Eclipse, do a "Run as..." -> "Maven clean", followed by "Maven install"...   then perhaps another refresh.

Go to the the target folder, find the jarDir, and export it to your cygwin environment as we did previously.

Re-run the app, and this time should you might see only 1 logging statement, and that is good enough for us for now.

Let's move on.

Review Changes

You can review all the code changes by going here:


Plans for Second Iteration

For our second app iteration, we want to introduce persistence, take a look at JPA, Hibernate, MySQL, etc...

Stay tuned for the next article!

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

java 8,command line interface

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}