Friday, May 8, 2009

Sun Java


How to Write Your First Program in Java


from wikiHow - The How to Manual That You Can Edit

Just as we humans use different languages to interact with each other, similarly, computers have their own languages through which we can communicate with them. These languages are broadly of two types: High Level and Low Level Languages.
One very popular High Level language is Java, which can be used to write efficient programs for the internet and the world wide web. Java can also be used to write programs for other environments such as consoles and mobile phones, but its popularity is mainly because of its power to generate robust applications for the internet. Today we'll see how to write a small program in Java. When you begin learning a computer programming language, it is customary to write the "hello world" program as your first. This program tells the computer to display the text "Hello World!" on the computer monitor. It's like telling the world that you are the newest member on board the Java bandwagon. So let's get started!

[edit] Steps



  1. Navigate to www.java.sun.com and download the Java JDK or Java SE, not JRE (Java Runtime Environment). Once its downloaded, install it. Then restart the computer.
  2. Open the text editor and write the following code:
    public class HelloWorld{
    public static void main(String[] args){
    System.out.println("Hello World!");
    }
    }
  3. Save this code as HelloWorld.java. In Windows, make sure you choose the "all files" option from the file type drop down list box, otherwise it will be saved as a text file and not a .java file.
  4. Click start > run and type command or cmd (try each and see which one works for your operating system).
  5. From the command prompt, drill down to the folder where you saved the HelloWorld.java file. (type "cd " you can use tab for auto completion)
  6. Type the following: javac HelloWorld.java and press Enter. You should see that after thinking for a while the computer returns to the command prompt.
  7. Type "java HelloWorld" (no quotes) and see what happens. You should see the text "Hello World" displayed on the monitor right below where you issued the java HelloWorld command.


[edit] Things You'll Need


  • Java Development Kit (can be downloaded from Sun Microsystems site for free). Note that for different operating systems there are different JDK's (JDK is a set of tools created by Sun to assist programmers with writing software using Java) Once you've downloaded the JDK, run the setup to set up the development environment.
  • Text editor -- for Windows, use notepad; for linux you can use VI or emacs


[edit] Tips


  • Java is case sensitive. Remember this.

  • If you don't have admistrator privelidges on the computer your trying to install the SDK on, start the installer and when it gets to a part of the install where theres a checkbox asking if you want to add the SDK to the environment variables, uncheck it. It should install properly after that. The only downside is that you can't compile java programs by typing javac in cmd prompt, you have to use a program like Eclipse, NetBeans, or JCreator (I prefer Eclipse). So download and install your choice SDK (Software Development Kit). Then it should ask you to specify where your java SDK or Java SE is installed, specify where the program java.exe is, so find that in your C:\Program Files\JDK\jdk\ directory. Then you're ready to go!


[edit] Related wikiHows




[edit] Sources and Citations





Article provided by wikiHow, a wiki how-to manual. Please edit this article and find author credits at the original wikiHow article on How to Write Your First Program in Java. All content on wikiHow can be shared under a Creative Commons license.

3 comments: