How to Read Large File in Java

How to Read Large File in Java

In our last article, we cover How to read file in Java.This post will cover how to read large file in Java efficiently.

Reading the large file in Java efficiently is always a challenge, with new enhancements coming to Java IO package, it is becoming more and more efficient.

We have used sample file with size 1GB for all these. Reading such a large file in memory is not a good option, we will covering various methods outlining How to read large file in Java line by line.

 

1 Using Java API

We will cover various options how to read file in Java efficiently using plain Java API.

 

 1.1 Using Java BufferReader


public class ReadLargeFileByBufferReader {
public static void main(String[] args) throws IOException {
String fileName = "/tutorials/fileread/file.txt"; //this path is on my local
try (BufferedReader fileBufferReader = new BufferedReader(new FileReader(fileName))) {
String fileLineContent;
while ((fileLineContent = fileBufferReader.readLine()) != null) {
// process the line.
}
}
}
}

Output


Max Memory Used : 258MB
Time Take : 100 Seconds

 1.2 Using Java 8 Stream API

public class ReadLargeFIleUsingStream {
public static void main(String[] args) throws IOException {
String fileName = "/tutorials/fileread/file.txt"; //this path is on my local
// lines(Path path, Charset cs)
try (Stream inputStream = Files.lines(Paths.get(fileName), StandardCharsets.UTF8)) {
inputStream.forEach(System.out::println);
}
}
}

 

Output

Max Memory Used : 390MB
Time Take : 60 Seconds

 1.3 Using Java Scanner

Java Scanner API also provides a way to read large file line by line.


public class ReadLargeFileByScanner {
public static void main(String[] args) throws FileNotFoundException {
String fileName = "/Users/umesh/personal/tutorials/fileread/file.txt"; //this path is on my local
InputStream inputStream = new FileInputStream(fileName);
try(Scanner fileScanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())){
while (fileScanner.hasNextLine()){
System.out.println(fileScanner.nextLine());
}
}
}
}

Output


Max Memory Used : 460MB
Time Take : 60 Seconds

2 Streaming File Using Apache Commons IO

This can also be achieved by using Apache Commons IO  FileUtils.lineIterator () Method


public class ReadLargeFileUsingApacheCommonIO {
public static void main(String[] args) throws IOException {
String fileName = "/Users/umesh/personal/tutorials/fileread/file.txt"; //this path is on my local
LineIterator fileContents= FileUtils.lineIterator(new File(fileName), StandardCharsets.UTF_8.name());
while(fileContents.hasNext()){
System.out.println(fileContents.nextLine());   
}
}
}

Output


Max Memory Used : 400MB
Time Take : 60 Seconds

As we saw how to read large file in Java efficiently. Few things which you need to pay close attention

  1. Reading the large file in one go will not be a good option (You will get OutOfMemoryError ).
  2. We Adapted technique to read large file line by line to keep memory footprint low.

I used VisualVM to monitoring Memory, CPU and Threadpool information while running these programmes.

based on our test, BufferReader has the lowest memory footprint, though the overall execution was slow.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons IO

How to read file in Java

How to read file in Java

In this post, We will cover as  How to read file in Java. This post will cover different options available in Java to see how to read file in Java.

1.  Reading File Using JDK

JDK provide number of option to read file in Java, we will explore few of the options to read file

  1.1 Read File using Java 8 Stream API

Java 8 Provides an efficient way to read a file using Stream API. You can use Files.line to read it as a  stream

File Content

This Show How
To Read File
Using Java 8
public class ReadFileByStream {
public static void main(String[] args) {
String fileName = "/tutorials/fileread/SampleFile.txt"; //this path is on my local
try {
List<String> readStream=Files.lines(Paths.get(fileName)).collect(Collectors.toList());
readStream.forEach(System.out::println);
} 
catch (IOException e) {
e.printStackTrace();
}
}
}

Output

This Show How
To Read File
Using Java 8

Java 8 Stream API is really clean and provide an elegant way to read the file.We have seen too many open files in a system exception being reported.

 

1.2 Read File using BufferReader and StringBuilder

BufferReader with StringBuilder is an efficient way to read small file in Java

