Tag Archives: Java

Java – How to Calculate Size of Objects & Arrays

This article represents a list of web pages which can help one understand the memory usage of Java objects and arrays along with examples. Please feel free to comment/suggest any other cool pages. Also, sorry for the typos. The in-memory size of the object depends on the architecture, mainly on whether the VM is 32 or 64-bit. The actual VM implementation also matters. How to calculate memory usage of a Java object?: Very simplified explanation of how one could calculate a memory of any Java object. For example, lets say, you want to calculate the memory of a Java object which holds two int variables, one boolean variable, one Long object, …

Continue reading

Posted in Java, Web. Tagged with .

How to Dockerize Springboot Web App

This blog represents instructions and code samples on how to Dockerize a Springboot Web app. The source code be found from this git repository. Following are key aspects of this blog: Dockerfile & image Install the image Run the container and access the app Dockerfile & Image Download the code from this page and put them all in a folder. Before we go one to create the image, lets know a little more about the Dockerfile. Following points need to be noted in the Dockerfile: The image created is a Centos6 image The Java is installed in a predefined path “/opt/jdk” Then, Maven is installed and JAVA_HOME is set. The source …

Continue reading

Posted in Dockers, Java. Tagged with , .

Java Code Sample to Access Firebase Data

This article represents Java code sample which can be used to access Firebase database. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Pre-requisite for Firebase Database Access It is recommended to not allow anyone to access Firebase database without any kind of authentication. The minimum that could be done is enable “Anonymous Authentication” by logging in Firebase console. Java Code Sample for Firebase Database Access In the code below, note some of the following: Code for anonymous authentication Code for accessing a user object based on a given userId Firebase firebase = new Firebase(“https://dbname.firebaseio.com/”); firebase.authAnonymously(new Firebase.AuthResultHandler() { @Override …

Continue reading

Posted in Java, Web. Tagged with , .

Springboot Web Hello World with a Java & Pom.xml file

This blog presents instructions on how to quickly get started with Springboot Hello World with just one Java file and a Pom.xml. Before getting set up with files, make sure you have installed and configured following Java; Setup Java_Home as it is required by maven Maven; Maven would be used to package our app.   Java Code Sample Following is the code for Java file. Make sure to create hello/HelloController.java file within src/main/java. This is required for maven to package it correctly. package hello; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @SpringBootApplication public class HelloController { @RequestMapping(“/”) String home() { return “Hello World!”; } public static …

Continue reading

Posted in Java, Web. Tagged with .

How to Upload CSV Files using Apache FileUpload/CSV

This article represents code samples on how to use Apache Commons FileUpload and Apache Commons CSV libraries to upload CSV file using Java Servlet. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.   Code Samples – Java Code Following is the code for Java Servlet. package com.vitalflux.core; import java.io.IOException; import java.io.StringReader; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVRecord; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileIOController extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (ServletFileUpload.isMultipartContent(request)) …

Continue reading

Posted in Java. Tagged with .

Java – How to Download Existing Google App Engine Project

This article represents tips on how to download existing google app engine project including its source code (HTML/JSP files).. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are steps that need to be taken: Make sure you have JDK installed. Access this page for downloading Java Download Google App Engine SDK for Java. The download file could be found on this page. Unzip the App engine SDK and go to the bin folder using command prompt. Go to your http://appengine.google.com account and get the information such as your app_name and your_app_version. On Windows, execute the command as …

Continue reading

Posted in Java, Web. Tagged with .

Docker – Quick Java 8 or Java 7 Dev Environment with Dockers

This article represents code samples which could be used to build and start Java 7 or Java 8 Docker containers appropriately based on the needs with a single script and command such as “./startJava.sh -v 7 Java7” for Java 7 and “./startJava.sh -v 8 java8” for Java 8. This would be useful to try and test your Java code against Java 7 and Java 8 versions very easily based on the fact that you could start both Java7 and Java8 containers simultaneously and, run your code in these containers at the same time for testing. Please feel free to comment/suggest if I missed to mention one or more important points. …

Continue reading

Posted in DevOps, Dockers, Java, Web. Tagged with , , .

Dockers – How to Get Started with Java8 Dev Environment

This article represents take away code samples and tips on how to setup Java8 container using Dockers and get started. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: Java 8 Image Dockerfile Install Scripts to Install and Start Java8 Env   Java 8 Image Dockerfile Following is Dockerfile for creating Java8 image. Name the file as java8_base.df. # Base is the Centos environment FROM centos:centos6 # Make the directories under which Java 8 will get installed RUN mkdir /opt/jdk RUN cd /opt # Install wget and tar which …

Continue reading

Posted in DevOps, Dockers, Java. Tagged with , , .

Java – How to Migrate from JRockit to HotSpot JVM

This article represents information on migration from JRockit to HotSpot JVM. Recently, the migration guide from JRockit JVM to HotSpot JVM has been published. The information about the same can be found on this page. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. The detailed information could be found on this page. Following are top 5 areas where changes need to be made for migrating from JRockit JVM to HotSpot JVM. Tuning garbage collection. One could also access HotSpot GC tuning guide on following page. Java runtime options Java compilation optimization Logging: There are parameters related to verbose …

Continue reading

Posted in Java. Tagged with .

Java – How to Get Users Tweets using Twitter HBC Http Client

This article represents code samples which could be used to retrieve users tweets for one or more Twitter users, using Twitter HBC Java client. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: Key Steps in Retrieving the Users Tweets Code Sample – Get User Tweets Key Steps in Retrieving the Users Tweets Following are key steps required to be taken to retrieve users tweets: Get consumer key and access tokens details Determine userId for users for whom you want to get the tweets as they appear. For test …

Continue reading

Posted in Java, Web. Tagged with .

Java – How to Get Started with Twitter HBC Streaming API

This article represents instructions on how to get started with HBC, A Java HTTP client, for consuming Twitter’s Streaming API. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: How to get Twitter Consumer/Access Token Keys Instructions to Get Started with HBC How to get Twitter Consumer/Access Token Keys Before getting started with HBC API for integrating with Twitter API, make sure that you have acquired following four details from Twitter: Consumer Key Consumer Secret Access Token Access Secret Token All of the above keys could be started from …

Continue reading

Posted in Java, Web. Tagged with , .

Maven – How to Build Jar Files and Obtain Dependencies

This article represents facts on what would it take to build one or more jar files for a given framework/library using Maven, provided the framework’s downloadable files consisted of pom.xml. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. So far, whenever I came across pom.xml file in the framework that I downloaded in order to get the jar file, I hated it. I used to, then, go to internet and get the compiled jar file(s) for the framework/library. And, good thing is that I have been able to get my work done. This was purely out of my …

Continue reading

Posted in Java, Tools, Web. Tagged with .

Java – How to Get Company Updates using LinkedIn API

This article represents code samples and changes that need to be done in original Linkedin-J framework (open-source) to get company updates of different types such as following: Job Posting Status Updates New Product Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.   Code Samples & Steps to Get Company Updates Following files need to be changed along with mentioned changes: LinkedinApiUrls.properties: Following code needs to be added under APIs mentioned under companies: com.google.code.linkedinapi.client.getCompanyStatusUpdates=http://api.linkedin.com/v1/companies/{id}/updates{queryParameters} LinkedInApiUrls.java: Following code needs to be added: public static final String GET_COMPANY_STATUS_UPDATES = linkedInApiUrls.getProperty(“com.google.code.linkedinapi.client.getCompanyStatusUpdates”); CompaniesApiClient.java: Following APIs need to be added: /** * Gets the …

Continue reading

Posted in Java, Web. Tagged with , .

Java – How to Scrape Web using Multi-threading (ExecutorService)

This article represents code examples on how to Scrape multiple URLs at once using Java Multi-threading API such as ExecutorService.  The sole reason why I have been doing scraping lately is the need to get data from web to apply data analytics/science (machine learning algorithms) and extract knowledge from the data. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are three different methods whose code samples have been presented below: scrapeURLs that takes input file consisting of URLs to be scraped and output file where the output needs to be written scrapeIndividualURLs which takes as an argument, URL …

Continue reading

Posted in Java, Web. Tagged with .

Java – How to Create Binary Search Tree for String Search

This article represents code samples and high level concepts for creating a binary search tree to do String related operations such as some of the following: Search one or more words Replace word with new word Find number of occurences of a given word in a string For those who are new to Binary Search Tree, note that Binary Search Tree is defined as tree that satisfy some of the following criteria: Each node in the tree has at most only two children Each node is represented with a key and associated data Key in left children is less than the parent node and key in the right node is …

Continue reading

Posted in Java, Web. Tagged with , .

Java – How to Create a Binary Search Tree

This article represents the high level concept and code samples which could be used to create a binary search tree in Java. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: What is a binary search tree? What are different kind of traversals? Code Samples What is a binary search tree? A binary search tree is a binary tree in which every node contains a key that satisfies following criteria: The key in left child is less than the key in the parent node The key in the right …

Continue reading

Posted in Java, Web. Tagged with .