Thursday, 24 October 2013

Running a Java Program from Command Prompt

  • Create a temporary folder C:\mywork.  Using Notepad or another text editor, create a small Java file HelloWorld.java with the following text:
    public class HelloWorld
    {
      public static void main(String[] args)
      {
        System.out.println("Hello, World!");
      }
    }
    Save your file as HelloWorld.java in C:\mywork.  To make sure your file name isHeloWorld.java, (not HelloWorld.java.txt), first choose "Save as file type:" All files, then type in the file name HelloWorld.java.
  • Run Command Prompt (found under All Programs/Accessories in the Start menu).  Type
    C:\> cd \mywork
    This makes C:\mywork the current directory.
    C:\mywork> dir
    This displays the directory contents.  You should see HelloWorld.java among the files.
    C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
    This tells the system where to find JDK programs.
    C:\mywork> javac HelloWorld.java
    This runs javac.exe, the compiler.  You should see nothing but the next system prompt...
    C:\mywork> dir
    javac has created the HelloWorld.class file.  You should see HelloWorld.java andHelloWorld.class among the files.
    C:\mywork> java HelloWorld
    This runs the Java interpreter.  You should see the program output:
    Hello, World!
    If the system cannot find javac, check the set path command.  If javac runs but you get errors, check your Java text.  If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command.  Java is case-sensitive!
  • It is possible to make the path setting permanent but you have to be very careful because your system might crash if you make a mistake. Proceed with extreme caution!In Windows XP, go to Control Panel, choose "System," click on the "Advanced" tab, click on the "Environment variables" button. In the lower list, "System variables," click on Path:

    Click "Edit" and at the end append
    ;C:\Program Files\Java\jdk1.5.0_09\bin
    (or the path to the appropriate folder where the latest version of JDK is installed).  Do not put spaces before the appended path string.
    Click OK on the path edit box and OK on the Ennvironment Variables box.  The new setting will go into effect next time you run Command Prompt.

Monday, 7 October 2013

Final Classes

               You can declare that your class is final; that is, that your class cannot be sub-classed  There are (at least) two reasons why you might want to do this: security reasons and design reasons.
               1.Security: One mechanism that hackers use to subvert systems is to create sub-classes of a class and then substitute their class for the original. The subclass looks and feels like the original class but does vastly different things possibly causing damage or getting into private information. To prevent this kind of subversion, you can declare your class to be final and prevent any sub-classes from being created. The String class in the java.lang package is a final class for just this reason. The String class is so vital to the operation of the compiler and the interpreter that the Java system must guarantee that whenever a method or object uses a String they get exactly a java.lang.String and not some other string. This ensures that all strings have no strange, inconsistent, undesirable, or unpredictable properties.
If you try to compile a subclass of a final class, the compiler will print an error message and refuse to compile your program. In addition, the byte code versifier ensures that the subversion is not taking place at the byte code level by checking to make sure that a class is not a subclass of a final class.
              2.Design: Another reason you may wish to declare a class as final are for object-oriented design reasons. You may think that your class is "perfect" or that, conceptually, your class should have no sub-classes.
To specify that your class is a final class, use the keyword final before the class keyword in your class declaration. For example, if you wanted to declare your (perfect) Chess-algorithm class as final, its declaration would look like this:
final class ChessAlgorithm {
    . . .
}
Any subsequent attempts to subclass ChessAlgorithm will result in a compiler error such as the following:
Chess.java:6: Can't subclass final classes: class ChessAlgorithm
class BetterChessAlgorithm extends ChessAlgorithm {
      ^

1 error