Sunday, October 9, 2022

Auto-Reload Spring MVC Project

This is a much-required process while we develop spring boot applications. We do changes continuously and want to see the result at the same time without manually rebuilding the application. 

For the changes in Java files, we need to include a dependency 

developmentOnly 'org.springframework.boot:spring-boot-devtools'


In Eclipse, this should be enough, I have not tested it though.

If you are using IntelliJ, we need to do one extra step to enable hot-reload.





You need to go to settings (CTRL+ALT+S) and click "Build, Execution, Deployment" select "Compiler", and then on the right side, select the select "Build project automatically".


So far, we have implemented the hot reload of Java files. 

This does not take care of the changes in other files, for example, changes in template files, or other resources files. There are many alternatives to implement this, I prefer using gulp which watches the changes in the resource files and transfers the changes files into the build directory.  

(Reference:https: //attacomsian.com/blog/spring-boot-auto-reload-thymeleaf-templates )

1) Your system should have the latest npm and node. 
2) Instal gulp-client 
npm install gulp-cli -g
Installing globally makes it available to all other applications too.
3) Create a file package.json in the root folder with the following content

{
"name": "Corona Tracker",
"scripts": {
"gulp-watch": "gulp-watch"
},
"dependencies": {
"gulp": "^4.0.2",
"gulp-watch": "^5.0.1"
}
}


4) Run the command npm install (which installs these packages) 
5) Now, we have to define the actual task of watching and taking action. For that, create a file called "gulpfile.js" with the following text:

var gulp =require('gulp'),
watch=require('gulp-watch');

gulp.task('watch',function(){
return watch('src/main/resources/templates/**/*.*',()=>{
gulp.src('src/main/resources/templates/**')
.pipe(gulp.dest('build/resources/main/templates/'));
});
});

The task defined here is "watch"

6) Run "gulp watch" which watches directory templates, and if any changes are there, it copies the changes into the build directory. 


I preferred this method of loading static file changes because it is fast and fully customizable. The method in IntelliJ did not work in my case. (Changing IntelliJ registry did not sound good to me) 

Monday, September 19, 2022

Access Systems in a LAN in Windows 10

Accessing a remote system with a hostname is a bit tricky in windows 10. I went on forgetting after it is done, so writing it as a reference.

It is very easy to map remote ip with a hostname. 

The file is located under

C:\Windows\System32\drivers\etc\hosts

You need to be an administrator or open notepad to change the file. 

In this file, you just need to type ip address of the remote system and the hostname to access it. 

Thats it !

Sunday, February 27, 2022

Scala 3 Introduction

Scala3 is trending and it's a significant upgrade from scala2. In this article, I am going to write a short introduction to what actually does Scala do and how can we start programming with it. I have done some research on the recently released version (called Scala3) and how can we get started with it. 

Preparing Environment

To prepare the development environment for scala, we have to install java, scala, and configure the JAVA_HOME path so that scala knows where the java is installed. The use of sdkman makes the task of managing the SDKs easier, I prefer to install it. 

1) Install SDKMan

The installation of sdkman is simple. Go to this URL and follow the instructions


Commands:
Installation
$ curl -s "https://get.sdkman.io" | bash
Load Environment
source "$HOME/.sdkman/bin/sdkman-init.sh"
Test it 
$ sdk version

2) Install JDK
$ sdk install java

3) Install Scala

$ sdk install scala

4) Install SBT

$ sdk install sbt

You can verify the installation from the command line. 


Create a sample Scala3 project:
sbt new scala/scala3.g8
This will create a sample Scala3 project. We can take this to start a project. 

Saturday, October 17, 2020

Design Pattern Walkthrough

The term Design Pattern is a buzzword in the software development world. Actually, we can develop software even without a design pattern, but software development is a continuous process, changes come and the software should adapt to the changes. So, if the software is developed without any consideration of the design pattern, then it is difficult to scale and maintain. Also, if design patterns are properly used, it's very easy to understand your code to other developers. A design pattern is nothing, but the adapted practices by experienced software developers, so it is a good idea to consider design patterns while we develop software.

