Latest News

Tuesday, March 28, 2017

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();

  }


}