Category Archives: Uncategorized

[Solved] Maven: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK


This maven exception occurs  when your Window/Unix environment variable for JAVA_HOME and PATH are pointing to JRE instead of JDK. That’s why maven will not compile code and throw below exception.

Problem

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SpringBootApp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.5.2/maven-install-plugin-2.5.2.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.5.2/maven-install-plugin-2.5.2.pom (7 KB at 2.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/25/maven-plugins-25.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/25/maven-plugins-25.pom (10 KB at 10.2 KB/sec)
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ SpringBootApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ SpringBootApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Saurabh Gupta\facingIssuesOnIT\SpringBootApp\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.342 s
[INFO] Finished at: 2018-05-28T15:59:31+05:30
[INFO] Final Memory: 20M/209M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project SpringBootApp: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Solutions

To resolve this issue you can follow below steps by solving command prompt or Eclipse.

Command Prompt

How to Configure Maven in Window and Linux?

Eclipse

  • Right click on project -> Select Properties
  • Select Java Build Path-> Libraries Tab -> Click on Add Library
  • Refer below link for reference.

Eclipse : How to set java path of JDK/JRE?

[Solved] java.lang.ArithmeticException: / by zero in JAVA


java.lang.ArithmeticException is Unchecked exception and sub class of java.lang.RuntimeException. It’s thrown when an exceptional condition occurred in Arithmetic Operations. It can also occurred by virtual machine as if suppression were disabled and /or the stack trace was not writable.

Constructors:

  • ArithmeticException() : Constructs an ArithmeticException with no detail message.
  • ArithmeticException(String s) : Constructs an ArithmeticException with the specified detail message.
Example :
An integer value “divide by zero” throw ArithmaticException.  In below example i am dividing int, double, float and long value with 0. For long and int type value it’s throwing Arithmatic Exception while for double and float printing special value as Infinity.  See below How to fix Arithmetic Exception? section.
package example;

public class ArithmaticExceptionExample {

	public static void main(String[] args) {
		int x=0;
		int y=5;
		double z=6;
		float l=6;
		long k=10L;
		//Integer value divide by integer value as 0 throw ArithmeticException
		try
		{
			System.out.println("Integer value divide by zero");
		System.out.println(y/x);
		}
		catch(ArithmeticException ex)
		{
			ex.printStackTrace();
		}
		//Double value divide by integer value as 0 No Exception special value Infinity
		System.out.println("Double value divide by zero");
		System.out.println(z/x);

		//Float value divide by integer value as 0 No Exception special value Infinity
		System.out.println("Float value divide by zero");
		System.out.println(l/x);

		//Long value divide by integer value as 0 throw ArithmeticException
		try
		{
		System.out.println("Long value divide by zero");
		System.out.println(k/x);
		}
		catch(ArithmeticException ex)
		{
			ex.printStackTrace();
		}

	}

}

Output:

Integer value divide by zero
java.lang.ArithmeticException: / by zero
	at example.ArithmaticExceptionExample.main(ArithmaticExceptionExample.java:15)
Double value divide by zero
Infinity
Float value divide by zero
Infinity
Long value divide by zero
java.lang.ArithmeticException: / by zero
	at example.ArithmaticExceptionExample.main(ArithmaticExceptionExample.java:31)

How to fix Arithmetic Exception?

In above example:

Y/X or K/X result ArithmeticException

What happens here is that since both the dividend and the divisor are int, the operation is an integer division, whose result is rounded to an int. Remember that an int can only contain whole number (of limited range, some 4 billion numbers approximately) That’ s why throwing Arithmatic Exception. same case for long value.

Z/X or L/X result special value Infinity

Here float (L) or double (X) precision value  divide by zero store in float and double value that why result is special value Infinity.

 

[Solved] NullPointerException In Java


NullPointerException is Runtime Exception and Unchecked Exception which thrown when trying to access an object which is having value as null.

What is Null Value?

Null value is a special  value which tell JVM this reference variable is assigned with no value as null. It means variable is not pointing to any object instances and not having any value.

Cases when NullPointerException Happen:

  • Accessing or modifying a null object’s field.
  • Taking length of null  from object like String, Array, ArrayList etc.
  • Invoking a method from a null object.
  • Calling method passing argument as wrapper object having null value while method having primitive arguments.
  • Accessing or modifying the slots of null object, as if it were an array.
  • Throwing null, as if it were a Throwable value.
  • When you try to synchronize over a null object.

Below are some examples for NullPointerException for above cases which showing how NullPointerException can happen in your code.

import java.util.List;

public class NullPointerExceptionExample {

	public static void main(String[] args) {
		//Example 1: String object assigned with null
		try
		{
		String str=null;
		if(str.length()>0)//NullPointerException Here
			System.out.println("String Length :"+str.length());
		}
		catch(NullPointerException ex)
		{
			System.out.println("Example 1 :String object assigned with null");
			ex.printStackTrace();
		}

		//Example 2: Collection List Object Assigned with Null
		try
		{
		List list=null;
		if(list.size()>0)//NullPointerException Here
			System.out.println("List Length :"+list.size());
		}
		catch(NullPointerException ex)
		{
			System.out.println("Example2 :Collection List object assigned with null");
			ex.printStackTrace();
		}

		//Example 3: Calling method with wrapper and method having primitive value
		try
		{
		Long acNumber=null;
		checkValue(acNumber);
		}
		catch(NullPointerException ex)
		{
			System.out.println("Example 3 :Calling method with wrapper and method having primitive type");
			ex.printStackTrace();
		}

		//Example 4 :Accessing slot of Array object having value as null
		try
		{
		Employee [] empArr=new Employee[10];
		System.out.println("Employee Name :"+empArr[5].getName());
		}
		catch(NullPointerException ex)
		{
			System.out.println("Example 4: Accessing slot of array objet having value as null");
			ex.printStackTrace();
		}
	}

	public static void checkValue(long acNumber)
	{
		System.out.println("Account Number:"+acNumber);
	}

	private class Employee
	{
		private int id;
		private String name;
		private double salary;

		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public double getSalary() {
			return salary;
		}
		public void setSalary(double salary) {
			this.salary = salary;
		}

	}

}

Output :

Example 1 :String object assigned with null
java.lang.NullPointerException
	at example.NullPointerExceptionExample.main(NullPointerExceptionExample.java:12)
Example2 :Collection List object assigned with null
java.lang.NullPointerException
	at example.NullPointerExceptionExample.main(NullPointerExceptionExample.java:25)
Example 3 :Calling method with wrapper and method having primitive type
java.lang.NullPointerException
        at
example.NullPointerExceptionExample.main(NullPointerExceptionExample.java:38)
Example 4: Accessing slot of array objet having value as null
java.lang.NullPointerException
	at example.NullPointerExceptionExample.main(NullPointerExceptionExample.java:50)

 How to avoid NullPointerException?

NullPointerException can be avoid by using considering below points:

  • Need to ensure object as initialize properly before any operation check for null .
  • Need to ensure object reference is not null before calling any method or operation on fields.
  • While comparing string with literals. Instead of invoking the method from the null object, consider invoking it from the literal.
  • Before calling method check arguments if valid then only call methods.
  • Use of Ternary operator for initialize with some default value if object value is null.
  • We can use NullPointerException catch block when no action perform or if need to throw some Customize Exception or User Defined Exception.

In below example consider all above case for avoid NullPointerException.

public class NullPointerExceptionExample {

	public static void main(String[] args) {
		//Example 1: String object assigned with null
		//NullPointerException avoided by checking null before use
		String str=null;
		if(str!=null && str.length()>0)
			System.out.println("String Length :"+str.length());

		//Example 2: Object assigned with Null
		//NullPointerException avoided by using  null check before accessing field
		Employee emp=null;
		if(emp!=null)
			System.out.println("Employee Name :"+emp.getName());

		//Example 3: name is assigned with null
		//NullPointerException avoided by calling equals method from literals instead of name object.
		String name=null;
		if("FacingIssuesOnIT".equals(name))
		{
			System.out.println("Name is FacingIssuesOnIT");
		}
		else
		{
			System.out.println("Name is not FacingIssuesOnIT");
		}

		//Example 4: Calling method with wrapper as null and method having primitive value
		//NullPointerException avoided by checking null on method arguments before calling method.

		Long acNumber=null;
		if(acNumber!=null)
		{
		checkValue(acNumber);
		}

		//Example 5: Object reference value is null and need to perform operation
		//NullPointerException avoided by using ternary operator
		String siteName=null;
		name=siteName==null?"":siteName;
		System.out.println("Site Name Length:"+name.length() );

	}

	public static void checkValue(long acNumber)
	{
		System.out.println("Account Number:"+acNumber);
	}

	private class Employee
	{
		private int id;
		private String name;
		private double salary;

		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public double getSalary() {
			return salary;
		}
		public void setSalary(double salary) {
			this.salary = salary;
		}

	}

}

Summary

  • Here you have learn about NullPointerException and Null value.
  • Cases with examples where NullPointerException can happen.
  • Al so explained with example to avoid NullPointerException by applying null check on object, compare String values by calling operations on literals and using ternary operators.

Java, How to convert List to Arrays and Array to ArrayList


In our day to day programming sometime use this conversion by using iteration but there is another way by using Java API’s as below.

ArrayList to Arrays

List provide toArray() method to convert list elements in Arrays.

List<T> list = new ArrayList<T>();
T[]arr=list.toArray(new T[list.size]);

Example :

import java.util.ArrayList;
import java.util.List;

public class ConvertArrayListToArray {

	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("Georgia");
		list.add("Texas");
		list.add("Newyork");
		list.add("Okhlama");
		String [] states = list.toArray(new String[list.size()]);
	}

}

