Programming:Java:J2SDK

From Wiki books

Contents

The J2SDK

The Java 2 Software Development Kit is the package containing the the java compiler and other tools needed to convert your java programs into executable class files which can be run by the java interpreter. The J2SDK can be downloaded from java.sun.com for free. Versions are available for windows, linux and solaris and Mac.

Compiling java programs

Once you finish writing a java program and save it as a .java file you can compile it. To compile it go to a command prompt or shell or whatever its called on your OS. Change to the directory you have saved the file in(the command is normally cd) and type:

javac your_file_name.java

If there are no errors the compiler will exit and the prompt will return otherwise you will be alerted of any errors which you have to repair. Now a .class file will be generated which contains the bytecodes of your program.

Running Java programs


Now that your program has successfully compiled you are ready to run it. Type this in the prompt:

java your_file_name

Remember, do not put the extension .java/.class while executing the command or it will not work.

HelloWiki.java example



    1) Create a file called HelloWiki.java

    The HelloWiki.java file will contain the following:

    public class HelloWiki {

       public static void main(String[] args) {
           System.out.println("Hello Wiki");
       }
    

    }



    2) Compile the file called HelloWiki.java

    javac HelloWiki.java


    3) Run your new HelloWiki class


    At the command line, issue the following:

    java -classpath . HelloWiki
    


    The class will then print out the following:


    Hello Wiki