Pages

Monday, 30 November 2020

Sunday, 29 November 2020

Add Project to GitHub Using Git Base

 HOW TO ADD A PROJECT TO GITHUB USING GIT BASE


  • Open github
  • Create repository
    • Now you have command to use 
    • You can just follow steps and copy - paste it
  • Open gitbase
  • Go to the file location
  • Enter command in gitbase
    • For location right click on project
    • Properties
    • Resource
    • Location
    • Copy location path
Commands:
  1. $ cd "<path>"
  2. $ git init
  3. $ git remote add origin http------------ (code copied from git repo)
    • If Initialized found
      • Ok
    • Else if Reinitialized found
      • Better to remove
      • $ rm -rf .git
      • $ git init
  4. $ git add .
  5. $ git commit -m "first commit"
    • Dot(.) means, add all code from current location
  6. $ git branch -M main
  7. $ git push -u origin main
Refresh repository

--Thanks

Java | Spring Boot Project | Firebase For Database | GitHub | Heroku To Deploy Project LIVE

STEP 1:          CREATE SPRING BOOT WEB PROJECT
  • New Spring Starter Project
  • Web
  • New packages
    • com.example.demo
    • com.example.demo.controller
    • com.example.demo.dbinit
    • com.example.demo.model
    • com.example.demo.service

STEP 2:          INITIALIZE FIREBASE METHOD    
  • New class in dbinit
     @Service                                          //to treat as a service class
     public class InitializeFirebase {
   
         @PostConstruct                           //to execute after run
        public void initializeFirebase() throws IOException {
        }

     }

STEP 3:          FIREBASE LOGIN
  • Go to Console
  • Select Cloud Firestore
  • Start Collection
    • Collection ID
      • "beginner"
    • Document ID
      • xyz123
    • id = String | xyz123                   
      •  //Document Id and id can be same, else system can autogenerate Document ID      

STEP 4:          FIRESTORE CLOUD CODE TO CONNECT
  • Click setting icon near Project Overview * (left bar)
  • Go to Project Setting 
  • Service accounts
  • Java
    • COPY THE CODE

STEP 5:          CODE TO INITIALIZE FIREBASE
  • Paste code in initializeFirebase() method
     @PostConstruct
     public void initializeFirebase() throws IOException {

          FileInputStream serviceAccount =
             new FileInputStream("AAAAAAA.JSON");
          FirebaseOptions options = new FirebaseOptions.Builder()
             .setCredentials(GoogleCredentials.fromStream(serviceAccount))
             .setDatabaseUrl("https://BBBBBBB.com")
             .build();

          FirebaseApp.initializeApp(options);

      }

STEP 6:          ADD DEPENDENCIES
  • Go to maven Repositories
  • Search firebase admin
  • Any compatible version Copy the code
    • Here 6.11.0 has been taken
  • Add code in pom.xml
  • Import packages to initialize firebase in STEP 5.

STEP 7:          GENERATE NEW PRIVATE KEY

  • Switch to STEP 4
  • Click on Generate new private key
  • Download json file
  • Paste it into the project folder
    • You can right click on project in IDE then paste it
  • Now copy the json file name and paste into
    •  FileInputStream serviceAccount =
                   new FileInputStream("./<filename>.JSON");

STEP 8:          CREATE MODEL: Student
  • id
  • name
  • age
  • place
Now generate
  1. field constructor
  2. default constructor
  3. setters getters
  4. toString() method


STEP 9:          HOME.JSP
  • Go to folder src (not in package src but in folder src)
  • Create folder webapp
  • Create new jsp file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student</title>
</head>
<body>
    <form action="/save">
        id: <input type=text name=id value="1000"><br>
        name: <input type=text name=name><br>
        age: <input type=text name=age><br>
        place: <input type=text name=place><br>
        <input type=submit value=save>
    </form>
</body>
</html>