public class ReadFileByBufferReader {
public static void main(String[] args){
String fileName = "/tutorials/fileread/SampleFile.txt";
try(BufferedReader bufferReader = new BufferedReader(new FileReader(fileName))) {
StringBuilder fileContent = new StringBuilder();
String line = bufferReader.readLine();
while (line != null) {
fileContent.append(line);
fileContent.append(System.lineSeparator());
line = bufferReader.readLine();
}
String fileInformation = fileContent.toString();
System.out.println(fileInformation);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output

This Show How
To Read File
Using Buffer Reader

Above Example used try-with-resources feature to autoclose stream.

1.3 Read File using Scanner

package com.umeshawasthi.tutorials.corejava.io.readfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Created by umesh on 5/2/17.
*/
public class ReadFileByScanner {
public static void main(String[] args) throws FileNotFoundException {
String fileName = "/tutorials/fileread/SampleFile.txt";
File inputFile = new File(fileName);
useScannerByLoop(inputFile);
useScannerWithoutLoop(inputFile);
}
private static void useScannerByLoop(final File file) throws FileNotFoundException {
Scanner fileScanner = new Scanner(file);
while(fileScanner.hasNextLine()){
System.out.println(fileScanner.nextLine());
}
fileScanner.close();
}
private static void useScannerWithoutLoop(final File file) throws FileNotFoundException {
Scanner fileScanner = new Scanner(file);
fileScanner.useDelimiter("\\Z");
System.out.println(fileScanner.next());
}
}

Output (For both cases)

This Show How
To Read File
Using Scanner
With Loop

Make sure to check next line when using Scanner else it can throw NoSuchElementException exception

 

1.4  Apache Commons IO Utils

Apache Commons provide the convenient method to work with files.

Here is a clean and efficient way to as how to read file in Java using Apache Commons IO Utils.

public class ReadFileByApacheIO {
public static void main(String[] args) throws IOException {
String fileName = "/tutorials/fileread/SampleFile.txt";
try(FileInputStream inputStream = new FileInputStream(fileName)){
String fileContent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
System.out.println(fileContent);
}
}
}

Output

This Show How
To Read File
Using Apache
IO

We have used Apache Commons IOUtils using Java 7  try-with-resources feature.

Apache FileUtils

public class ReadFileByApacheFileUtils {
public static void main(String[] args) throws IOException {
String fileName = "/Users/umesh/personal/tutorials/fileread/SampleFile.txt";
List<String> fileContent= FileUtils.readLines(new File(fileName), StandardCharsets.UTF_8);
for(String line : fileContent){
System.out.println(line);
}
}
}

Output

This Show How
To Read File
Using Apache
IO

There are multiple ways as How to Read file in Java. All options have their own positive and negative.

Our recommendation is

  1. Apache Commons
  2. Buffer Reader
  3. Others

If you are using Google Guava, you can use Guava in a very similar way Apache Commons IO.

Please refer to our post on how to convert InputStream to String for a basic understanding of the process.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons IOUtils
  2. Apache Commons IO FileUtils

Convert inputstream to string in Java

Convert InputStream to string in Java

Converting InputStream to String in Java is very common operation, in this post, We will cover how to Convert InputStream to String in Java using

  1. Apache Commons IOUtils
  2. Java

 

1. Apache Common

One of the clean ways to Convert InputStream to String in Java is using Apache Commons IOUtils.

public class InputStreamStringCommonIO {
public static void main(String[] args) throws IOException {
String sampleTestString = "Converting input stream to String using Apache Common IO";
InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());
convertUsingCopy(inputStream, StandardCharsets.UTF_8);
convertUsingToString(inputStream, StandardCharsets.UTF_8);
}
public static void convertUsingCopy(final InputStream inputStream, final Charset inputEncoding) throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, inputEncoding);
System.out.println(writer.toString());
}
public static void convertUsingToString(final InputStream inputStream, final Charset inputEncoding) throws IOException {
String output = IOUtils.toString(inputStream, inputEncoding);
System.out.println(output);
}
}

Output

Converting input stream to String using Apache Common IO

Just note that IOUtils.toString()  method does not close inputStream, you can use IOUtils.closeQuietly()  to close it.

Most of the modern project use  Apache Commons as defacto API, if you are using Apache Commons IO, this is your best option.

 

2. Java Way

If you do not have the option to use Apache Commons IO, there are multiple ways to Convert InputStream to String in Java.

    2.1 Using InputStreamReader and StringBuilder

