white-turtle

web-space of - vishal basra

turtles and code!


Java 101

Hi!

In this post I’ll be trying to write about how I learned to write Java and my first Java program.

My use-case :-

  1. Learn and understand how to run Java
  2. Learn and understand how to write a basic Java program
  3. See how DNS lookups work in Java
  4. Watch out for an UnknownHost Exception exception within my DNS environment and find out if there are errors
  5. Log the output to a file and STDOUT
  6. Notify me in case an error occurs - I’m using Slack and incoming-webhooks to send notifications to a channel of my chosing
  7. Run this program in a way that isn’t super shitty

DISCLAIMER This is in no ways a perfect program and neither is it 100% complete, this is how I learnt to figure out DNS related errors in my environment from Java services

How to install Java

  1. You want to install two packages Java and Java-devel based on your distrubition. For RHEL/CentOs like machines this becomes sudo yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

  2. Verify your java version and java compiler version (javac) like this

    • java -version
    • javac -version

How to write a basic Java program

  1. Make a file called HelloWorld.java in a directory and cd to there Add whatever you want to in the file , say
public class HelloWorld {

    public static void main(String[] args) {
        // Prints "Hello, World" to STDOUT
        System.out.println("Hello, World");
    }

}
  1. Compile it using the java compiler - javac HelloWorld.java ; this will create a .class file

  2. Verify and run the compiled class file as - java HelloWorld

  3. Make a mainfest file called manifest.mf ; this will tell your application which class to look for as the default echo Main-Class: HelloWorld > manifest.mf

  4. Create the JAR file jar cvfm HelloWorld.jar manifest.mf *.class

  5. Make the JAR executable chmod 744 HelloWorld.jar

  6. Run the test file as :- java -jar /tmp/HelloWorld.jar

How to run a Java program acceptable(ishly) for this test

Do not cron it! Instead, use screen see man screen. To run it in the background :-

  • yum -y install screen

  • screen -l java -jar /opt/java-test/lookupTest.jar >> /var/log/lookupTest-RAW.log ; if [ -e /var/log/Java-DNS-Lookup.log.1 ] ; then rm -f /var/log/Java-DNS-Lookup.log.* ;fi

  • detach that screen control + a + d or screen -d <screen ID> to find the id, do screen -ls

To terminate, find and kill the process

( The last part of removing extra log files is because I don’t know how to code properly. (It’s inefficient, but works ) )

Now there will be two log files that you can assess :-

  • One at /var/log/lookupTest-RAW.log which is the complete standard output of the program

  • The other at /var/log/Java-DNS-Lookup.log which contains a simple status of if the lookup was sucessful or not. (This file is taken care of by the program itself)

Lets get down to business and see how it works!

The code is fairly easy and simple to understand, i’ve hoped that I’ve used enough comments for it to be readable.

See the full code here at my github repo!

Why have I not included my code in this page directly?

Well because natively, I can only imbed gists in Jekyll, and I don’t want to copy code from my repo into a Gist.
I could have just copy-pasted my code here, but I don’t want to do that as that’ll not be source-controlled!

my photo

UnknownHost Exception

What is the UnknownHost Exception?

When a Java program calls the InetAddress library to do a lookup against a DNS name using a method ( like getAllByName ) then it may raise several exceptions.

See the library and the exceptions listed there for each method.

The UnknownHost exception means that the lookup failed and the Java program could not get a valid response. this may indicate

  • Obvious DNS errors in your infra
  • Problems in your resolver DNS servers
  • Network connectivity between your DNS servers and Java
  • Could be a bug in your version ( yeah, unlikely but who knows! )
  • The end of the world?

What do these look like

unknownhostexception exception="com.sun.jersey.api.client.ClientHandlerException: java.net.UnknownHostException: google.com"

vishal@white-turtle.org