STEP 10:          CONTROLLER SETUP
  • Create class StudentController in controller package
     @Controller
     public class StudentController {

        @RequestMapping("/")
        public String home() {
            return "home.jsp";
        }

     }


STEP 11:          RUN APP
  • Run as Spring Boot App
  • Open Browse
  • Go to Localhost:8080
    • It will download something
    • That is jsp page
Actually project doesn't have any library to run jsp

  • Go to maven repositories
  • Search Tomcat Jasper
  • Version should be same as 
    • In project go to maven dependency library
    • Check the version of tomcat-embed-core-9.0.39.jar
    • Tomcat Jasper version should be same as tomcat-embed-core version
  • Add dependency to pom.xml

STEP 12:          RELAUNCH PROJECT AFTER TOMCAT JASPER
  • Now page will be visible
id: ________________
name:_____________
age:_______________
place:_____________
<save>


STEP 13:          CONTROLLER FOR SAVE

    @RequestMapping("/save")
    public String saveStudent(Student student) {
         
        System.out.println("successfully saved");
        return "home.jsp";

    }

  • Relaunch Project:
  • Refresh Browser
  • Click save button
  • In console "successfully saved" printed
  • So save button working properly


STEP 14:          SERVICE CLASS TO SAVE DATA
  • Make a service class
     @Service
     public class StudentService {

            public static void saveStudent(Student student) {
            
             }
      }


STEP 15:          FIREBASE CODE TO SAVE DATA
  • Open firebase
  • Go to docs
  • Cloud firestore
  • Add and manage data
  • Add data
  • Select java
  • Copy the code(only required)
  • Paste it into the saveStudent() method (STEP 14)
  • Replace 
    • Collection name
    • Document
    • Object

     public static void saveStudent(Student student) {
         
            Firestore dbFirestore = FirestoreClient.getFirestore();
            ApiFuture<WriteResult> future = dbFirestore.collection("beginner").document(student.getId()).set(student);

        }


STEP 16:          CALL SAVESTUDENT() METHOD IN CONTROLLER

    @RequestMapping("/save")
    public String saveStudent(Student student) {
   
        StudentService.saveStudent(student);
        System.out.println("successfully saved");
        return "home.jsp";
    }


STEP 17:          ADD DATA TO FIREBASE CLOUD
  • Relaunch project
  • Add data
  • Open firebase cloud 
  • Data is added successfully


STEP 18:          ADD PROJECT TO GITHUB
  • Open github
  • Create repository
    • Now you have command to use 
    • You can just follow steps and copy - paste it
  • Open gitbase
  • Go to the file location
  • Enter command in gitbase
    • For location right click on project
    • Properties
    • Resource
    • Location
    • Copy location path
Commands:
  1. $ cd "<path>"
  2. $ git init
  3. $ git remote add origin http------------ (code copied from git repo)
    • If Initialized found
      • Ok
    • Else if Reinitialized found
      • Better to remove
      • $ rm -rf .git
      • $ git init
  4. $ git add .
  5. $ git commit -m "first commit"
    • Dot(.) means, add all code from current location
  6. $ git branch -M main
  7. $ git push -u origin main
Refresh repository


STEP 19:          DEPLOY APP TO HEROKU
  • Open heroku
  • Create new app
    • "test" (use required name)
  • Connect to github
    • Authorized(for first time)
      • Heroku and git
  • Search repo 
    • Connect
  • Enable automatic deploy
  • Deploy branch
App successfully deployes
You will get url
Like "test.herokuapp.com"


-- Thanks --

Friday, 27 November 2020

Create Runnable JAR File And Run It By CMD

How To Create A Runnable JAR File And Run This JAR File By Command Prompt.

step 1: Runnable jar file.

step 2: Run jar file by command prompt.


Runnable jar file

Tuesday, 10 November 2020

Run CMD In Background

How To Run Command Prompt In Background By Java Code?