public class ConvertByInputStreamReader {
public static void main(String[] args) {
String sampleTestString = "Converting input stream to String using JDK Input Stream Reader";
InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());
System.out.println(inputStream);
}
private static String getStringByInputStreamReader(final InputStream inputStream) throws IOException {
final int bufferSize = 1024;
final char[] buffer = new char[bufferSize];
final StringBuilder output = new StringBuilder();
Reader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
int character = 0;
while ((character = streamReader.read(buffer, 0, buffer.length)) > 0) {
output.append(buffer, 0, character);
}
return output.toString();
}
}

 

    2.2 Using BufferReader and StringBuilder

public class ReadUsingBufferReader {
public static void main(String[] args) {
String sampleTestString = "Converting input stream to String using JDK Buffer Reader";
InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder outStream = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
outStream.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(outStream.toString());
}
}

Output

Converting input stream to String using JDK Buffer Reader

 

    2.3 Using Scanner

public class ReadUsingScanner {
public static void main(String[] args) {
String sampleTestString = "Converting input stream to String using JDK Using Scanner";
InputStream inputStream = new ByteArrayInputStream(sampleTestString.getBytes());
Scanner inputScanner = new Scanner(inputStream, StandardCharsets.UTF_8.name()).useDelimiter("\\A");
String output = inputScanner.hasNext() ? inputScanner.next() : "";
System.out.println(output);
inputScanner.close();
}
}

Output

Converting input stream to String using JDK Using Scanner

Google Guava provide a good alternate to Convert InputStream to String in Java.

As we saw, there are a couple of ways to convert InputStream to String in Java, it based on your preferences, personally, I prefer Apache Commons IO.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons IO

Java Sorting Example(Comparable and Comparator)

Java Sorting Example (Comparable and Comparator)

In this article, we will cover Java sorting example (Comparable and Comparator). Java Collection API provides a way to sort Array and Lists but it expects input from the developer.This articles will try to give some example to use both java.lang.Comparable and java.util.Comparator to sort objects.

 

1. Sort an ArrayList

Collections API’s utility class Collections provide a handy way to sort an ArrayList in natural ordering provided all elements in the list must implement the Comparable interface.

public class SimpleSorting {
public static void main(String[] args) {
List<String> locationList = new ArrayList<String>();
locationList.add("California");
locationList.add("Texas");
locationList.add("Seattle");
locationList.add("New Delhi");
Collections.sort(locationList);
for (String location : locationList) {
System.out.println("Location is: " + location);
}
}
}

Output

Location is: California
Location is: New Delhi
Location is: Seattle
Location is: Texas

Above example worked perfectly with Collections.Sort() as List defined in example was Lits<String> and java.lang.String implements the Comparable interface.

In the case of List contains custom objects, Collections.sort() will fail if the custom object does not implement the Comparable interface.

2. Sort Object using Comparable

public class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Person person) {
return this.age > person.age ? 1 : this.age < person.age ? -1 : 0;
}
}

To sort it, we will use Collections.sort() method

public class ComparableDemo {
public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Umesh Awasthi", 35));
persons.add(new Person("Robert Hickle", 55));
persons.add(new Person("John Smith", 40));
persons.add(new Person("David", 23));
Collections.sort(persons);
for (Person person : persons) {
System.out.println("Person is: " + person.getName());
}
}
}

Output is

Person is: David
Person is: Umesh Awasthi
Person is: John Smith
Person is: Robert Hickle

Pay close attention to Person class declaration public class Person implements Comparable<Person>  which indicates that Person class is a comparable if you remove Comparable<Person> from your Person class and try Collection.sort(persons) , compile will give the following error

Error:(21, 20) java: no suitable method found for sort(java.util.List<com.umeshawasthi.tutorials.corejava.sorting.Person>)
method java.util.Collections.<T>sort(java.util.List<T>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: com.umeshawasthi.tutorials.corejava.sorting.Person
upper bound(s): java.lang.Comparable<? super com.umeshawasthi.tutorials.corejava.sorting.Person>)
method java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))

As a developer, it’s out responsibility to pass the comparable object to Collections.sort() method.

It’s up to you how you want to use the compareTo method and what will be your comparison logic, I have used a simple logic to compare the age of the person but you can use any custom logic to in compareTo()  method.

3. Sort Object using Comparator

java.lang.Comparable interface provides only one way to compare this object with another one, what are the options in case we need to compare 2 objects to sort them correctly.How about sorting Person with name or age?  What if we want to override the natural ordering. We need to create a Comparator for these requirements.

First and super short template is to provide a custom comparator while calling Collections.sort()  method

