How to Compile and Run Java Code from a Command Line

Being spoiled by IDEs and automated building tools I recently realized that I don’t know how to run java code from a command line anymore. After playing a guessing game for an hour trying to compile a simple piece of code that took 5 minutes to write, I thought maybe it’s time to do a little research.


Lets say we have a fairly standard java project that consists of three top level folders:

/bin – empty folder that will contain compiled .class files

/lib – contains third party .jar files

/src – contains .java source files

Our task would be to compile and launch the project from its root folder. We will use Windows OS as example (on Unix systems the only difference would be path separation symbol – ":" instead of ";").

Compiling Java Code

The first step is compiling plain text .java sources into Java Virtual Machine byte code (.class files). This is done with javac utility that comes with JDK.

Assuming we are at the application root folder trying to compile file from com.example package that uses lib1.jar and lib2.jar libraries from lib folder to a destination bin folder, compilation command should have the following format:

javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/

As a result bin/com/example/Application.class file should be created. uses other classes from the project, they all should be automatically compiled and put into corresponding folders.

Running Java Code

To launch a .class file we just compiled, another JDK utility called java would be needed.

Assuming we are at the application root folder trying to launch Application.class file from com.example package that uses lib1.jar and lib2.jar libraries from lib folder, the launch command should have the following format

java -cp bin;lib/lib1.jar;lib/lib2.jar com.example.Application

Note that we don’t provide a filename here, only an actual class name that java would attempt to find based on provided classpath.

Some Notes About Classpath

Lets say during compilation a compiler stumbles upon somecom.example.Util class. How to find it in the file system? According to Java file naming rules,Util class has to be located somewhere in file under /com/example/ folder, but where to start searching for this path? Here is where classpath comes into play which sets the starting folder for searching for classes. Classpath can be set in 3 different ways:

  • If no --classpath parameter is passed, CLASSPATH environment variable is used
  • If CLASSPATH environment variable is not found, current folder (".") is used by default
  • If --classpath is explicitly set as a command line parameter, it overrides all other values

The fact that classpath when set overrides default value (current folder) can cause some unexpected results.

For example if we don’t use any third party libraries, only our own com.example.Utilclass, and try to compile from the src folder:

javac com/example/

this would work, but then if we decide to add a third party libarary to the classpath:

javac -cp lib/lib1.jar com/example/

it would cause an error:

package com.example.Util does not exist

This happens because when we set -cp lib/lib1.jar we override default value for the classpath – current folder. Now a compiler will be looking for all classes only inside that jar file. To fix this we need to explicitly add the current folder to the classpath:

javac -cp .;lib/lib1.jar com/example/

Installing Coldfusion 11 on CentOS/SL 6.x 64 bit

  1. Installing the required C++ Library
  2. Running the ColdFusion installer
  3. Starting ColdFusion for the first time
  4. Adding hostname pointed to localhost
  5. Finish the ColdFusion installation in your browser

1. Install the required C++ Library

There’s one final thing you need to do before running the installer. ColdFusion 8 and 9 require the C++ library for a few features such as custom tags, Web Services and some cfimage functionality. Download and install the library using the package manager built into CentOS.

yum install

There are several ways you can go about getting the installation file for ColdFusion 9. The easiest is probably by logging into your Adobe account and downloading either the 32-bit install file (coldfusion_11_WWE_linux.bin) or the 64-bit install file (coldfusion_11_WWE_linux64.bin). I highly recommend you go with a 64-bit installation as this will allow you to allocate much more RAM to the ColdFusion server than in a 32-bit environment. Of course, this requires you have a 64-bit version of CentOS installed as well.

2. Make the install file executable and running the installer

Before you can run the installation file you need to make it executable. Navigate to the directory where you placed the file and run the change mod command to make it executable.

cd /install

# For the 32-bit installation
chmod +x coldfusion_11_WWE_linux.bin

# For the 64-bit installation
chmod +x coldfusion_11_WWE_linux64.bin

