Tuesday 29 May 2018

Jenkins address already in use on Linux

Problem : Address already in use in Jenkins.


ava.io.IOException: Failed to start Jetty
        at winstone.Launcher.<init>(Launcher.java:156)
        at winstone.Launcher.main(Launcher.java:354)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at Main._main(Main.java:294)
        at Main.main(Main.java:132)
Caused by: java.net.BindException: Address already in use
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:437)
        at sun.nio.ch.Net.bind(Net.java:429)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:298)
        at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
        at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at org.eclipse.jetty.server.Server.doStart(Server.java:431)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at winstone.Launcher.<init>(Launcher.java:154)
        ... 7 more


Solution :


lsof -i tcp:8080


This should list pids for applications using port 8080. Once you have the pid you can kill the process with kill command


kill -9 <PID> #where <PID> is the process id returned by lsof


This will solve the problem.


Tuesday 22 May 2018

Angular 2 with NPM hello world


Step 1 : Installing Nodejs

To access to the Node Package Manager (NPM) and run our server, we need to first install Nodejs.

You may already have it installed. To check whether it's installed, visit your console and type:

node -v

If it goes unrecognized, you will need to visit Nodejs.org and visit the downloads page. Choose the installer appropriate for your OS, and install it.


Once complete, reload your console/command line, and re-run the


This time it should provide you with the version installed.


Step 2: Installing Angular

We're going to use the Angular CLI to create our Angular app. Let's install it at the command line through NPM.

npm install @angular/cli -g

  
Give it some time to install once installed go into the any folder where you prefer to store your projects and then run the following command:

ng new mean

cd mean

Once you're in the new project folder, we're going to run a command with the Angular CLI that will create a build of our project. We need to do this because our server is going to look for a /dist folder to serve the files. Run below command to build your project.

ng build



Now Run the Angular 2 application by typing below command.

ng serve


Now, open the browser then go to 
http://localhost:4200 

you should see angular page.


Close the running Angular app first by press ctrl+c



Monday 21 May 2018

Constructor in 5 mins




Q. What is constructor.

Constructor is a special member function which has same name as class name and called whenever the object of that class is created.


Q. What are the properties of constructor 

Constructor has following properties:

  • Constructor has same name as class name.
  • It is used for initializing variables of class.
  • It is called whenever object of class is created.
  • It does not have return type, not even void.
  • It can have parameters.


Q. How many types of Constructors are there.

  • Default Constructor       -->  public Message()
  • No-arg Constructor       -->  public Message()
  • Parametrized Constructor     -->  public Message(String str)
  • Copy Constructor                                



Q. What are rules for writing Constructor:


  • Constructor(s) of a class must has same name as the class name in which it resides.
  • A constructor in Java can not be abstract, final, static and Synchronized.
  • Access modifiers can be used in constructor declaration to control its access 
  • It can have parameters
  • It should not contain return type.
  • It can have throws clause: we can throw exception from constructor.


Q. What happens if you keep a return type for a constructor?

It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name


Q. How to invoke constructor.

        Message m= new Message();


Q. What is the difference between constructors and other regular methods?



Constructor
Regular method
Constructors must have the same name as the class name and cannot return a value .

public class Message {

   
private String msg;   
   
//constructor
   
public Message() {}
//parameterize constructor
   
public Message(String str){
        this
.msg=str;
    }
}

The constructors are called only once per creation of an object.
Regular methods can have any name and can be
 called any number of times.

public class Message {
   
//Method
   
public String getMsg() {
    
System.out.println("hello method");
       
return "TestMethod";
    }
}

regular methods can be called many times.



Q. What happens if you do not provide a constructor?

If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit constructor.

public Message() {}.


If a class includes one or more explicit constructors like


     //Non-parameterize or NO-ARG constructor
    public Message() {}

    //parameterize constructor
    public Message(String str){
        this.msg=str;
    }


Then java compiler does not create the default constructor public Message() {}.


Q. How a no – argument constructor is different from default Constructor?

By default compiler creates a default constructor with no formal parameters and no throws clause.

And If you have added your own constructor (no matter whether it's without parameters or with parameters) the compiler will not add the default constructor in this case.


Q. What is Constructor Chaining ?

Constructor Chaining is calling another constructor from one constructor.


Q. How you call one constructor from another? 

By using this() syntax.


package com.thread;

public class Message {

    // parameterize constructor
    public Message(String str) {
        System.out.println(str);
        System.out.println("String constructor");
    }

    // parameterize constructor
    public Message(Integer number) {
        // calling another constructor
        this("Calling String constructor");
        System.out.println("Integer constructor");
    }

    public static void main(String[] args) {
        Message m2 = new Message(3);
        Message m3 = new Message("Test");

        System.out.println(m2);
        System.out.println(m3);
    }

}

//Output
String constructor
Integer constructor
Test
String constructor


Q. How to call the superclass constructor?

You can use the keyword Super to invoke the super class’s constructor.

super("Your Text");


Q. Can we declare constructor as private?

Yes we can declare constructor as private.


Q. How to write  parameterize constructor? 


package com.thread;

public class Message {

    private String msg;
    private Integer number;

    // constructor
    public Message() {
        System.out.println("Empty constructor");
    }

    // parameterize constructor
    public Message(String str) {
        System.out.println("String constructor");
        this.msg = str;
    }

    // parameterize constructor
    public Message(Integer number) {
        System.out.println("Integer constructor");
        this.number = number;
    }

    public static void main(String[] args) {
        Message m1 = new Message();
        Message m2 = new Message(3);
        Message m3 = new Message("Test");

        System.out.println(m1);
        System.out.println(m2);
        System.out.println(m3);
    }

}
Output:
Empty constructor
Integer constructor
String constructor



Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...