public class ComparatorDemo {
public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Umesh Awasthi", 35));
persons.add(new Person("Robert Hickle", 55));
persons.add(new Person("John Smith", 40));
persons.add(new Person("David", 23));
persons.add(new Person("David", 63));
Collections.sort(persons, new Comparator<Person>() {
public int compare(Person person, Person person1) {
int name = person.getName().compareTo(person1.getName());
if(name == 0){
return name;
}
return person.getAge() > person1.getAge() ? 1 : person.getAge() < person1.getAge() ? -1 : 0;
}
});
for (Person person : persons) {
System.out.println("Person is: " + person.getName());
}
}
}

Output

Person is: David
Person is: Umesh Awasthi
Person is: John Smith
Person is: Robert Hickle
Person is: David

Check the output, the interesting part is David, which is first and last result of the output.

Another efficient way is to define the Comparators in the Contact itself to reuse them instead of recreating every time

public class ComparatorPerson {
private String name;
private int age;
public ComparatorPerson(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static Comparator<ComparatorPerson> COMPARE_BY_NAME = new Comparator<ComparatorPerson>() {
public int compare(ComparatorPerson one, ComparatorPerson other) {
return one.name.compareTo(other.name);
}
};
public static Comparator<ComparatorPerson> COMPARE_BY_AGE = new Comparator<ComparatorPerson>() {
public int compare(ComparatorPerson one, ComparatorPerson other) {
return one.age > other.age ? 1 : one.age < other.age ? -1 : 0;
}
};
}

Comparator with multi options

public class MultiComparatorDemo {
public static void main(String[] args) {
List<ComparatorPerson> persons = new ArrayList<ComparatorPerson>();
persons.add(new ComparatorPerson("Umesh Awasthi", 35));
persons.add(new ComparatorPerson("Robert Hickle", 55));
persons.add(new ComparatorPerson("John Smith", 40));
persons.add(new ComparatorPerson("David", 23));
persons.add(new ComparatorPerson("David", 63));
Collections.sort(persons,ComparatorPerson.COMPARE_BY_AGE );
for (ComparatorPerson person : persons) {
System.out.println("Person Name Sorted by Age is: " + person.getName());
}
System.out.println("######################################################");
Collections.sort(persons,ComparatorPerson.COMPARE_BY_NAME );
for (ComparatorPerson person : persons) {
System.out.println("Person Name Sorted by Name  is: " + person.getName());
}
}
}

Output

Person Name Sorted by Age is: David
Person Name Sorted by Age is: Umesh Awasthi
Person Name Sorted by Age is: John Smith
Person Name Sorted by Age is: Robert Hickle
Person Name Sorted by Age is: David
######################################################
Person Name Sorted by Name  is: David
Person Name Sorted by Name  is: David
Person Name Sorted by Name  is: John Smith
Person Name Sorted by Name  is: Robert Hickle
Person Name Sorted by Name  is: Umesh Awasthi

 

4. Sort Object using Java8

Java 8 added a new way of making Comparators that reduces the amount of code you have to write Comparator.comparing. Java 8 streaming API provides neater approach. It’s always good to understand how compareTo()  and compare()  method work under the hood

public class Java8CompareExample {
public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Umesh Awasthi", 35));
persons.add(new Person("Robert Hickle", 55));
persons.add(new Person("John Smith", 40));
persons.add(new Person("David", 23));
persons.add(new Person("David", 63));
persons.sort(Comparator.comparing(Person::getName).thenComparingInt(Person::getAge));
for (Person person : persons) {
System.out.println("Person Name is: " + person.getName());
}
persons.sort(Comparator.comparingInt(Person::getAge).thenComparing(Person::getName));
for (Person person : persons) {
System.out.println("Person Name is: " + person.getName());
}
}
}

In this article, we cover Java Sorting Example(Comparable and Comparator) provided by JDK, we will cover some other API which provides the same functionalities with additional features.

All the code of this article is available Over on Github. This is a Maven-based project.

As a bonus, have a look at Apache Common ComparatorUtils which provide some of the ready to use feature for you.

Reference

  1. Comparator
  2. Comparable
  3. Comparator.comparing

Convert String to int in Java

Convert String to int in Java

There are few ways to convert String to int in Java. Below are few of popular ways for converting String to int

  1. Integer.parseInt()
  2. Integer.valueOf()

1.  Integer.parseInt() Examples

Integer.ParseInt()  will return an int primitive value.If efficiency is your concern and you only need primitive value, then using Integer.ParseInt()  is the best option.

Here is an example to convert “50” to an int value