Basically, we can divide the design pattern into the following categories:

A. Creational Design Pattern
B. Structural Design Pattern
C. Behavioural Design Pattern
D. J2EE Design Pattern (Only for Java)


A. Creational Design Pattern
1) Singleton
2) Factory
3) Abstract Factory 
4) Builder
5) Prototype 

B. Structural Design Pattern
1) Adapter 
2) Decorator
3) Bridge
4) Composite
5) Facade 
6) Flyweight 
7) Proxy

C. Behavioural Design Pattern
1) Command
2) Observer
3) Iterator
4) Memento
5) Template
6) State
7) Strategy
8) Visitor
9) Mediator 
10) Chain of Responsibility

D. J2EE Design Pattern



Thursday, October 15, 2020

Create USB Windows 10 Installer in Ubuntu System

 As the title clearly says, creating bootable USB in the Ubuntu system is a bit challenging as compared to the Windows system. Windows system provided their own tool and we can seamlessly create a bootable thumb drive. After the less availability of CD ROMs, it is very important to know how to create bootable USB disks. 

Yes, I also tried to use some of the tools I was familiar with, such as UnetBootIn, copying with dd tool, etc, but unfortunately, I was unsuccessful. It creates a bootable USB, but the problem, the installation of windows crashes, with some file related error. 

Because I did not have any windows system in my machines, and I have to install windows in my newly bought notebook, I have to create anyhow a bootable Windows 10 installer using my Linux machine. I carried out some research on this, got a working tool to create a bootable Windows installer on USB.

1) Installation of WOEUSB command

What I did is, I have first of all run a couple of Linux commands to add the repository, then install the tool.

sudo add-apt-repository ppa:nilarimogard/webupd8

sudo apt udpate

sudo apt intall woeusb

Now installation completes. 

2) Get USB Info

We have to find out the usb device id (for example: /dev/sdb or /dev/sdc). Using one of the following commands, we can find out that information:

sudo fdisk -l

lsblk 

3) Unmount the USB Disk

This is important, we have to unmount the USB before carrying out the problem. There is a simple command to carry out this task:

sudo umount /dev/sdb1

3) Create the installer 

The final step is to run a command to create the actual installer. 

sudo woeusb --target-filesystem NTFS --device Win10_2004_English_x64.iso /dev/sdb

Here, Win10_2004_English_x64.iso is the iso file downloaded from the Microsoft site. Which can be freely downloaded. 

/dev/sdb is the path of the USB disk.

It can take up to 5-6 minutes depending upon how fast your system is.

After the process finishes, we are ready to use it as a windows 10 installer. 



Friday, April 10, 2020

Creating multipurpose executable Jar file

As the title says, this recipe is focused on creating a multipurpose jar file, where a single jar file can be used for multiple requirements. I am going to cover the whole process by diving into the following steps:
1) Create a Java application
2) Bundling into an executable Jar file
3) Running a Jar file

I take a problem scenario to explain the above steps and how they are carried out.

Problem:
We want to have a java application, which should have addition, subtraction, multiplication and division functionalities,  that we can call those functionalities providing the command line parameters. For example

add 2 4 4 (result=2+4+4)
subtract 54 12  (result=54+12)
multiply 12 54 55 (result=12*54*55)
divide 34 17 (result=34/17)


So, let's start:
1) Create a Java application
I am creating a Java application with Gradle as a build tool because it is easy to use. I am creating 4 classes with the main method which is responsible for corresponding operation and parameters. One option could be to create a single method and get an operation as a parameter, which makes it really complex to handle parameters. If we create a main method for each operation, it makes our tasks of handling parameters easy.

So, I begin with creating four java classes with main method, which manipulates the arguments to get the corresponding result. Because the example task is very simple, our actual task could be way bigger with many dependent libraries. So, we define a bunch of libraries in "build.gradle" file and we create a "fatJar" with all dependencies included in the jar file. An alternative will be we copy dependent libraries in the classpath.

I begin with the fatJar method, which create a jar file with all dependent libraries automatically from gradle. For this, we have to write a method to create fatJar. Have a look at the sample build.gradle file.

