Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, 21 April 2020

How to Generate JAR and Use as dependency in Spring Boot

For Generating Jar File-

You can extend your project by maven-assembly-plugin.

What is Assembly Plugin-

An "assembly" is a group of files, directories, and dependencies that are assembled into an archive format and distributed. For example, assume that a Maven project defines a single JAR artifact that contains both a simple application and a web application. Such a project could define two "assemblies" that bundle the application with a different set of supporting scripts and dependency sets. One assembly would be the assembly for the simple application, and the other assembly could be a web application bundled with a slightly different set of dependencies. for more about assembly plugin click here

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
        <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        </configuration>
        <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
            <goal>single</goal>
            </goals>
            </execution>
        </executions>
</plugin>

Include these line in your pom.xml and make build of it by using maven command -

mvn package

After the build you will get 3 jars. The main one will be the same as a usual Maven project, while the second one will have the classifier appended with exec and be the executable JAR. The third jar name will be appended by jar-with-dependencies and will contain your classes with classes added as dependencies in your spring boot application(spring-boot-starter-web, thymeleaf,...), so into the pom of the application where you want to add that project as dependencies you won't have to add dependencies from spring boot project.

Include Generated Jar In your another project -

Steps for adding external jars in IntelliJ IDEA:

  1. Click File from the toolbar
  2. Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
  3. Select Modules at the left panel
  4. Dependencies tab
  5. '+' → JARs or directories
Happy Coding  

Saturday, 2 September 2017

what are Java features ?

Java 5 Features:


    > For-each loop
    > Autoboxing and Unboxing
    > Covariant Return Type
    > Annotation
    > Generics
    > Enum
    > Static Import
    > Varargs
    

Friday, 1 September 2017

Difference between ear jar and war files ?

These files are simply zipped files using java jar tool. These files are created for different purposes. 


.jar files: The .jar files contain the group of .class files and libraries. 


.war files: The war file contains static resources i.e., html, js, css...etc and dynamic resources jsp, servlets etc and other files (web.xml) for necessary for the development of web applications. Combination of these files zipped in to one module called .War file. 


.ear files: The .ear file contains anything Java/J2EE (Enterprise) related files zipped in to one module called .ear file. It is deployed in Application server.

Thursday, 31 August 2017

What is Class Loader ? What are the Types (Hierarchy) of Java Class Loaders

Class Loader is used to load classes and interfaces.


Types of class loader


  • Bootstrap class loader : This class loader is responsible for loading core java API classes like rt.jar Bootstrap class loader is native implementation and so they may differ across different JVMs. Location :- jdk/jre/lib/rt.jar 

Simple Currency Formatter in Java

NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers. NumberFormat also provides methods for determining which locales have number formats, and what their names are.

NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.

Wednesday, 30 August 2017

Search String in Sorted String Array Using Binary search

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.


We basically ignore half of the elements just after one comparison.

Wednesday, 23 August 2017

wrapper class in java

why we need wrapper class?

Java is an object oriented language and can view everything as an object. A simple file can be treated as an object, an address of a system can be seen as an object.

"wrapper can be used to convert any data type into object"

The primitive data types are not object they do not belongs to any class they are defined in the language itself that's why Java is not

Monday, 21 August 2017

public static void main (String args [] ) in Java

Main method in java is entry point for any core java program. Remember we are not talking about servlet, MIDlet or any other container based managed java program where life cycle method are provided to control the execution.

public static void main( String [] ar )

public - Java used this

Wednesday, 12 July 2017

Diffrence between @Controller and @ControllerAdvice in Spring Boot

@Controller 

  • @Controller is used to mark classes as Spring MVC Controller (e.g. a web controller).
  • It is typically used in combination with annotated handler methods based on the RequestMapping annotation.

@ExceptionHandler

@ExceptionHandler works at the Controller level and it is only active for that particular Controller, not globally for the entire application.

HandlerExceptionResolver

This will resolve any exception thrown by the application. It is used to resolve standard Spring exceptions to their corresponding HTTP Status Codes. It does not have control over the body of the response, means it does not set anything to the body of the Response.It does map the status code on the response but the body is null.

@ControllerAdvice

@ControllerAdvice used for global error handling in the Spring MVC application.It also has full control over the body of the response and the status code.