How to resolve package com.sun.image.codec.jpeg does not exist?

Sometimes the java project (usually a lower version of Java (1.6 or lower)) results in the following compilation error:-

INFO] Compiling 106 source files to /Users/JavaProject/src/java/web/target/classes [INFO] ————————————————————- [ERROR] COMPILATION ERROR : [INFO] ————————————————————- [ERROR] /Users/JavaProject//src/java/web/main/java/net/esi/ppc/presentation/service/CaptchaImageGenerator.java:[24,31] error: package com.sun.image.codec.jpeg does not exist [ERROR] /Users/JavaProject//src/java/web/main/java/net/esi/ppc/presentation/service/CaptchaImageGenerator.java:[25,31] error: package com.sun.image.codec.jpeg does not exist

To resolve this issue you can add the following in the pom.xml

<compilerArgument>

            -XDignore.symbol.file

</compilerArgument>

It’s basically an argument for the Java compiler. Also, adding the arguments “-XDignore.symbol.file” will depend on the build tool.

A much better solution is to rewrite the code without using the proprietary classes.Because you are not supposed to directly use the classes under the package com.sun.image.codec.jpeg

But if you have a legacy code which you can’t change, then this is the solution.

You can read the following post for more information :-https://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

The package com.sun.image.codec.jpeg has been removed in Java 7 as mentioned in the Java SE 7 and JDK 7 Compatibility Guide.

Synopsis: The Non-standard com.sun.image.codec.jpeg Package is Retired

Description: The com.sun.image.codec.jpeg package was added in JDK 1.2 (Dec 1998) as a non-standard way of controlling the loading and saving of JPEG format image files. This package was never part of the platform specification and it has been removed from the Java SE 7 release. The Java Image I/O API was added to the JDK 1.4 release as a standard API and eliminated the need for the com.sun.image.codec.jpeg package.

Let me know if this solution worked for you .Happy Coding guys an gals 🙂

Loops in Java

Loops in java are used to execute a set of statements repeatedly until a condition is satisfied or to iterate a collection of objects.

There are 4 types of loop:-

  • The basic for loop
  • The enhanced for loop
  • The while loop
  • The do while loop
  • The forEach loop

The basic for loop

The basic for loop has the following syntax:-

for(initialization ; condition ;counter increment/decrement){

}

If we have only one statement in the for loop then the curly braces are not mandatory.

Step 1 :- The initialization part of the loop executes for the first time.

Step 2 :- The condition is checked .If the expression evaluates to true the statements inside the for block is executed else the control flow exits the loop that is step 4

Step 3 :- The counter is incremented or decremented.

We repeat 2 and 3 phases until the condition is not met and the for loop is terminated

Step 4 :- Exit the loop

The enhanced for loop

The enhanced for loop is used to iterate a collection of objects or an array in java

Syntax:-

for(data_type item:collection/array){

//statements ….

}

  • Item is the single item from the collection 
  • Collection /array is the variable which you have to iterate or loop through 

Here’s the flow of the enhanced for loop:-

  • Iterate through each item in the array /collection
  • Places each item in the variable 
  • Executes the body of the loop

The loop iterates through all the objects in the collection/array one by one.

The while loop

The while loop is used to execute a set of statements until a condition is satisfied.

Here’s the flow:-

  1. Loop starts with the condition.If the condition is true step 2 is executed.If false the loop exists that is step 3.
  2. The statements inside the loop is executed .Usually the statements also update the variable being used in the conditional statement(at the start of the loop)
  3. Exit the loop

The do while Loop

The do while loop make sure the statements in the block is executed at least once irrespective of the conditional expression in while .

Syntax :-

Do {

..//statements 

}while(expression);
It ends with a colon.

Here’s the flow:-

Step1 :- The statements is executed in the do while block 

Step2 :- The expression is evaluated.If it is true the statement in the block is executed else the control flow exists the do while block that is step3 

Step3 :- Exit the do while block.

The forEach method

In Java 1.8 a new forEach method is introduced to iterate the elements.It is defined in Iterable and Stream interface.It’s a default method in iterable interface.

We can loop a collection of objects using the forEach method as the collection interface extends the Iterable interface.

The forEach method take functional interface (Consumer ) as the arguments so we can use lambda expressions along with the forEach method.

That’s all about loops in Java.I hope you enjoyed reading this article.

Happy Coding 🙂



Default Methods In Java

Default methods allows us to add new functionality to new or existing interfaces without breaking the existing functionality of legacy code.

Prior to Java 1.8 interfaces did not allow default interfaces and it was very difficult to modify an existing interface without impacting the class which implemented the interface.For instance if a new method is added to an interface ,it  needs to be implemented in all the classes implementing that interface.