public class JavaParseInt {
public static void main(String[] args) {
String number ="50";
int result = Integer.parseInt(number);
System.out.println(result);
}
}

Here is the output of the above code

50

2.  Integer.valueOf() Examples

You can also use Integer.valueOf() , this method will return Integer object

public class JavaIntegerValueOf {
public static void main(String[] args) {
String number ="60";
Integer result = Integer.valueOf(number);
System.out.println(result);
}
}

Output

60

Actually, valueOf uses parseInt internally. If efficiency is your concern do not use this method to convert String to int or Integer.

3.  NumberFormat Exception

Both Integer.parseInt()  and Integer.valueOf()  can throw NumberFormatException if the string cannot be parsed as an integer or int

public class NumberFormatExceptionExample {
public static void main(String[] args) {
String invalidNumber = "abc";
parseIntNFException(invalidNumber);
}
public static void parseIntNFException(String number) {
Integer result = Integer.parseInt(number);
System.out.println(result);
}
public static void valueOfNFException(String number) {
Integer result = Integer.valueOf(number);
System.out.println(result);
}
}

Output

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at NumberFormatExceptionExample.parseIntNFException(NumberFormatExceptionExample.java:13)
at NumberFormatExceptionExample.main(NumberFormatExceptionExample.java:8)

Note: PraseInt() return a primitive value while valueOf() will always return new Integer object, ideally, if you just want to convert String to an int, it’s always better to avoid creating new Object.

Furthur Reading

It’s always a good habit to read Javadoc for more inside details

  1. JavaDoc valueOf()
  2. JavaDoc parseInt()
  3. NumberFormatException

Download above examples from

  1. GitHub Repository

Java Comparable vs Comparator

Java Comparable vs Comparator

In this articles we will discuss Java Comparable vs Comparator, very often people have  lots of questions when it comes to Java Comparable vs Comparator

  1. What is Comparable and what is Comparator in Java
  2. How do we use them
  3. What is the difference between Comparable and Comparator?
  4. When we should use Comparable vs Comparator

This post does not cover basics about Java’s Comparable and Comparator ( Will cover that in another post ), but this post is more towards getting insight about Comparable vs Comparator.

 

Comparable

public interface Comparable<T>

When a class implement Comparable, the compareTo method of the class defines the “natural” ordering of that object. CompareTo method should follow some general contracts, few of the examples are

  1. Should always be returned 0  for objects when equal ()  comparisons return true.
  2. Comparable is useful when there’s a single natural default comparison.

Comparator

public Interface Comparator<T>

Comparator provides a way to compare 2 objects and can be used to compare objects in a way that might not align with the natural ordering.

One of the common examples is String, String is generally compared alphabetically, ” a”.compareTo(“b”)  would use alphabetical comparisons.If you want to compare based on the String length, writing custom Comparator is a preferred solution.

 

If you do not have control over the source code ( where you can not implement Comparable ), then use Comparator.Comparator is useful when you have multiple ways to compare an object ( e.g you can compare cars by brand and cc etc.).

Both seem to be different, but to summarise, there isn’t much difference.Both ends to similar means.In general implement comparable for natural order, and write a comparator for other sorting or comparison needs.

Please read Java Sorting Example(Comparable and Comparator) to get an understanding of using Comparable and Comparator for sorting objects in Java.

BigDecimal equals versus compareTo

While going through compareTo(T o) method in the Comparable interface, I came across a very interesting finding of BigDecimal equals versus compareTo and though about sharing about it.

Usually, we require that two objects are equal if and only if they are compared as same

For BigDecimal things are really interesting when we check both equals() and compareTo() method
Consider following small program

BigDecimal x = new BigDecimal("2");
BigDecimal y = new BigDecimal("2.00");
System.out.println(x.equals(y));
System.out.println(x.compareTo(y) == 0 ? "true": "false");

We can say that x is equal to y (not object reference), running the above program will show following out

false
true

One of the questions that came to my mind while checking this out is “How compareTo determine that x is equal to y for BigDecimal and what is the difference between compareTo() and equals() methods in BigDecimal”

The answer was in JavaDoc of the equals() method:

Compares this BigDecimal with the specified Object for equality. Unlike compareTo , this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). equals methods takes scale into consideration

To summarise, equals() check if BigDecimal objects are same is every aspect while compareTo only check for numeric values.

run following code and check the output

BigDecimal x = new BigDecimal(2);                           
BigDecimal y = new BigDecimal(2.00);                        
System.out.println(x.equals(y));                            
System.out.println(x.compareTo(y) == 0 ? "true": "false");

