New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.
|
|||||||||||||
Menu Articles Using Java Applets Looking for Java resources? Check out the Java Coffee Break directory! |
Getting started with Java"A beginner's guide to Java programming"
The Java programming language has, within only a few years, become a de facto industry standard for developing portable applications, and interactive dynamic content for web pages. It's extremely easy to pick up Java programming skills, and in this lesson, you'll learn how to write, compile, and run Java applications. First, though, you'll need to download the Java Development Kit (JDK). You may prefer at a later time to use a third party IDE, such as Visual J++ or Borland JBuilder, but it is important to become familiar with the basics of Java first.
Installing the JDKSun offers many versions of its JDK, for different Java versions and platforms. You are advised to use the most recent JDK, to gain access to all of the functionality of the latest version of the Java platform. If you don't already have the JDK installed, then stop off at Sun's official Java site, http://java.sun.com/. There you'll be able to download the JDK for Windows & Solaris. The installation process is fairly straightforward, but you should consult the documentation if you encounter problems. You'll need to make sure that the JDK is installed correctly, and that the JDK tools are within your operating system's path, before proceeding. Setting up environmental variables for JDKUnder Windows, you set environmental variables by modifying the autoexec.bat file, located in the root directory of your hard-drive. This batch file is executed on startup; modifications to this file will not take effect until you run it, or reboot.
Modifying autoexec.batYou'll need to edit the autoexec.bat file with a simple text editor.
Open it up, and add a path statement pointing to the /bin/ directory
under your JDK installation directory.
For example, if I'd installed my JDK to c:\jdk1.3\, I'd want to point the path statement to c:\jdk1.3\bin\. However, you must also be careful not to overwrite existing path statements set for other software packages. To prevent this, use the following statement (modifying directory for your installation location) set path=%path%;c:\jdk1.3\bin\ Next, you need to set the classpath to include the current directory. The classpath helps JDK locate Java software (class files) that are installed on your machine. Some versions of JDK don't require this, but some of the tools that ship with JDK do require it, so it's best to set this environmental variable up properly. set classpath=%classpath%;.\
That done, reboot and you'll be ready to start writing your first Java application. Writing your first Java applicationLike any other language, Java programming starts with writing source code. Source code are instructions that will be followed by the computer. We can create source code using any standard text editor, such as 'notepad' on Windows, or 'vi' on Unix. All Java source code ends in a '.java' extension. Our first application will be extremely simple - the obligatory "Hello World". If you've never heard of "Hello World" before, its a common programming exercise when learning a new language. This example will display the words "Hello World" when executed. class myfirstjavaprog { public static void main(String args[]) { System.out.println("Hello World"); } } Code : myfirstjavaprog.java You should type this code out, using your preferred text editor, and save it as 'myfirstjavaprog.java'. Place this file in a directory (for example, "c:\src\") of its own, not your Java installation directory. Its best to keep your tools and source code separate.
Compiling your first Java applicationWhile some languages are interpreted (such as PERL), others introduce an extra step, called compilation. Compilation takes source code (files ending in a .java extension), and converts it into byte-code. This byte-code is low level machine code, which is executed inside a Java Virtual Machine (JVM). To compile Java code, we need to use the 'javac' tool. This ships as part of the JDK, so if you have it installed and in the current path, you can compile your application using the following command from any DOS prompt or Unix session : javac myfirstjavaprog.java This will produce a Java class file, called myfirstjavaprog.class. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code. Running your first Java applicationNow the time has come, (said the Walrus). You're ready to run your first Java application. From a dos prompt or Unix session, enter the directory where your source code and class file is stored, and type the following : java myfirstjavaprog
Your Java application will load, display its message, and then terminate. Congratulations! SummaryWriting and developing Java applications and applets need not be a great mystery. Armed with the JDK and a text editor, virtually any programmer can write in Java. You've just taken the first step, in a long journey, towards learning Java. |
Copyright 1998, 1999, 2000 David Reilly
|
Privacy | Legal | Linking | Advertise! |
Last updated:
Monday, June 05, 2006
|