Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Run JAR Applications

If there is only 1 class with a main method or if there is a Main-Class defined for the JAR, you can use the following command to run the application.

java -jar app.jar

If you there are multiple classes with main methods in the JAR, you can execute any …

Check Whether an Email Address Is Valid in Java

See the following code.

String email = "test@test.com";
Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
Matcher m = p.matcher(email);
boolean matchFound = m.matches();
if(matchFound){
    System.out.println("EMAIL OK");
}else{
    System.out.println("EMAIL ERROR");
}

Copy Arrays in Java

There are several different ways to copy arrays of object (primitive types) in Java. However, the fastest way seems to be System.arraycopy. This methed is implemented using native code and only performs a shallow copy. Acutally most methods for copying arrays in Java perform shallow copy.

int arr1[] = {0 …