Tuesday, 26 March 2024

Run springboot jar as service in Linux (or) Azure VM


 Create as .sh file add jar execution command

/usr/bin/java -jar /u01/api/docuchat-api-0.0.1.jar --spring.config.location=file:/u01/api/config/ > /u01/api/logs/springboot.log


in you case you can use only /usr/bin/java -jar jarname.jar 

Create .service file under /etc/systemd/system/ folder

my file name: dora-api-demo.service
Script is below 

[Unit]
Description=dora-demo-api

[Service]
User=root
WorkingDirectory=/usr/local/bin
ExecStart=/usr/bin/bash /u01/api/dora-service.sh
SuccessExitStatus=0

[Install]
WantedBy=multi-user.target

once the script is passed inside the dora-api-demo.service file
need use below command to 
sudo systemctl daemon-reload
sudo systemctl enable dora-api-demo
sudo systemctl start dora-api-demo  (To start the service)
sudo systemctl status dora-api-demo (To check the status of the service)
sudo systemctl stop dora-api-demo (to stop the service)
sudo systemctl restart dora-api-demo (to restart the service)


Thursday, 21 March 2024

Records in java, canonical constructor & compact constructor using java

 Record is a certain type of  class. It is also special type of class.

We can create recording like below

public record EmployeeRecord(String name, int employeeNumber) {

}


We can access the name, employeeNumber, as below

For Example: EmployeeRecord record = new EmployeeRecord("Dinesh", 24);

record.name();

record.employeeNumber();


- if you declare record instance variables are final by default. 

- toString(), hashCode() & equal() are overrided automatically.

- We can't extend any class or record 

- we can implement the interface at end before flower braces

- By default the records is final. we don't required to declare final variable.

- By default Canonical Constructor is created. 

 - We can declare the variable with static final 

- we can declare the static methods 

Canonical Constructor is a constructor that is created with all instance fields as a parameters 

for example from above code 

    public EmployeeRecord (String name, int employeeNumber ) {

        this.name = name;

        this.employeeNumber = employeeNumber;

    }

- if you want override constructor you can do. Why do we required to override constructor is if need to do some validation like age should not be negative then you can throw exception if required.

- We can use Compact Constructor instead of overriding it.

What is the Compact Constructor?

  Compact Constructor with our argument or bracket we can declare need to declare as below

public EmployeeRecord {

     if (employeeNumber < 0) {

        throw new ILegalArgumentException("Not valid number");

    }

}


Wednesday, 4 August 2021

Generating CSR certificate using Keytool

 Perquisites: Need to have Keytool (which is present in the Jdk bin folder)


For generating the key store need to enter in the command prompt the below

keytool -genkey -alias docx -keyalg RSA -keystore docx

Beside alias you need give the file generation name. I have given docx.  you can change the keystore value.

Once you enter in the command prompt the below will be generated

Enter keystore password:

Re-enter new password:

What is your first and last name?

  [Unknown]:  test.test.com

What is the name of your organizational unit?

  [Unknown]:  test

What is the name of your organization?

  [Unknown]:  test

What is the name of your City or Locality?

  [Unknown]:  Singapore

What is the name of your State or Province?

  [Unknown]:  Singapore

What is the two-letter country code for this unit?

  [Unknown]:  SE

Is CN=test.test.com, OU=Agility, O=test, L=Singapore, ST=Singapore, C=SE correct?

  [no]:  yes


Once the activity is completed a file is generated with docx from the above command.


If you required the .csr file to be generate from the docx file need to run the below command.

c:\temp>keytool -certreq -keyalg RSA -alias docx -file docx.csr -keystore docx

once click enter then it will ask for password.

Enter keystore password:

File will be generated.


After getting the root.crt, intermidate.crt & servercertificate.crt file 

Run the below commands in the server 

keytool -import -alias root -keystore docxuat -trustcacerts -file root.crt

keytool -import -alias intermediate -keystore docx -trustcacerts -file intermediate.crt

keytool -import -alias docx -keystore docx -file ServerCertificate.cr

------------------------

once done for applying in the network need to run the below commands