Example

Consider the following interface which is an existing piece of code:-

The interface is implemented by various branches of the Good Food Restaurant.

We later plan to add the restaurant working hours .But if we add it to the interface ,the method needs to be implemented in all the three classes.This can be a lot of work especially in big projects where the interface is implemented in several different classes and multiple new method are added in the interface.

Default method comes to the rescue in all these cases.With default methods its not mandatory for an implementing class to implement the default method , its optional.

A default method is defined in an interface and if the implementing class do provide the implementation of the default method then it overrides the default method definition.Here’s the summary when you implement an interface that contains a default method:-

  • Do not redefine the default method in the implementing class ,which lets you use the default method definitions whenever it is called.
  • Redefine the default method, in this case it is overridden

Extending an interface with a default method

The following rules apply when you extend an interface that contains a default method along with one more scenario:-

  • Do not redefine the default method in the implementing class ,which lets you use the default method definitions whenever it is called.
  • Redefine the default method, in this case it is overridden
  • Redeclare the default method which makes it abstract.

That’s all about default methods in Java.I hope you enjoyed reading this articles.

Java Lambda Expressions

What are Lambda Expression?

Lambda expressions were introduced in Java 1.8 and it’s the first step towards functional programming.Lambda expressions allow you to treat functionality as a method arguments.The functionality can be passed around to different methods as lambda expressions and executed on demand.It also make the code clean and concise.

In short lambda expressions are basically blocks of code that can be passed around methods as arguments ,assigned to variables and returned from function calls.

Why they are used?

Anonymous classes enable implement a base class or an interface without giving a name.But for classes/interfaces where there is only one method even anonymous classes looks verbose and lengthy.Lambda expression lets you implement single method classes/interfaces in a much more compact way.

Lambda Expression and Functional Interfaces

Lambda expression and functional interfaces go hand in hand. Lambda expressions are used to implement the abstract method of the functional interface. Here’s some of the most commonly used functional interfaces.You can find the complete list of functional interfaces at https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

Consumers

It’s a built in functional interface in the java.util.function package.It can be used in all the context where an object needs to be consumed and it has a void return type.For instance it can accept some object ,perform some operation on the object and display the result without returning any result.It accepts a single input argument.

Predicates

A functional interface and represents an operation that takes a single input and returns a boolean value.

Function

It represents a function that accepts one argument and returns a result.For instance it can accept an object ,perform some operation on the object and return the modified object.

Suppliers

It represents a function that do not take any arguments but returns a result.

In the lambda expression any of these and many other functional interfaces can be implemented depending on the requirements.

Lambda Expression Syntax

(argument-list) -> {body}  

  • Argument List can be empty or we can have as many arguments as we want
  • Arrow Token (->):- It is used to link arguments with the body
  • Body :- It contains expressions and statements .The curly braces are optional if there is only one statement

Example

We have an employee class and an interface.

Without Lambda Expression the code will look like this.We are using anonymous class in this case:-

With lambda expressions:-

Lambda expressions is pretty versatile feature in Java 1.8 and this article gives you a glimpse of it.

Let me know you inputs on lambda expressions.I would be glad to hear from my readers. Happy Coding 🙂

Anonymous Class in Java

Anonymous classes can be tricky to understand for beginners.Mainly because the syntax can look verbose or at least it looked very confusing to me when I started my career as a java developer.So let’s talk anonymous classes today.

Anonymous classes were introduced in java to make code more concise and reduce the lines of code.Here’s some facts about anonymous classes

  • We can declare and instantiate an anonymous class at the same time.
  • They do not have a name.
  • An anonymous class have access to the members of its enclosing class.
  • An anonymous class can not access local variable in its enclosing scope that are not final or effectively final.
  • A variable declaration in an anonymous shadows any variable in its enclosing class with the same name
  • Can not declare static initialization blocks in anonymous classes.
  • Can not declare a constructor in anonymous classes

Example of anonymous class:-

When you run the program , you will get the following output:-

Syntax of Anonymous Classes:-

An anonymous class is an expression.It contains class definition inside the curly braces.

It has the following pieces:-

  • It starts with the new operator.
  • The name of the interface to implement or the class name.
  • Parenthesis that contain arguments to the constructor just the way we do it in a normal object instantiation.
  • A body of the anonymous class.The body starts and ends with curly braces and colon at the end of the ending braces

You can declare the following in anonymous classes:-

  • Extra Methods which are not originally declared in the interface or the class you are implementing
  • Initialization blocks
  • Fields
  • Local Classes

You can create an anonymous class from an interface or a class or an abstract class.

That’s all about anonymous classes . I hope you enjoyed reading about anonymous classes.Have fun with coding.