Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Sunday, 15 February 2015

Custom Annotation in java


1. source - discard compile time.
2. compile time - annotation exist in class file but not in vm
3. run time - annotation exist in run time vm knows these types of annotation(Hibernate,Spring use these annotation for understanding the class at run time). below example is run time annotation


package test;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface Test1{
public String name() default "vimal";
}
@Test1(name="vimalesan")
public class CustomAnnotationPro {
public static void main(String arg[]) {
Class<CustomAnnotationPro> c=CustomAnnotationPro.class;
if(c.isAnnotationPresent(Test1.class)){
Annotation annotation=c.getAnnotation(Test1.class);
Test1 t=(Test1)annotation;
if(t.name().equals("vimal")){
System.out.println("No Change");
}
else{
System.out.print("New value "+t.name());
}
}

}
}


Wednesday, 6 November 2013

Compile and Run java program using another java program (Java Reflection)

CompileRun.java
import java.io.*;
import java.lang.reflect.*;
public class CompileRun
{
 public static void main(String arg[])throws Exception
 {
  System.out.println(“Helo”);
  new com.sun.tools.javac.Main().compile(new String[]
{“HelloWorld.java”});
  Class c=Class.forName(“HelloWorld”);
  Object o=c.newInstance();
  Object ar[]=new Object[]{};
  Class cl[]=new Class[]{};
  Method m=c.getDeclaredMethod(“print”,cl);
  m.invoke(o,ar);
 
 
 }
}

HelloWorld.java 
public class HelloWorld
{
 public void print()
 {
  System.out.println(“Hello World”);
 }
}
Classpath
1)  %classpath%;Q:\SDE7_Tomcat6_JDK6\Java\jdk1.6.0_24\lib\*
2) javac CompileRun.java
3) java -cp “Q:\SDE7_Tomcat6_JDK6\Java\jdk1.6.0_24\lib\*;.” CompileRun

Friday, 27 September 2013

Program - Java Magic Word - cafebabe

import java.io.*;

public class MagicWord{
public static void main(String args[]) throws Exception{
FileInputStream fis=new FileInputStream("MagicWord.class");
int ch;
int count=0;
while((ch=fis.read())!=-1){

System.out.printf("%x",ch);
count++;
if(count==4)
break;
}
}
}

Thursday, 26 September 2013

Simple simulation for how trial software program works in java

import java.io.*;
class Welcome
{
 public void print(){
 System.out.println(“Welcome to learn java”);
 }
}
public class filedel
{
 public static void main(String args[]) throws Exception
 {
  String name=”magic1″;
  boolean flag=false;
  String str=”";
  StringBuffer bu=new StringBuffer();
  RandomAccessFile rand = new RandomAccessFile(“filedel.class”,”rw”);
  int i=(int)rand.length();
  rand.seek(0);  //Seek to start point of file
  for(int ct = 0; ct < i; ct++){
  byte b = rand.readByte();
  bu.append((char)b);
  if(flag==true)
  {
  rand.seek(rand.getFilePointer()-1);
  char out=(char)b;
  str=out+”";
  if(Integer.parseInt(str)==3)
  {
   rand.close();
   File f=new File(“filedel.class”);
   f.delete();
   break;
  }
  rand.writeByte(b+1);
  break;
  }
  if(bu.indexOf(“magic”)!=-1)
  flag=true;
  
  }
  System.out.println(“your currently running “+str+” time this program”);
  Welcome w=new Welcome();
  w.print();
  rand.close();
 }
}

1) save into filedel.java
2) javac filedel.java
3)
java filedel
java filedel
java filedel
java filedel

run class file more than  3 times. see what happen.

Sunday, 8 September 2013

JAVA – Just Another Virtual Architecture


Java is one of the most popular programming languages. Old name for java is OAK. It is one of the object oriented programming language. Lot of open source application are developed using java.

Java is platform independent language. Java program stored with .java extension(source code). Java compiler compiles source code and converts into class file. Class file load and execute using java interpreter. Java compiler is machine independent. Java interpreter is machine dependent (JRE).

Write a simple hello world program in java

public class HelloWorld{
          public static void main(String arg[]){
                   System.out.println(“HelloWorld”);
}
}

Step for compile and execute the java file

1.     Save the code as a HelloWorld.java
2.     Compile. cmd>Javac HelloWorld.java
3.     Run. cmd>Java HelloWorld

Note: After compile, you get HelloWorld.class file where you stored in the HelloWorld.java location. This class is machine independent you can copy and paste into some other machine and try to run.