Latest News

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, August 29, 2017

August 29, 2017

How to run java program from command prompt ?


1. Write the following code into NOTEPAD.

public class HelloWorld {

    public static void main(String[] args) {
                System.out.println("Hello, World");
    }
}

2. Now save this file as Hello.java.
3. Open command prompt as an admin and locate directory wherever you save this file.
4. First thing you need to compile java  class to write javac hello.
5. Then after if no error appears then write java hello , output will print.

if you haven,t installed java yet then follow this.
August 29, 2017

How to set up java in windows 10 ?

So first thing you need to perform is install jdk from here

1. Choose jdk as per your system if your system has 32 bit then download 32 bit else download 64 bit.

JDK 86(32-bit)
2. Open system propeties by pressing window key + pause(break) or by following.
System property
3. Now go to the advanced system setting on the letf panel.

4. Now select environment varibales.



5. Go to the system variable and create new and follow below details.


NOTE: Choose whatever path of jdk as in your system. Create same variable in User variable also.

Type java in cmd and if you get output like this than you have successfully set up java in your machine.

🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵🍵




















Tuesday, March 28, 2017

March 28, 2017

Servlet Example using apache server

1.Create new web project in netbeans.



2. Here if you don't get Java Web option on left than follow below steps:




3.In plugins windows search Java Web and EE..





4. After creating Web Project you will have this type of Folder structure...


5. In index.jsp page we will make one simple form to take radius from user...



 


6. Now in jsp file have a look at the
   this means we are linking this form with Area class which we are going to create . Actually this Area is a servlet.


In folder structure right click on package and select servlet.
Note: Servlet name and name in form action tag should be exactly same.

7.In servlet write below code.





import java.io.IOException;

import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

 *
 * @author Batman
 */
public class Area extends HttpServlet {

    /**

     * Processes requests for both HTTP GET and POST
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            double r=Double.parseDouble(request.getParameter("txtrdus"));
            double area=(double)(2*Math.PI*r);
            out.println("Area of CIrcle is: "+area);
        } finally {
            out.close();
        }
    }

    //

    /**
     * Handles the HTTP GET method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**

     * Handles the HTTP POST method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**

     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }//


}

Output:



Download full example here..
March 28, 2017

Installation of Apache web server for JAVA Web Programming

1. Download apache server from here..
              https://www.apache.org/dyn/closer.cgi

2. Extract the folder and you will get following files in apache folder(in my case my folder name is apache-tomcat-6.0.37)

Extracted files



3. Now copy the Apache folder and paste it in the C drive(it must be your system drive).

     In my case PATH:C:\apache-tomcat-6.0.37.

4. Now in order to access server first of all you need to set path for JDK.
if you don't have JDK installed in your machine first install JDK from here..JDK

Now set path variable for JDK.

* Go to My computer and right click on it and select properties.



*On next screen select Advance System Setting.


*On next screen select Advance tab and choose Environment Variable.


* Then select new variable as follow.


* Then fill the field by following path.


For JDK path go through your C drive and look for the following path
C:\Program Files\Java\jdk1.8.0_101
if your machine jdk is located here than use this path as shown in above figure and click ok.

5. So if you already have JDK setup than after step 4 just move here...
   For apache server your machine should have CATALINA variable.
   To set path for CATALINA see following figure.


6. Now just try to run your JAVA web program using this server it could work.

See sample i created one web app:



WebPage of  Program









Friday, September 30, 2016

September 30, 2016

How to create .txt file in java?

You can use bufferedWriter class to create a file and write contents into it.

Example:

public class WriteFromBufferWriter {
     public static void main(String[] args) throws IOException
 {

   File fl = new File("d:/myfile.txt");// location of file

   String str="Write this string to my file and update it";
 FileWriter fw = new FileWriter(fl.getAbsoluteFile()) ;
     BufferedWriter bw=new BufferedWriter(fw);
   fw.write(str);
   bw.close();
     System.out.println("Done");
 
  }

}

You can also use fileoutputstream class to create file.

Example:

class Fileoutputstream {

    public static void main(String[] args) throws IOException {
        
       FileOutputStream fout=new FileOutputStream("abc.txt");
       String s="My name";
       byte b[]=s.getBytes();
       fout.write(b);
       fout.close();
        System.out.println("Success");
        }
    Fileoutputstream(String a1) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}



Thursday, September 29, 2016

September 29, 2016

How to create thread in java?

We can create thread by two methods:
       1.By extends
       2.By implements.

 Example:

class ByExtends extends Thread{ 
public void run()
    {
        System.out.println("thread is running...");
    }
public static void main(String args[]){ 
    ByExtends t1=new ByExtends(); //Create object of ByExtends class
    t1.start();  //start thread
 } 
}


Example:


public class ByImplements implements Runnable {

   
    public void run(){ 
        System.out.println("thread is running..."); 
        } 
         
        public static void main(String args[]){ 
        ByImplements m1=new ByImplements(); 
        Thread t1 =new Thread(m1); //passing class object in thread
        t1.start();  //start thread
         } 
        }


Multiple Thread:

Example:

public class MultipleThread implements Runnable{
   
    public void run(){
        System.out.println("The current Thread = "+Thread.currentThread().getName());
    }
     
}
 class Multithread{
    public static void main(String[] args) {
        MultipleThread mThread=new MultipleThread();
        Thread t1=new Thread(mThread);
        Thread t2=new Thread(mThread);
        Thread t3=new Thread(mThread);
        t1.start();
        t2.start();
        t3.start();
       
    }
}

September 29, 2016

Thread Priroty in java.

Thread Priority:

  1. public static int MIN_PRIORITY=1
  2. public static int NORM_PRIORITY=5
  3. public static int MAX_PRIORITY=10

Example:

 class Test implements Runnable{
    public void run(){
        System.out.println("Priority of current thread="+Thread.currentThread().getPriority());
   //getting default priority
    }
  
}
public class GetPriorityDemo{
    public static void main(String[] args) {
        Test t=new Test();
        Thread t1=new Thread(t);
        t1.start();
    }
}


Set Priority in java: 

class Test1 implements Runnable{
    public void run(){
        System.out.println("Priority of current thread="+Thread.currentThread().getPriority());
    }
   
}
public class SetPriorityDemo{
    public static void main(String[] args) {
        Test t=new Test();
        Thread t1=new Thread(t);
        t1.setPriority(4);//set priority
        t1.start();
        //Thread.yield();
    }
}
September 29, 2016

How to read .txt file in java?


To read the file you can use buffered reader class.



Example:


public class ReadFromBufferReader {

     public static void main(String[] args) throws IOException
 {

   File fl = new File("d:/myfile.txt");//path of your text file

   BufferedReader br = new BufferedReader(new FileReader(fl)) ;
   String str;
   while ((str=br.readLine())!=null)
   {
    System.out.println(str);
   }
   br.close();

  }


}