Deploying a simple java application

Posted on Posted in deployment, java

Recently, I had the opportunity to work on a project in which I had to develop three apps in java. No fancy stuff (Spring, EE etc.) involved. Since the focus here is more on the deployment therefore I will spare you the intrinsic complexity of the app itself and even simplify the deployment model (taking one app as an example) so that I can put my point across more effectively.

In a nutshell this is a local deployment. The title could alternatively be changed to “How to run java application locally”. Here is a high level view of what is happening:

1- A simple console app

2- Reads a file off the file system

3- Processes the contents

4- Loads the database up with the processed information

 

 

 

 

 

Technical specs:

  • Development technology: Java and related frameworks
  • Destination Platform: Windows
  • Database: Microsoft SQL Server

To prepare the app for deployment, the following items must be considered:

Configuration: Keep the properties external in a java properties file. This enables the application to work in an environment where the following properties could be different.

1- Properties

1.1- sourcefilepath: The complete file path (with the filename). Ex:

sourcefilepath=C:\\Edifact-to-PowerBI\\appa-edifact-to-xml-transformer\\Files\\Source\\<filename>

1.2- dbURL: Used to connect with the database. Ex:

jdbc:sqlserver://<SystemName>\\<DBServer>;databaseName=<DBName>

1.3- dbusername: database username

1.4- dbpass: database password. This value could be encrypted also but for this example, plaintext would do

2- Code to load up properties:

private static Properties loadProperties()
    {
    	InputStream input = null;
    	Properties propObject = null;
    	
    	try {
    		propObject = new Properties();
    		/*Path to the properties file. The following code retreives the properties file 
    		 from the relative path. This enables to property to remain open to 
    		 updates/modifications rather than getting baked into the jar. 
    		*/
    		String path = new File("src/main/resources/config.properties").getAbsolutePath();
    		
    		input = new FileInputStream(path);
    		
    		/*
    		 	Loading the properties object with properties defined in the config file
    		 */
    		propObject.load(input);

    	}catch (IOException ex) {
    		ex.printStackTrace();
    	}finally {
    		if (input != null) {
    			try {
    				input.close();
    				
    				
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	/*
    	  Returns the property
    	 */
    	return propObject;
    }

To read a given property call the getProperty method on it. Ex:

propObject.getProperty("sourcefilepath"));

Execution:

1- In order to run the program/app, its better to export it as a runnable jar.

2- Runnable jar: is simply put, a jar file that has (in addition to a regular jar) MANIFEST.MF file embdedded into it. This manifest file makes the jar file aware of the entry point of the application (the class which has the main method implemented)

3- Exporting the app as a runnable jar is pretty easy of you are using eclipse by following these steps:

3.1- Right click on the project name and choose export:

3.2- Choose “Runnable JAR file” under “Java”:

 

3.3- Choose the class that has the main method, under “Launch configuration” dropdown

3.4- Choose the export destination accordingly and hit finish

 

4- In order to run the jar file, this needs to be done on the command prompt/terminal (if you are on linux based systems): java –jar jarfilename.jar

5- Here is a tip: if you want to spare your customer the hassle of running commands, do him a favor and package the command in a cmd file (or .sh file on linux/mac)

5.1- Open editor and type this command (the jar execution command): java –jar jarfilename.jar

5.2- Save it as app.cmd (as the target system in my case is windows)

5.3- You can let the customer create a shortcut to this cmd file and put that on his/her desktop for some added convenience

To sum it all up, here is what I’d suggest:

  • Create a new folder that is going to house the application code and its resources:
    • Runnable jar
    • Configs
    • Source/Target folders
  • Create the execution cmd
  • Create a new subfolder, called Execution, that should contain the following:
    • Copy the shortcut to the execution cmd
    • Copy the shortcut to the config.properties file
  • This folder can be staged anywhere on the customer’s system. This severs as the only place from where he/she can run the program from

 

Leave a Reply

Your email address will not be published. Required fields are marked *