File: build.gradle



settings.gradle



The method jar create the jar file without dependencies. But the fatJar create a jar file with all dependencies, and we can use out of the box.

So, after definition of Gradle build information.

Now, we create class files with specific operation in main methods. The class files look something like this:

Add.java



Subtract.java



Multiply.java



Division.java



The application structure looks like this:



So far, we have created a java application with the required operations.


2) Bundling into an executable Jar file

Bundling into an executable Jar file is quite easy because we have already written a method in build.gradle file above. So, we just call

gradle clean && gradle fatJar

The created Jar file will be in folder build/libs as shown above(Operations.jar)

Note: If we simply run "gradle build", then it will create a thin jar file(i.e. without dependencies)


3) Running a Jar file

The Last Step is to execute the Jar file Operations.jar. The bundled jar file includes all the classes, and we can run all operations providing corresponding arguments.
For example:
java -cp Operations.jar com.kpaudel.operations.Add 12 34.33 45.3

Result=91.63

Similarly, we can check all other operations providing the parameters. 

java -cp Operations.jar com.kpaudel.operations.Subtract 123.56 23
Result=100.56

java -cp Operations.jar com.kpaudel.operations.Multiply 12.2 34.5
Result=420.9

java -cp Operations.jar com.kpaudel.operations.Division 12 2 2
Result=3.0


So, we can independently manage each operation with their own arguments, which makes very easy to handle input arguments. Apart from this advantage, bundling into a single jar file makes it easy to deploy, which means only a single jar file with many operations can be called separately.

I am using this method to create download and upload operations for my cloud storage, getting advantage of using the same jar file for different purposes of download and upload operations(with independent input parameters.) 


I hope you have enjoyed this recipe.  Thanks for reading :)

Wednesday, October 30, 2019

LED Strips Programming (Arduino)

Today, I am going to share my experience on the LED Strips programming using Arduino. It will be fantastic to decorate on Christmas nights, which you can program each LEDs as required like we could create a cool animation.

This article does not go into detail. This will just give you the idea on how we can program LED strips and you can program the lights according to your need.

Let's begin, we need the following items before we begin the programming.

1) Arduino
2) Power Source (10 V)
3) Wires
4) LED Strip

We connect the power source to one end of the led strip. Normally positive wire of the strip is red, and the negative is white. The white is grounded with the ground of the strip. Strip has also cables to be connected to the Arduino. GND should be connected to the negative power supply. Note that the green color cable is used for the color signal which we normally connect to PIN 7 of Arduino.

If you are using an external 10V power supply, we connect the positive to the red and negative to the white one. NEVER connect the red one into the 5V pin of the Arduino which burns it. I have burnt 3 Arduino by mistakenly connecting this 10V in 5V pin in Arduino.

So, the connection will be like this:

1) Without external power
If we DON'T want to use external power, then we need to use an Arduino +5V pin as a power supply to the LED strip. Please note that, because Arduino can't deliver enough power, we have to compromise the brightness of the LED Strips.

The connection diagram will be as follows:


2) With external power supply
If the Arduino power is not enough for the LED strips, we need external power supply. I have used 15V 5A power supply.  Please be very careful while connecting external power supply. Note that if you mistakenly connect your +15 V in the +5V pin, then you will burn your Arduio which I did 4 times!
So, if you using an external power supply, never connect the red (positive) +15V to the Arduino. But the negative of the power supply (normally white line) should be connected to the GND of the Arduino. The PIN 7 should be connected to the green line DIN of the strip. 
Please look into the connection diagram

Please note that the +15V red line is not connected to the Arduino.

Now we come to our actual task: programming. Before we start we have to install the needed libraries.

We need Adafruit_Neopixel and optional Tinkerkit libraries. Adafruit library is used to actual programming while Tinkerkit libraries make it easy to read sensor values because we are using sensors to control the led lights.

The code is made downloadable from git repository:


This example code is just a kind of animation I have created. But we can use the power of
Arduino to define cool animations in your own way.
Just try yourself :)