Follow the instructions:

  • First take a ProcessBuilder object with parameters("cmd.exe","/c","assoc")
    • Put required command instead of "assoc".
  • Give directory name (System.getProperty("user.dir")).
  • Defining true to redirectErrorStream() is recommended
  • Now call start method via ProcessBuilder object.

    Following code can be used to run cmd in backgound and read/print output.

public class Main {
	public static void main(String[] args) throws IOException {
		ArrayList<String> output =  new ArrayList<String>();
		boolean executionStatus=false;
		
		String myDirectory = System.getProperty("user.dir"); 	
		try {
			ProcessBuilder pbuilder = new ProcessBuilder("cmd.exe","/c","assoc");
			pbuilder.directory(new File(myDirectory));
			pbuilder.redirectErrorStream(true);
			Process process = pbuilder.start();
            
		//Reading Output
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line;
			while(true) {
				line = reader.readLine();
				if(line==null)break;
				output.add(line);			
			}
			executionStatus = true;
		} catch (Exception e) {
			e.printStackTrace();
			executionStatus = false;
		}
        
		System.out.println("executionStatus : "+executionStatus);
		System.out.println("output:");
		for(String x:output)
			System.out.println(x);
	}
}

For running Command Prompt directly but not in background, Following code can be used:
Runtime.getRuntime().exec(new String[] {"cmd", "/K", "Start"});

Tuesday, 3 November 2020

Executable File From Runnable Jar File

HOW TO CREATE AN .EXE FILE ?

Make sure the project is compiled and run properly. 

Step 1:    Make runnable jar file of project.

Step 2:    Use Launch4j to configure.

Step 3:    Save configuration.

Step 4:    Build Wrapper.

Here we have taken a java project built in Eclipse IDE. Go to the project in Eclipse IDE.


Runnable jar file

click here to get Runnable jar file.


           

Software Required

Now we need a software named Launch4j

1.    Install Launch4j

2.    Extract zip file.

3.    Run Launch4j.exe


 

Create .exe file

1.    Start Launch4j software

2.    Go to Basic tab.

3.    In Output file section

            a.    Browse project folder in workspace

            b.    Named the file as myschedule.exe

4.    In Jar section

        a.    Browse myshedule.jar (previously created jar file).


5.    As per requirement Classpath and Header tabs can be configured.

6.    Go to Single instance tab.

a.    Select Allow only a single instance of the application

                                          i.    Selection will make sure, there will be a single window of application running at a time.

b.    Fill up the Mutex name

                                          i.    Here myschedule given to mutex name

                                         ii.    You should also give a window title.


7.    Go to JRE tab

a.    Here you can provide Min JRE version to the executable environment.

b.    Our project built in 1.8.0, so it is filled up.


8.    As per requirement Set env. variables and Splash tabs can be configured.

9.    Go to Version Info tab.

a.    You can customize as per your requirements

b.    Select Add version information.

c.     File version: 1.0.0.0

                                          i.    Make sure format of version looks like x.x.x.x

d.    Free form: 1.0.0.0

e.    File description: myschedule

                                          i.    you can describe your project here.

f.      Copyright: 2020


g.    Addition information

h.    Product version: 1.0.0.0

i.      Free form: 1.0.0.0

j.      Product name: myschedule

k.     Company name: Uniconverge Technologies Pvt. Ltd.

l.      Internal name: myschedule

m.   Original filename: myschedule.exe

                                          i.    Extension in compulsory here

 

10.  In Messages tab some messages can be given for the particular event.

11.  Now click Save icon


 

a.    Go to the project folder

b.    Make a folder Launch4j config

c.     Enter the Launch4j config folder

d.    Name the file config

e.    Save it

12.  Click Build wrapper (setting icon)


13.  You will see a message in log

a.    Successfully created


14.  myschedule.exe is created in project folder


15.  If you double click on myschedule.exe, you can see your project is running properly.


- - Thanks - -

Deploy Your Code To Heroku App

GitHub To Heroku Node.js To Heroku  1. HOW TO DEPLOY AN APP TO HEROKU FROM GITHUB