The install process may take a few minutes to get going depending on your server specs. You’ll be presented with a multi-page license agreement that you have to accept in order to continue the installation. After that, you are presented with installation questions.

After the installation is finished you will see a success screen that tells you to start ColdFusion and run the Configuration Wizard. The wizard isn’t something you have to run specifically, as the first time you launch the ColdFusion Administrator the wizard will run for you. You are given a URL for the ColdFusion Administrator:


3. Starting the coldfusion for first time

Change the directory to coldfusion install location

cd /opt/coldfusion11/cfusion/bin

and issue

./coldfusion start

4. Adding hostname pointed to

You might end into error 500.

Issue this as root

echo “ “`hostname` >> /etc/hosts

Then restart the coldfusion. It worked in my case.

5. Finish the ColdFusion installation in your browser

Open your favorite browser and copy/paste the following into the address bar. Change machinename to the IP address of your server. A local IP such as 10.x.x.x or 192.x.x.x will work if you are connected to a server in your office or you are connected to an external server via VPN. You might also be able to use the external IP of the server. Or, if the server is already hosting a domain name, you could change machinename to

After loading this URL you should see a ColdFusion-branded Configuration and Settings wizard screen with a password prompt. Enter the password for the ColdFusion Administrator you created during installation and press enter. ColdFusion will do a few things and then show a new screen with an okay button. Press the button to go straight to the main screen of the ColdFusion Administrator.

SOAP request using Lua

Lua is a short and simple language exposing the power of C in clean syntax. In recent project I had to make lua talk to SOAP Server on a payment gateway. I was able to do this successfully. The lua code is based on LuaSoap. You can down the full sample code from here. It required three libraries to be built on the *nix system before the actual soap request is made. Happy tweaking with Lua.

## Dependencies