keytool -importkeystore -srckeystore docx.jks -destkeystore docx.p12 -deststoretype PKCS12 -srcalias docx -srcstorepass Agility@123 -srckeypass Agility@123 -destalias docx -deststorepass Agility@123 -destkeypass Agility@123


openssl pkcs12 -in docx.p12 -nocerts -nodes -out docx.key

openssl pkcs12 -export -out docxfinalcrt.pfx -inkey docx.key -in docx.txt

Monday, 26 July 2021

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element mvc:cors


In Spring MVC i tried to apply the cors i got the issue

Multiple annotations found at this line:

- Start tag of element <mvc:cors>

- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element  mvc:cors

before solution below was displayed

 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"

To Solve the issue i changed to (removed version) then worked

 http://www.springframework.org/schema/mvc/spring-mvc.xsd" 





Sunday, 12 January 2020

Interview Question for Java, Advance Java, Spring

Interview Question from top company in this days.

In interview mostly people concentrate on the collections.
This days people are asking like logical and theoretical questions. You should be prefect in the theoretical 

1. Write program for removing the duplicate values from the list without using the collections

import java.util.ArrayList;
import java.util.List;

/**
 * @author DGuntha
 *
 */
public class RemoveDuplicate {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Test");
list.add("Java");
list.add("test1");
list.add("Java");
list.add("Java1");
list.add("Java");
list.add("1");
list.add("2");
list.add("1");
for (int i=0; i < list.size(); i++) {
for (int j=i+1;j<list.size();j++) {
if (list.get(i) == list.get(j)) {
list.remove(j);
}
}
}
list.stream().forEach(s -> System.out.println(s));
}

}

2.Using the collection how to remove the duplicates from the list?
Answer: You can added all the value in to the LinkedHashSet from the list 
public static void removeDuplicatesFromList(List<String> list) {
Set<String> set = new LinkedHashSet<String>();
set.addAll(list);
list.clear();
list.addAll(set);
list.parallelStream().forEach(s -> System.out.println(s));
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Test");
list.add("Java");
list.add("test1");
list.add("Java");
list.add("Java1");
list.add("Java");
list.add("1");
list.add("2");
list.add("1");
removeDuplicatesFromList(list);
}
3. What is ConcurrentModificationException?

ConcurrentModificationException

public class ConcurrentModificationTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> myList = new ArrayList<String>();

myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
myList.add("5");
myList.add("3");

//If Use the iterator and try to remove the value ConcurrentModification exception occurred
// Because count value of the iterator is stored in the abstract class.
Iterator<String> itorator = myList.iterator();
while (itorator.hasNext()) {
String value = itorator.next();
System.out.println("List value : "+value);
if (value.equals("3")) {
myList.remove(2);
}
}
// This will not provide the ConcurrentModification exception because we are moving the list.
for (int i = 0; i< myList.size(); i++) {
if (myList.get(i).equals("3")) {
myList.remove(3);
}
}

}

}


what is stream map in java?

Monday, 24 April 2017

Homework-5.1-BLOG-MONGODB

Finding the most frequent author of comments on your blog
In this assignment you will use the aggregation framework to find the most frequent author of comments on your blog. We will be using a data set similar to ones we've used before.
Start by downloading the handout zip file for this problem. Then import into your blog database as follows:
mongoimport --drop -d blog -c posts posts.json
Now use the aggregation framework to calculate the author with the greatest number of comments.
To help you verify your work before submitting, the author with the fewest comments is Mariela Sherer and she commented 387 times.
Please choose your answer below for the most prolific comment author:

ANSWER:
Elizabet Kleine

db.posts.aggregate([{$unwind:"$comments"},{$group:{_id:"$co
mments.author",count:{$sum:1}}},{$sort:{count:-1}}])

Tuesday, 18 April 2017

System Error 1326 has occurred & Logon failure: unknown user name or bad password using command prompt

Using the windows command prompt

Unable to login into remote machine & following error was displayed in command prompt

System Error 1326 has occurred 
Logon failure: unknown user name or bad password

For solving the above issue

net use \\10.201.81.99\e$ /USER:domainname\username password