Showing posts with label create. Show all posts
Showing posts with label create. Show all posts

Monday, 29 March 2021

javax.json: Create json and print

Step 1: Define JsonObject instance by setting necessary properties.

JsonObject json = Json.createObjectBuilder()
.add("firstName", "Ram")
.add("lastName", "Gurram")
.add("id", 32)
.add("male", false)
.add("dateOfBirth", new SimpleDateFormat("DD/MM/YYYY").format(date)).build();

 

Step 2: Conver JsonObject to string.

String result = json.toString();

 

Find the below working application.

 

JsonCreateDemo.java

package com.sample.app;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

import javax.json.Json;
import javax.json.JsonObject;

public class JsonCreateDemo {

	public static void main(String args[]) {
		LocalDate dateOfBirth = LocalDate.of(1985, 1, 10);
		Date date = Date.from(dateOfBirth.atStartOfDay(ZoneId.systemDefault()).toInstant());

		JsonObject json = Json.createObjectBuilder()
							.add("firstName", "Ram")
							.add("lastName", "Gurram")
							.add("id", 32)
							.add("male", false)
							.add("dateOfBirth", new SimpleDateFormat("DD/MM/YYYY").format(date)).build();
		
		String result = json.toString();

		System.out.println(result);
	}

}

Output

{"firstName":"Ram","lastName":"Gurram","id":32,"male":false,"dateOfBirth":"10/01/1985"}



 

 

Previous                                                    Next                                                    Home

Saturday, 9 May 2020

docker: create, start: Create and start the container

'docker create' command is used to create a container, once the container is created, you can start the container using 'docker start' command.

Step 1: Execute the below command to create a container
docker create -p1234:8080 jboss/wildfly

$docker create -p1234:8080 jboss/wildfly
a523924b741accfb04a72d6322c6197c50b4be0b175b8a2d9e087d22d20a1786
$
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   3 seconds ago       Created                                              silly_cori

From the output, you can observe that container is created with id 'a523924b741a'.

Step 2: Start the container using 'docker start' command.
$docker start a523924b741a
a523924b741a

open the url 'http://localhost:1234/', you can able to see WildFly server home page.


You can even execute the command 'docker ps -a' to check whether container started or not.
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                           PORTS                    NAMES
a523924b741a        jboss/wildfly       "/opt/jboss/wildfly/…"   About a minute ago   Up 50 seconds                    0.0.0.0:1234->8080/tcp   silly_cori



Previous                                                    Next                                                    Home

Wednesday, 29 April 2020

Javassist: Create new interface at run time

Using ‘makeInterface’ method, you can create new interface.

Step 1: Get an instance of ClassPool.
ClassPool pool = ClassPool.getDefault();

Step 2: Create an interface using makeInterface method.
CtClass ctClass = pool.makeInterface("com.sample.app.interface.Arithmetic");

Step 3: Define methods
CtMethod sum = new CtMethod(CtClass.intType, "sum", new CtClass[] { CtClass.intType, CtClass.intType }, ctClass);
sum.setBody("return $1 + $2;");

CtMethod areaOfCircle = new CtMethod(CtClass.doubleType, "areaOfCircle", new CtClass[] { CtClass.doubleType },ctClass);

Step 4: Add methods to the interface.
ctClass.addMethod(sum);
ctClass.addMethod(areaOfCircle);

Find the below working application.

App.java
package com.sample.app;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

public class App {

 public static void main(String args[]) throws Exception {
  ClassPool pool = ClassPool.getDefault();

  CtClass ctClass = pool.makeInterface("com.sample.app.interface.Arithmetic");

  CtMethod sum = new CtMethod(CtClass.intType, "sum", new CtClass[] { CtClass.intType, CtClass.intType },
    ctClass);
  sum.setBody("return $1 + $2;");

  CtMethod areaOfCircle = new CtMethod(CtClass.doubleType, "areaOfCircle", new CtClass[] { CtClass.doubleType },
    ctClass);

  ctClass.addMethod(sum);
  ctClass.addMethod(areaOfCircle);

  ctClass.writeFile("/Users/Shared/assistDemos");

 }
}

Run App.java.

You can observe Arithmetic.class file is generated at folder /Users/Shared/assistDemos.
$tree /Users/Shared/assistDemos
/Users/Shared/assistDemos
└── com
    └── sample
        └── app
            └── interface
                └── Arithmetic.class

4 directories, 1 file

Open Arithmetic.class file in decompiler to see the source code.



Previous                                                    Next                                                    Home

Wednesday, 11 March 2020

Create file parent directories if they are not exist

Below snippet is used to create parent directories of the file, if those are not exist.

String directoryPath = "Enter file absolute path here";
File file = new File(directoryPath);
file.getParentFile().mkdirs();



You may like