This is a tricky part of BigDecimal, if you do not have clarity of this and implement it incorrectly, it can lead to many strange bug/issues in your code.

Bottom line is

Use compareTo() instead of equals() to compare numeric values since equals() takes scale into consideration

Remote debug spring boot application with maven and IntelliJ

I run in to an issue some time back where I was required to  debug a remote spring boot web application using IntelliJ and was not sure what is the best way to do it.

IntelliJ has still some issue to run Spring-boot powered web applications with Tiles and I faced same one while working on Shopizer.

We used command line to run Spring boot application using maven plugin. In this post I will try to outline how I configured IntelliJ to debug remote application.

To debug remote spring boot application make sure you have  “Spring Boot Maven Plugin” plugin configured in your pom.xml file

        org.springframework.boot
spring-boot-maven-plugin
1.5.2.RELEASE

You have 2 way to configure debug option using spring boot maven plugin

  1. configure debug option using <jvmArguments> option
  2. use command line argument
        org.springframework.boot
spring-boot-maven-plugin
1.5.2.RELEASE&
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
...

 

Alternatively we will can pass these parameters using command line and we will go with this option in this post.

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"

By default, the run goal runs in the same process unless jvm arguments or an agent have been specified, above configuration will ensure that you are forking the process and able to debug it.

Next step is to configure IntelliJ to enable debug points, use below steps to configure it

  1. Open run/debug configuration dialogue box and click on the new iconRemote
  2. Create remote configuration , see screen shot belowJVM
  3. Click on the debug button and you are all set to debug remote spring boot applicationDebug

Make sure that you have same port number as used while starting spring boot application using Maven.

 

Build and Run Shopizer 2.0.5

With the latest 2.0.5 release, Shopizer team has done a number of changes including migrating to Spring Boot ( 2.0.5 is based on Spring Boot 1.3.5 ). Please read Shopizer 2.0.5 for more details.

In this post, I will try to give you an overview of downloading Shopizer 2.0.5, build and run it on your local machine. Shopizer comes with a predefined demo DB which can be used out of the box to run demo application.

I am assuming that you have basic understanding of following terms

  1. JAVA ( we will be using JDK 1.8)
  2. Spring
  3. Maven
  4. Build
  5. Elastic Search ( 2.4.1 )

I have following configurations for this post

JAVA

java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode

Maven

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)
Java version: 1.8.0_101, vendor: Oracle Corporation
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.3", arch: "x86_64", family: "mac"

 

I will start by cloning Shopizer from GitHub ( You can use UI client ), for this post I am using Git command line tool

clone shopizer

 

Elastic Search

Shopizer uses Elastic Search for its front end search before we build Shopizer, we need to start Elastic Search server and make sure to update configuration with the correct port number and host information. Please update

configs.properties

file under

sm-core/resources

with the correct value for Elastic Search

Once clone is completed, move to the root folder and run following command to build Shopizer

mvn:clean install

This will make sure that you have your build and packet created by Maven

build

 

Spring boot provides embedded support for Tomcat 8, which mean you don’t need standalone Tomcat to run your application.

Run the following command to start embedded Tomcat 8 for Shopizer 2.0.5

mvn spring-boot:run

application-started

When you run your application, Spring Boot will detect that you have a Spring MVC controller and start up an embedded Apache Tomcat 8 instance, by default.

You are all set to run your shop and admin panel. Go ahead and use following URL to open shop and back-end system for Shopizer

Back-end / Admin : localhost:8080/admin/logon.html

backend-login

use following details to log in to the backend

username : admin
password: password

backend

 

Web shop : http://localhost:8080/shop

shop-frontend

I hope this will help you to setup and run default OOTB Shopizer 2.0.5. Let me know in case you have any questions or comments.

In our next post, we will cover how to setup development environment for Shopizer using IntelliJ IDEA’s

 

 

 

Shopizer 2.0.5 Released

Shopizer team has released version 2.0.5 with a lots of improvements and bug fixes. below are some of the major up-gradations and changes in 2.0.5

  1. We have upgraded code base to 1.8.
  2. QSDL has been replaced completely with JPA.
  3. 2.0.5 is based on Spring Boot and provide all the powers and flexibility of Spring boot.
  4. Spring data JPA
  5. Build issues has been fixed which was preventing deployment in some of the cases

For more details and download, please refer to Shopizer GitHub repository.