HTML

4 examples to Send Email with Attachment from Linux using mailx command

One of the common task for programmers working in Linux machine is to send emails, for example, your Java applications are running on Linux e.g. RHEL 5 or RHEL 6 version and you need to send a report of all the clients connecting to your application. An easy way to accomplish this is to write a shell script, which will dump all clients in the log file and then use grep command and generate a report. If you have to grep logs from multiple hosts, you can also use the SSH command to run the same command on multiple hosts. Once you go the list of clients connecting to your application, you can send the CSV file to yourself in an email. Now, the question is how to send an email from Linux? and more importantly how to send an email with attachments? Well, there are different options based on whether you are running on RHEL 5 or RHEL 6.

Is it possible to instantiate an Abstract class in Java?

Earlier I have shared one of the frequently asked questions in Java, can we make an abstract class final in Java and my readers really liked and asked for more such questions. So, today I am going to talk about whether we can create an instance of an Abstract class in Java or not? This is another interesting core Java question you will find on telephonic interviews or Oracle certified Java programmer certification. The answer to this question is simple, No, you cannot instantiate an abstract class in Java because it is abstract, it is not complete hence it cannot be used.  When you create an instance of a class, its's constructor is called and even though abstract class can have a constructor, the compiler will not allow you to create an instance of the class. It's a compile-time error to create an instance of an abstract class in Java.

How to use Queue in Java - Example Tutorial

The JDK provides implementation of several data structure in Java collection framework e.g. ArrayList is a dynamic array, LinkedList represents a linked list, HashMap represents hash table data structure, and Queue interface represent queue data structure. Btw, If you are not familiar with data structure itself then see Introduction to Data Structures & Algorithms in Java, a good course to start with the essential data structure in Java. The Queue data structure allows you to process elements in the first in first out order and that's why it is also known as FIFO data structure. In order to provide first in first out functionality, it allows you to insert data at one end and delete data from other ends, also known as consuming data. If you need to process objects in the FIFO order you can use a Queue to store them.

Top 4 Free Microsoft SQL Server Books - PDF Download or Online Read

Hello Guys, today, I am going to share some of the good database and SQL server books which are freely available on the internet for reading online or download as PDF. If you have been reading this blog then you know that I love books, I love to read books and I love to collect books that's why I have a big online and offline library. Many people question me do I really find to read all books which I share? Well, I don't read all books page by page, unless and until its something like Effective Java or Clean Code. I mostly skim through books and stopping on interesting and new things, which I don't know or probably have a misconception, but I do like to refer books many times. If I skim through a book, I know where can I find more information about a particular concept and then I explore it in more depth.

How to change date format of String in Java 8

One of the common programming task in Java is to change the date format of a given Date or String. For example, you have something like "2017-01-18 20:10:00" and you want to convert it that date into "2017-01-18", or you want to convert from dd-MM-YY to MM-dd-YY or to any other format of your choice and need, but a valid date format as per Java specification. How will you do that? Well, it's not that difficult. It's just a two-step process. In the first step, you need to parse String to create an equivalent date using current format and then once you got the date, you need to again convert it back to String using the new format. The same process is repeated in both Java 8 and before, only corresponding API and classes changes.

How to Create Random Alphabetic or AlphaNumeric String of given length in Java - Example

Suppose, you want to generate an alphabetic or alpha-numeric string of given length in Java? How do you do it? Well, if you are like me, you probably search a library e.g. Apache commons-lang for something which can do this task. It's a good thing. There is no point on re-inventing a wheel if a tried and tested solution already exists. In fact, Effective Java, the most respected book in Java world also suggest to know and use your library. In fact, Apache commons-lang does have a class called RandomStringUtils, which allows you to generate Random alphanumeric String in just one method call as shown below:

RandomStringUtils.randomAlphanumeric(20).toUpperCase();

This works great for many practical purposes but if you are generating security sensitive identifier then this is not 100% secure.

How to add/remove new elements to an array in Java?

Java beginners often ask how to add new elements to an array? or how to remove elements from an array in Java? is that possible in Java? Well, no, it's not possible in Java because arrays length cannot be changed once created. Which means neither you can add new elements nor you can remove an old one. All you can do is that reassign values to the differnet bucket. For example, if you have a String array of length 3 e.g. {"java", "is", "great"} then you can just replace the individual index to make it like {"Python", "is", "great"} by replace "java" with "Python" at first index, but you cannot add or remove elements.

5 ways to Compare String Objects in Java - Example Tutorial

