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 :-
- Learn and understand how to run Java
- Learn and understand how to write a basic Java program
- See how DNS lookups work in Java
- Watch out for an UnknownHost Exception exception within my DNS environment and find out if there are errors
- Log the output to a file and STDOUT
- Notify me in case an error occurs - I’m using Slack and incoming-webhooks to send notifications to a channel of my chosing
- 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
-
You want to install two packages
Java
andJava-devel
based on your distrubition. For RHEL/CentOs like machines this becomessudo yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
-
Verify your java version and java compiler version (
javac
) like thisjava -version
javac -version
How to write a basic Java program
- Make a file called
HelloWorld.java
in a directory andcd
to there Add whatever you want to in the file , say
-
Compile it using the java compiler -
javac HelloWorld.java
; this will create a.class
file -
Verify and run the compiled class file as -
java HelloWorld
-
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
-
Create the JAR file
jar cvfm HelloWorld.jar manifest.mf *.class
-
Make the JAR executable
chmod 744 HelloWorld.jar
-
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
orscreen -d <screen ID>
to find the id, doscreen -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!
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