LuaSOAP version >= 3.0.0 (
LuaExpat version >= 1.3.0 (
LuaSocketgit version >= 3.0-rc1 (
LuaSec version >= 0.4.1 (

The libraries are included in the lib folder. Please note the luasec-0.5.0 do not work on Centos environment.

## Package Dependencies:
Ubuntu:lua-dev, libexpat-dev, openssl-devel
CentOS: lua-devel, expat-devel, openssl-dev

## Compile Instructions

The libexpat can be built using the `make`. Upload the libexpat folder on your developement server and issue `make` command.

The luasocket can be built and installed using make. Upload the luasocket library on your development machine and issue `make {PLATEFORM}` and then `make install`. Please note the path of CDIR - path to *.so files and LDIR - path to lua files. These need to be updated in the lua script

The luasec can be built using the make. Upload the libsec-0.4 folder on your developement server and issue `make` command

## Path Lua and C Libraries

The correct path of these libraries (Lua & C) needs to be set in the run.lua for them to work. 


-- Agms Module to interact with the Agms Gateway

-- @author: Maanas Royy
-- @copyright: Avant-Garde Marketing Solutions, Inc.

agms = {}

 -- Request Object
 agms.Request = {}
 agms.Request.TransactionType = ""
 agms.Request.GatewayUserName = ""
 agms.Request.GatewayPassword = ""
 agms.Request.PaymentType = ""
 agms.Request.Amount = ""
 agms.Request.Tax = ""
 agms.Request.Shipping = ""
 agms.Request.OrderDescription = ""
 agms.Request.OrderID = ""
 agms.Request.PONumber = ""
 agms.Request.CCNumber = ""
 agms.Request.CCExpDate = ""
 agms.Request.CVV = ""
 agms.Request.CheckName = ""
 agms.Request.CheckABA = ""
 agms.Request.CheckAccount = ""
 agms.Request.AccountHolderType = ""
 agms.Request.AccountType = ""
 agms.Request.SecCode = ""
 agms.Request.FirstName = ""
 agms.Request.LastName = ""
 agms.Request.Company = ""
 agms.Request.Address1 = ""
 agms.Request.Address2 = ""
 agms.Request.City = ""
 agms.Request.State = ""
 agms.Request.Zip = ""
 agms.Request.Country = ""
 agms.Request.Phone = ""
 agms.Request.Fax = ""
 agms.Request.EMail = ""
 agms.Request.Website = ""
 agms.Request.ShippingFirstName = ""
 agms.Request.ShippingLastName = ""
 agms.Request.ShippingCompany = ""
 agms.Request.ShippingAddress1 = ""
 agms.Request.ShippingAddress2 = ""
 agms.Request.ShippingCity = ""
 agms.Request.ShippingState = ""
 agms.Request.ShippingZip = ""
 agms.Request.ShippingCountry = ""
 agms.Request.ShippingEmail = ""
 agms.Request.ShippingPhone = ""
 agms.Request.ShippingFax = ""
 agms.Request.ProcessorID = ""
 agms.Request.TransactionID = ""
 agms.Request.Tracking_Number = ""
 agms.Request.Shipping_Carrier = ""
 agms.Request.IPAddress = ""
 agms.Request.Track1 = ""
 agms.Request.Track2 = ""
 agms.Request.Track3 = ""
 agms.Request.Track_Type = ""
 agms.Request.Custom_Field_1 = ""
 agms.Request.Custom_Field_2 = ""
 agms.Request.Custom_Field_3 = ""
 agms.Request.Custom_Field_4 = ""
 agms.Request.Custom_Field_5 = ""
 agms.Request.Custom_Field_6 = ""
 agms.Request.Custom_Field_7 = ""
 agms.Request.Custom_Field_8 = ""
 agms.Request.Custom_Field_9 = ""
 agms.Request.Custom_Field_10 = ""
 agms.Request.SAFE_Action = ""
 agms.Request.SAFE_ID = ""
 agms.Request.ReceiptType = ""
 agms.Request.MICR = ""
 agms.Request.MICRSymbolSet = ""
 agms.Request.CheckFrontTIFF = ""
 agms.Request.CheckBackTIFF = ""
 agms.Request.CheckNumber = ""
 agms.Request.Terminal_ID = ""
 agms.Request.CCNumber2 = ""
 agms.Request.Clerk_ID = ""
 agms.Request.Billing_Code = ""
 agms.Request.InvoiceID = ""
 agms.Request.BatchID = ""
 agms.Request.DLNumber = ""
 agms.Request.DLState = ""
 agms.Request.IdentityVerification = ""
 agms.Request.CourtesyCardID = ""
 agms.Request.MagData = ""

 -- Response Object
 agms.Response = {}
 agms.Response.STATUS_CODE = ""
 agms.Response.STATUS_MSG = ""
 agms.Response.TRANS_ID = ""
 agms.Response.AUTH_CODE = ""
 agms.Response.AVS_CODE = ""
 agms.Response.AVS_MSG = ""
 agms.Response.CVV2_CODE = ""
 agms.Response.CVV2_MSG = ""
 agms.Response.ORDERID = ""
 agms.Response.SAFE_ID = ""
 agms.Response.FULLRESPONSE = ""
 agms.Response.POSTSTRING = ""
 agms.Response.BALANCE = ""
 agms.Response.GIFTRESPONSE = ""
 agms.Response.MERCHANT_ID = ""
 agms.Response.CUSTOMER_MESSAGE = ""

 -- AgmsAPI method
 agms.AgmsAPI = {}
 agms.AgmsAPI.url = ""

 function agms.AgmsAPI.ProcessTransaction (request)

 -- Fabricate the SOAP Request Body
 local request_body = {tag = "objparameters"}
 for key, val in pairs(request) do
 if val ~= "" then
 table.insert(request_body, { tag = key, val})
 -- Soap Call, Important part add the namespace
 local ns, meth, ent =
 {url = agms.AgmsAPI.url,
 namespace = "",
 soapaction = "",
 method = "ProcessTransaction",
 entries = {request_body}
 return ns, meth, ent

return agms


-- Lua code to interact with the Agms Gateway

-- @author: Maanas Royy
-- @copyright: Avant-Garde Marketing Solutions, Inc.

-- Define package path to load libraries
package.path = './lib/luasoap-3.0/src/?.lua;' .. package.path -- luasoap
package.path = './lib/luaexpat-1.3.0/src/?.lua;' .. package.path -- luaexpat
package.path = '/usr/local/share/lua/5.1/?.lua;' .. package.path -- luasocket
package.path = './lib/luasec-0.4.1/src/?.lua;' .. package.path -- luasec

-- Define package path to load C libraries
package.cpath = './lib/luaexpat-1.3.0/src/?.so;' .. package.cpath -- luaexpat
package.cpath = '/usr/local/lib/lua/5.1/?.so;' .. package.cpath -- luasocket
package.cpath = './lib/luasec-0.4.1/src/?.so;' .. package.cpath -- luasec

-- Import agms 
local agms = require "agms"
-- Import helper function to print lua table
local print_r = require "helper"

-- Import LuaSoap
agms.soap_client = require "client"
-- Import SSL, Version 0.4 require you to call https before we can actually load ssl.https.
-- It exports module as ssl.https
require "https"
local https = require "ssl.https"

-- Assign the https protocol in SOAP client
agms.soap_client.https = https

-- Prepare Request
agms.Request.GatewayUserName = "##########";
agms.Request.GatewayPassword = "##########";
agms.Request.TransactionType = "sale";
agms.Request.PaymentType = "creditcard";
agms.Request.Amount = "1.00";
agms.Request.CCNumber = "4111111111111111";
agms.Request.CCExpDate = "0120";
agms.Request.CVV = "123";

-- Debug request
-- print_r(agms.Request)

local ns, meth, ent = agms.AgmsAPI.ProcessTransaction(agms.Request);
print("namespace = ", ns, "element name = ", meth)
for i, elem in ipairs (ent[1]) do
 print (elem['tag'], '=', elem[1])

Install Oracle Java 8 (JDK 8u25) on CentOS/RHEL 6/5 and Fedora

Step 1: Download JAVA Archive

Download latest Java SE Development Kit 8 release from its official download page or use following commands to download from shell.

For 64Bit

# cd /opt/
# wget --no-cookies --no-check-certificate --header "Cookie:; oraclelicense=accept-securebackup-cookie" ""

# tar xzf jdk-8u25-linux-x64.tar.gz

For 32Bit

# cd /opt/
# wget --no-cookies --no-check-certificate --header "Cookie:; oraclelicense=accept-securebackup-cookie" ""

# tar jdk-8u25-linux-i586.tar.gz

Note: If Above wget command doesn’t not worked for you watch this screencast to downloadJDK from terminal.

Step 2: Install JAVA using Alternatives

After extracting archive file use alternatives command to install it. alternatives command is available in chkconfig package.

# cd /opt/jdk1.8.0_25/
# alternatives --install /usr/bin/java java /opt/jdk1.8.0_25/bin/java 2
# alternatives --config java

There are 3 programs which provide 'java'.

  Selection    Command
*  1           /opt/jdk1.8.0/bin/java
 + 2           /opt/jdk1.7.0_55/bin/java
   3           /opt/jdk1.8.0_25/bin/java

Enter to keep the current selection[+], or type selection number: 3

At this point JAVA 8 has been successfully installed on your system. We also recommend to setup javac and jar commands path using alternatives

# alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_25/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_25/bin/javac 2
# alternatives --set jar /opt/jdk1.8.0_25/bin/jar
# alternatives --set javac /opt/jdk1.8.0_25/bin/javac 
Step 3: Check Version of JAVA .

Check the installed version of java using following command.

# java -version 

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

Step 4: Setup Environment Variables

Most of java based application’s uses environment variables to work. Set the java environment variables using following commands

    • Setup JAVA_HOME Variable
# export JAVA_HOME=/opt/jdk1.8.0_25
    • Setup JRE_HOME Variable
# export JRE_HOME=/opt/jdk1.8.0_25/jre
    • Setup PATH Variable
# export PATH=$PATH:/opt/jdk1.8.0_25/bin:/opt/jdk1.8.0_25/jre/bin