here are many ways to compare String in Java e.g. you can use equals() and equalsIgnoreCase() for equality check and compare() and compareTo() for ordering comparison. You can even use the equality operator == to perform reference based comparison e.g. to check both the String reference variable points to the same object. In general, equals() is used to check whether the value of given String is same i.e. they contain same characters in the same sequence or not e.g. "Groovy".equals("Groovy") will be true if you compare them using equals() method. You can also use equalsIgnoreCase() to check if they are equal irrespective of case e.g. "Apple" and "apple" will be same if you compare them using equalsIgnoreCase() method.

How to convert List into Map in Java 8 - Example Tutorial

One of the common tasks in Java programming is to convert a list to map and I have written about this in past and today we'll see how Java 8 makes this task easier. Btw, be it Java 7 or Java 8, you need to keep something in mind while converting a list to map because they are two different data structure. For example, the list allows duplicate elements but keys in a map must be unique, the value can be duplicated but a duplicate key may cause a problem in Java 8. Similarly, another concern is order. A list is an ordered collection but the map doesn't guarantee any order unless you decide to use LinkedHashMap, which keeps insertion order or TreeMap which keeps mapping in the sorted order of keys. This is one of the important details which many Java beginners forget and then spend hours to chase subtle bugs.

Eclipse IDE - How to Remote Debug a Java Application Running on Linux

The remote debugging of Java program is an ultimate tool in the arsenal of a Java developer, which is often become the last and only tool to investigate a bug on a Java application running on remote host e.g. on Linux server or Windows server. Almost all major Java IDE provides remote debugging e.g. NetBeans, Eclipse, and IntelliJ IDEA, but I mostly use Eclipse for Java coding and so it's my preferred tool to remote debug a Java program. In order to setup remote debugging in Eclipse, you need to do a couple of tasks e.g. you need to start your JVM with debugging parameters or arguments and then you need to create a "remote debug configuration" in Eclipse IDE itself.

Dealing with Files and Directories in Java - 10 Thing Java Developer Should Know

The File API is one of the important parts of any programming language or API and even though Java's file API both new and old, are powerful, they are not intuitive enough compared to other languages e.g. Python. Apart from knowing the essential classes and abstractions e.g. File, InputStream, OutputStream, Reader, Writer, Channel etc, you also need to know and remember some nitty gritty detail to avoid subtle issues. There are many articles out there on the internet which can teach you how to read and write data from the file but there are very few which will tell you to do it in right way.

3 Ways to Generate Random Integers on a Range in Java

Manytimes you need to generate random numbers, particular integers in a range but unfortunately, JDK doesn't provide a simple method like nextIntegerBetween(int minInclusive, int maxExclusive), because of that many Java progarmmers, particularly beginners struggle to generate random numbers between a range e.g. random integers between 1 to 6 if you are creating a game of dice, or random number between 1 to 52 if you are creating a game of playing cards and you need to choose a random card, or most commonly random numbers between 1 to 10 and 1 to 100. Then, the question comes, how to solve this problem? How to generate random int values between a range? Well, you need to do a little bit of work.

Eclipse - Unsupported major.minor version 53.0, 52.00, 51.0 Error in Java

The UnsupportedClassVersionError is a big nightmare for Java developers, probably the next biggest after NoClassDefFoundError and ClassNotFoundException but its slightly easier to solve. The root cause of this error is that your code is compiled using higher JDK version and you are trying to run it on the lower version. For example, the Unsupported major.minor version 53.0 means your code is compiled in JDK 9 (the class version 52 corresponds to JDK 9) and you are trying to run it on any JRE lower than Java 9, probably JDK 8, 7, or 6.

How to lock a File before writing in Java? Example

A file is one of the oldest ways to store data and share data but if you are working in a shared file i.e a file which can be read or write by multiple readers and writers, you need to make sure that the file is locked before you try to write on it. This is needed to ensure that someone doesn't overwrite the data you are writing. Fortunately, Java provides a mechanism to lock a file before writing using the FileLock interface. You can get the handle of FileLock by using FileChannel for writing to a file. The FileChannel class is generally used to write faster in the large file and one of the common way to write binary data in Java.

Effective Java 3rd Edition by Joshua Bloch – Book Every Java Developer Should Read in 2018

Hello guys, you might have heard about it that Effective Java 3rd Edition is finally available. After 10 years of the long wait by Java programmers from all over the world, finally, an updated version of, probably the most popular Java book on the planet is released now. This new version covers the features and paradigm introduced in JDK 7, 8, and 9.  Joshua Bloch has done a great job in also updating most of the items to keep it relevant to the newer version. Apart from that, there is a whole new chapter added to explain best practices around Java 8 features. I know you don't need any reason to read this book, given its huge popularity and acceptance. Many startups which use Java,  particularly in India, are also distributing copies of this book as a New Year Gift.