Monday, September 12, 2011
Java Single Application Instance
*
* @author
*
* V Prakash
*
* Software Engineer
*
*
*
*/
import java.net.ServerSocket;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.io.IOException;
import java.net.BindException;
public class SingleInstance
{
public static ServerSocket serverSocket;
public static String errortype = "Access Error";
public static String error = "Application already running.....";
public static void main(String as[])
{
try{
//creating object of server socket and bind to some port number
serverSocket = new ServerSocket(16892);
//do not put common port number like 80,8080,8081 etc.
//Because they are already used by system
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(200, 200);
}
catch (BindException exc){
JOptionPane.showMessageDialog(null,error,errortype,JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch (IOException exc){
JOptionPane.showMessageDialog(null, error, errortype, JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
Tuesday, August 9, 2011
Features of Java 7.0
In this blog, I am focusing on new features added to Java 7.0 under the project - Project Coin, which contains small enhancements related to Java language.
- Support for Strings in Switch
- Improved Type Inference for Generic Instance Creation
- Automatic Resource Management
- Binary integral literals and underscores in numeric literals
- Multi-catch and more precise rethrow
Support for Strings in Switch
Starting from Java 7, switch statement supports strings. It means you can have a string as the expression in switch and compare it with strings given in case statements.Here is an example for a switch with String.
import java.util.Scanner;
// switch supports strings
public class StringSwitch {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.print("Enter the name of programming language you use : ");
String lang = s.nextLine(); // read a line from keyboard
switch(lang) {
case "java" :
System.out.println("Java Programmer"); break;
case "c#" :
System.out.println(".Net Programmer"); break;
case "c++" :
System.out.println("C++ Programmer"); break;
default:
System.out.println("Programmer"); break;
}
}
}
Improved Type Inference for Generic Instance Creation
Until now when you create an object of a class with parameterized types, we need to specify the type both in the declaration (left of =) and also in the call to constructor (right of =).For example here is an example in Java 6.0.
MapThe types to be used on the right of HashMap are evident from the declaration. So it is redundant but needed in Java 6.0.> anagrams = new HashMap >();
Start from Java 7.0, you are allowed to omit type information at the time of creating an object of class with generics if the information can be inferred from declaration. That means the following is enough in Java 7.0 for the declaration above in Java 6.0
MapThe above statement simplifies the declaration by removing redundancy related to specifying types in generic declarations.> anagrams = new HashMap<>();
Automatic Resource Management
A resource is an object that is to be closed manually. An example is an OutputStream, a Connection or a Socket. Automatic resource management statement is a form of the try statement that declares one or more resources. The scope of these resource declarations is limited to the try statement. When the try statement completes, whether normally or abruptly, all of its resources are closed automatically. For example the following is the code we have in Java 6.0 to open and close a stream.FileOutputStream fos = new FileOutputStream(path);Here is the modified version that uses Automatic Resource Management of Java 7.0.
try {
// code to process fos
}
catch(Exception ex) {
System.out.println(ex);
}
finally {
fos.close();
}
try (FileOutputStream fos = new FileOutputStream(path))
{
// code to process fos
}
catch(Exception ex) {
System.out.println(ex);
}
Binary integral literals and underscores in numeric literals
Binary integer literals are just like hexadecimal integer literals except that binary digits are used instead of hexadecimal digits. Binary literals are prefixed by "0b" rather than "0x". Here is an example for binary literal.In numeric literals, underscores are allowed as separators between digits. This applies to literals in binary, octal, hexadecimal, or decimal and applies to floating-point literals also.
int n = 0b10000000;
System.out.println(n); // prints 128
int n2 = 121_31_23_232; // integer literal with underscores
int n3 = 0xff_dd; // hexa literal with underscores
System.out.println(n2);
System.out.println(n3);
Multi-catch and more precise rethrow
Java 7.0 allows a single catch block to handle two or more exception by listing exception types separated by | (or) symbol. This allows one catch block to handle multiple exceptions and take the same action for multiple exceptions.try {
int v = Integer.parseInt(num);
int result = 100 / v;
System.out.println(result);
}
catch(NumberFormatException | ArithmeticException ex ) { // multi-catch
System.out.println("Not a valid number or zero");
}
More precise rethrow
If an exception parameter is not modified and if it is rethrown inside the catch block, the compiler applies a more precise analysis to determine the type of what can be thrown.For example consider the following example in Java 6.0.
class Ex1 extends Exception { }In the above code, throw ex; will result in error as ex is of type Exception and method has only Ex1 and Ex2 in throws clause. Exception is much more general than Ex1 and Ex2, so Java doesn't allow us to throw ex, which is declared as Exception.
class Ex2 extends Exception { }
public class RethrowException {
public void m1(int v) throws Ex1 , Ex2 {
try {
if (v < 0) {
throw new Ex1();
}
else
if ( v > 100 ) {
throw new Ex2();
}
// process
}
catch (Exception ex) {
throw ex;
// unreported exception java.lang.Exception; must be caught or declared to be thrown
}
}
}
But the same code without any change will compile in Java 7.0 as Java applies more precise analysis to determine the type of exception being thrown. Java 7 understands only two possible exceptions (Ex1 and Ex2) can be thrown from try block so it allows ex to be rethrown though ex is declared as Exception.
Here are some other links that are related to Java 7.0 features.
Objects Class in Java 7.0
Consider the following program in Java 6.0.
class Person {The above program results in the following program:
private String name, email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object obj) {
final Person other = (Person) obj;
return this.name.equals(other.name) && this.email.equals(other.email);
}
@Override
public int hashCode() {
return 1;
}
public void print(){
System.out.println (name);
System.out.println (email);
}
}
public class ObjectsDemo {
public static void main(String[] args) {
Person p1 = new Person("Srikanth Pragada", null);
Person p2 = new Person("Srikanth Pragada", "srikanthpragada@gmail.com");
p1.print();
System.out.println(p1.equals(p2));
}
}
Srikanth PragadaNullPointerException is thrown in equals() method as we are using email, which is set to null, for comparison. It means, if any variable is set to null, unless we explicitly handle null, will result in NullPointerException.
null
Exception in thread "main" java.lang.NullPointerException
at Person.equals(ObjectsDemo.java:11)
at ObjectsDemo.main(ObjectsDemo.java:31)
This problem of NullPointerException can be prevented by using null-safe operations using Java 7.0 Objects class.
The following are the static methods provided by Objects class.
Method | Description |
---|---|
static | Returns 0 if the arguments are identical and c.compare(a, b) otherwise |
static boolean deepEquals(Object a, Object b) | Returns true if the arguments are deeply equal to each other and false otherwise |
static boolean equals(Object a, Object b) | Returns true if the arguments are equal to each other and false otherwise |
static int hash(Object... values) | Generates a hash code for a sequence of input values. |
static int hashCode(Object o) | Returns the hash code of a non-null argument and 0 for a null argument |
static | Checks that the specified object reference is not null |
static | Checks that the specified object reference is not null and throws a customized NullPointerException if it is |
static String toString(Object o) | Returns the result of calling toString for a non-null argument and "null" for a null argument |
static String toString(Object o, String nullDefault) | Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise |
The following program demonstrates how to use some of these methods.
import java.util.Objects;When you run the above program the following output will be generated.
class Person {
private String name, email;
public Person(String name, String email) {
this.name = Objects.requireNonNull(name,"Name must be not null");
this.email = email;
}
public boolean equals(Object obj) {
final Person other = (Person) obj;
return this.name.equals(other.name) && this.email.equals(other.email);
}
public int hashCode() {
return 1;
}
public void print(){
System.out.println (name);
System.out.println (Objects.toString(email,"Email is null"));
// returns second parameter if email is null
}
}
public class ObjectsClass {
public static void main(String[] args) {
Person p1 = new Person("abc","abc@gmail.com");
p1.print();
Person p2 = null;
System.out.println(Objects.equals(p2,p1));
// null-save equals, if first parameter is null it returns false
p2 = new Person("abc","abc@gmail.com");
System.out.println(Objects.equals(p1, p2)); // null-save equals
Person p3 = new Person("Srikanth Pragada",null);
p3.print();
Person p4 = new Person(null,null);
}
}
abc
abc@gmail.com
false
true
Srikanth Pragada
Email is null
Exception in thread "main" java.lang.NullPointerException: Name must be not null
at java.util.Objects.requireNonNull(Objects.java:226)
at Person.(ObjectsClass.java:7)
at ObjectsClass.main(ObjectsClass.java:42)
Path class and Files class in NIO of Java 7.0
In this blog we use a couple of new classes introduced in new IO of Java 7. Our primary focus is on Path and Files classes.
Path class
Path class in Java 7 is similar to File class in Java 6.The Java I/O File API, as it was originally created, was not written to be extended.
Many of the methods do not throw exceptions even when an error is encountered leaving developers wondering what's happening.
The following code in Java 6.0 will not throw any exception even if the file is not found in the filesystem.
File f = new File("c:\\test.txt");Shown below is the code in Java 7.0 using Path class but it throws exception when file is not found.
f.delete();
Path fp = Paths.get("c:\\test.txt"); // get Path object
Files.delete(fp); // delete file represented by path object
output
======
Exception in thread "main" java.nio.file.NoSuchFileException: c:\test.txt
Method | Description |
---|---|
Path getFileName() | Returns the name of the file or directory denoted by this path as a Path object. |
FileSystem getFileSystem() | Returns the file system that created this object. |
Path getName(int index) | Returns a name element of this path as a Path object. |
int getNameCount() | Returns the number of name elements in the path. |
Path getParent() | Returns the parent path, or null if this path does not have a parent |
Iterator | Returns an iterator over the name elements of this path. |
WatchKey register(WatchService watcher, WatchEvent.Kind... events) | Registers the file located by this path with a watch service. |
Path toAbsolutePath() | Returns a Path object representing the absolute path of this path |
File toFile() | Returns a File object representing this path |
URI toUri() | Returns a URI to represent this path. |
The following code shows some other new methods of Path class.
Path p = Paths.get("c:\\jdk7.0\\bin\\javac.exe");
System.out.println( p.getNameCount()); // 3
System.out.println( p.getName(1)); // bin
System.out.println( p.getFileSystem().getClass());
// display file system - sun.nio.fs.WindowsFileSystem
File f = p.toFile(); // convert to File object
Files class
Files class provides static methods that operate on files and directories.The following are the important methods of Files class.
Method | Description |
---|---|
static Path copy(Path source, Path target, CopyOption... options) | Copy a file to a target file |
static void delete(Path path) | Deletes a file. |
static boolean deleteIfExists(Path path) | Deletes a file if it exists. |
static boolean isDirectory(Path path, LinkOption... options) | Tests whether a file is a directory |
static Path move(Path source, Path target, CopyOption... options) | Move or rename a file to a target file |
static DirectoryStream | Opens a directory, returning a DirectoryStream to iterate over all entries in the directory |
static String probeContentType(Path path) | Probes the content type of a file. |
static List | Read all lines from a file |
static long size(Path path) | Returns the size of a file (in bytes). |
static Path walkFileTree(Path start, FileVisitor visitor) | Walks a file tree |
The following example shows how to use newDirectoryStream() to get list of files from the given path. Though listFiles() method of File class provided this functionality, DirectoryStream is more scalable.
Path path = Paths.get("c:\\jdk7.0");The following code shows how to get the list of lines from a file using readAllLines() methods.
// take files from jdk7.0
try (DirectoryStreamdirectory = Files.newDirectoryStream(path))
// Closes stream at the end (ARM)
{
for (Path file : directory)
System.out.println(file.getFileName());
}
Path p = Paths.get("c:\\jdk7.0\\names.txt");
System.out.println("Size : " + Files.size(p));
Listlines = Files.readAllLines(p, Charset.defaultCharset());
for(String line : lines)
System.out.println(line);
The following example shows how to display the list of files from the given tree in the file system. It walks through the tree that starts with c:\jdk7.0 and displays all files and folder present within jdk7.0 folder.
Method walkFileTree() takes path to start with and an object of a class that extends SimpleFileVisitor class. For each file that is visited, visitFile() method is called. Before a directory is processed, preVisitDirectory() method is called. After directory is processed, postVisitDirectory() method is called.
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import static java.nio.file.FileVisitResult.CONTINUE;
public class WalkTree {
public static void main(String[] args) throws Exception
{
Path p = Paths.get("c:\\jdk7.0");
PrintFiles pf= new PrintFiles();
Files.walkFileTree(p,pf);
}
}
class PrintFiles extends SimpleFileVisitor{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
System.out.println( "File :" + file.getFileName());
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException ioe)
throws IOException {
System.out.printf("Processed Directory: %s\n", dir.getFileName());
return CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes bfa)
throws IOException {
System.out.printf("About to process directory: %s\n", dir.getFileName() );
return CONTINUE;
}
}
Friday, July 1, 2011
Java7 Launch
Time: 10AM
Topic
Speaker
Duration
Quiz and Tshirt (limited) distribution
JSR 334: Small language enhancements (Project Coin)
Marimuthu Rajagopal
60 mins
JSR 292: Support for dynamically-typed languages (InvokeDynamic)
Balamurugan
Concurrency and collections updates (jsr166y)
Prasanna Kumar
30mns
JSR 203: More new I/O APIs for the Java platform (NIO.2)
Ranjith Kumar
60 min
Open Discussion - Unconference
Come up with ideas
Open for all Java enthusiastic and Free registration
Java7 Launch kit will be shared, you can copy on your laptop / memory stick or you can download it.
T-Shirts are limited
If you like to talk on any topic post in the group.
Feel free to invite your friends.
Hashtag for the event #java7 #jugchennai
Register Now Free Registration
We request you to be in the venue on time. Please register If you are really confirming. Your registration help us to plan and execute the event in proper way.
Other Links:
Community Blog: http://www.jugchennai.in
JUGC Project Site: http://java.net/projects/chennaijavausergroup
Group Discussion: http://groups.google.com/group/jug-c
Twitter : http://twitter.com/jug_c
Google Profile:**http://www.google.com/profiles/chennai.jug**
Wiki: http://jug-c.wikispaces.com
Thursday, March 31, 2011
Use Folder Lock Software for protect your personal folder and valuable data
i ll ve been launched as soon as posible..