Arrays to ArrayList

Arrays class provide to asList() method to convert Array to immutable List So if you need to modify anything in List you will get java.lang.UnsupportedOperationException.

If you want to modification after convert to List then create with new ArrayList as below.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConvertArrayToArrayList {

	public static void main(String[] args) {
		String [] states= {"Georgia","Texas","New York", "Okhlama"};
		//Convert Array to immutable List
         List<String> stateList= Arrays.asList(states);
         try
         {
         stateList.add("North Carolina");
         }
         catch(UnsupportedOperationException ex)
         {
        	 ex.printStackTrace();
         }

         //Convert Array to mutable List
         stateList=new ArrayList<String>(Arrays.asList(states));
         stateList.add("North Carolina");
         System.out.println(stateList.get(4));
	}

}

In above example I have considered both cases for Immutable and Mutable.

More Sample Code

For more java and JDBC codes follow below links

[Solved] Elasticsearch “high disk watermark exceeded on one or more nodes”


This issue happen on Elasticsearch anytime   because your diskspace storage reached to more than 85% because in elasticsearch by default watermark is 85% of disk storage after reaching this limit elasticsearch stop working and print below warning in logs.

[2017-05-12T16:52:48,239][WARN ][o.e.c.r.a.DiskThresholdMonitor] [r78TO-I] high disk watermark [90%] exceeded on [r78TO-IgT9yPQQUgILl_5A][r78TO-I][C:\
Users\Facing Issues on IT\elasticsearch-5.4.0\data\nodes\0] free: 11.7gb[9.8%], shards will be relocat
ed away from this node
[2017-05-12T16:52:48,242][INFO ][o.e.c.r.a.DiskThresholdMonitor] [r78TO-I] rerouting shards: [high disk watermark exceeded on one or more nodes]
[2017-05-12T16:53:18,297][WARN ][o.e.c.r.a.DiskThresholdMonitor] [r78TO-I] high disk watermark [90%] exceeded on [r78TO-IgT9yPQQUgILl_5A][r78TO-I][C:\
Users\Facing Issues on IT\elasticsearch-5.4.0\data\nodes\0] free: 11.7gb[9.8%], shards will be relocat
ed away from this node
[2017-05-12T16:53:48,306][WARN ][o.e.c.r.a.DiskThresholdMonitor] [r78TO-I] high disk watermark [90%] exceeded on [r78TO-IgT9yPQQUgILl_5A][r78TO-I][C:\
Users\Facing Issues on IT\elasticsearch-5.4.0\data\nodes\0] free: 11.7gb[9.8%], shards will be relocat
ed away from this node

Solution :

We have to set below properties in config/elasticsearch.yml file for threshold value for disk storage , low and high watermark value.

cluster.routing.allocation.disk.threshold_enabled: true
cluster.routing.allocation.disk.watermark.low: 5gb
cluster.routing.allocation.disk.watermark.high: 2gb

Issues Solution

For more Elasticsearch issues solution follow link Common Elasticsearch Issues.