Friday, March 9, 2018

How to Set Path for Java In Mac

First step is to find where is java installed. For that open terminal and type the below command

/usr/libexec/java_home
This command will output the location of java installation directory something like this depending upon the version of jdk installed
/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
Next step is to set PATH environment variable. Execute the below command in Terminal
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
export PATH=${PATH}:$JAVA_HOME/bin


 
 

Friday, September 5, 2014

New and Updated JSRs in JEE 7


Java EE 7 contains 14 new and updated JSRs. Java specifications are available at http://www.jcp.org.
  • JSR236:ConcurrencyUtilitiesforJavaEE1.0
  • JSR 338: Java Persistence API 2.1
  • JSR339:JavaAPIforRESTfulWebServices2.0
  • JSR 340: Java Servlet 3.1
  • JSR 341: Expression Language 3.0
  • JSR342:JavaPlatform,EnterpriseEdition7
  • JSR 343: Java Message Service 2.0
  • JSR 344: JavaServer Faces 2.2
  • JSR 345: Enterprise JavaBeans 3.2
  • JSR346:ContextsandDependencyInjectionforJavaEE1.1
  • JSR 349: Bean Validation 1.1
  • JSR352:BatchApplicationsfortheJavaPlatform1.0
  • JSR353:JavaAPIforJSONProcessing1.0
  • JSR356:JavaAPIforWebSocket1.0 

Thursday, March 27, 2014

Regular Expressions Reference Card


Title Reference Explanation Example Matches
Letters Inside Square Brackets [Ww] W or w [Ww]oodwork Woodwork, woodwork

[1234567890] Any digit






Letters outside square brackets ame Matches exact sequence ame -> amersohail794 matches ame





Ranges [0-9] 0 to 9 [0-2] -> amersohail794 matches 794

[a-z] all characters from a to z [a-z] -> amersohail794 matches amersohail

[A-Z] all characters from A to A [a-z] -> amersohail794 matches nothing

[ ] matches space [] -> amer sohail matches space only





Not [^a-z] Not characters from a-z [^a-z] matches 794

[^a-aA-Z] Not characters from a-z and from A-Z [^a-zA-Z] -> amersohail794 matches 794





Beginning of line ^ carret outside the square bracket shows that following character should be matched beginning of the line ^o -> oooohhhh OOOOhhh oooOOOOHHHH will match to first character only and that is "o"





End of line $ $ will match end of the line






Pipe Symbol | for or. A|a is same as [aA] W|w matches W or w





Special Characters *  0 or more occurences of previous character o*O*h*  matches oohhh, ooooooooohhhhh,OOOOOhhhhhh,oooOOOOOhhhhh

+ 1 or more occurences of previous character


? 0 or 1 occurrence of previous character


. Any character


\ is used as escape character [\.] -> This is sample sentence. will match the . In the line

Tuesday, January 28, 2014

Java Programming Terms

 Functional Interface

Its new term in JavaSE 8 for interfaces having just one method e.g. Runnable, ActionListener, Comparator, all are the examples of functional interfaces. Previous name was Single Abstract Method Type (SAM).

Anonymous Inner Classes

Its on the spot implementation of interface without explicitly requiring a name. Usually implementation of interfaces is done by classes and each class does have name but anonymous inner class does not have any name

Lambda Expressions

Short hand or concise notation for implementing functional interfaces than anonymous inner classes.

Immutable and Mutable Objects

Immutable objects are those objects which can not be changed after its creation. String is most famous example of immutable object. There are many pros of immutable objects like they can not be corrupted by multiple threads in concurrent application, least overhead for GC

Guarded Blocks

Its a code block waiting for a certain condition to be true for its execution. It happens in multi threading programs when different threads do different task and they coordinate each other by setting certain conditions / variables.

Generic Type

Generic Type is a generic class or interface that is parametrized over types.

Generic Type Declaration

 Definition of a class with type variables is know as Generic Type Declaration. This type variable can be used anywhere within a class.

The Diamond

In Java SE 7, we can replace the type arguments required to invoke a generic class, with empty set of type arguments (<>) as long as the compiler can determine or infer the type arguments from the content. This pair of angle brackets is known as "the diamond".

Box integerBox = new Box<>();
 
Raw Type
A Generic Class or interface without type arguments is known as raw type. e.g. List is a generic type but simple List is a raw type. We can assign generic types to raw types but assignment of raw type to generic type will give warnings and will be checked at runtime.
 

Updating...

Friday, December 20, 2013

Create a KeyStore to save Secret Key

Secret key is used to encrypt and decrypt the strings like "password" and other secret information. KeyStore is the most secure place to keep secret key. 

Here are the main steps to create new key store and saving a secret key:
  • Get instance of KeyStore using KeyStore.getInstance method. It takes the name of the keystore
  • Once instance of KeyStore is available, load it as empty keyStore
  • Generate the secretKey and save it to keystore for future purposes.
    private final String KEYSTORE_TYPE = "JCEKS";
    private final String KEYSTORE_NAME = "CareKeyStore";
    private final String SECRET_KEY_NAME = "secretKeyAlias";
    private final String KEY_ALGO = "AES";

    private String final pwd ="mypass";

    KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
    ks.load (null,pwd.toCharArray());
    KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGO);
    keyGen.init(128);
    SecretKey secretKey = keyGen.generateKey();
    KeyStore.ProtectionParameter protParam =
                            new KeyStore.PasswordProtection(pwd.toCharArray());
    KeyStore.SecretKeyEntry skEntry =
                            new KeyStore.SecretKeyEntry(secretKey);
    ks.setEntry(SECRET_KEY_NAME, skEntry, protParam);
    java.io.FileOutputStream fos = null;
    try {
           fos = new java.io.FileOutputStream(KEYSTORE_NAME);
           ks.store(fos, pwd.toCharArray());
                       
     }catch(Exception ex){
           logger.error(null,ex);
                       
      }finally {
            if (fos != null) {
                  fos.close();
            }
      }






          

Thursday, July 4, 2013

JPA Reference Card

Collection of basic types