
- Forum
- Programming Talk
- Java
- Getting Started With Java Programming !!
Getting Started With Java Programming !!
This is a discussion on Getting Started With Java Programming !! within the Java forums, part of the Programming Talk category; This tutorial will introduce you to Java programming. You'll compile and run your very own Java application, using Sun's Java ...
-
Getting Started With Java Programming !!
This tutorial will introduce you to Java programming. You'll compile and run your very own Java application, using Sun's Java Development Kit.
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.
Downloading & Installing the JDK
Before you can develop Java applications, 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.
You should always use third party tools in combination with Sun's JDK, not exclusion, to ensure compatibility.
Installing the JDK
Sun 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/.
For an even quicker way to find the latest Java tools, visit the JDK download page, located at http://java.sun.com/jdk/index.html. The most current version at time of writing is the Java 2 SDK v1.3, which contains all the tools you will need for this tutorial.
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.
In most cases, this involves setting environmental variables. As many first-time Java developers do not have experience in this, we'll show you how to set these, for the most popular Java development environment -- Windows. Macintosh, Unix, and other environments, have their own way of setting environmental variables (for more information consult your operating system documentation).
-
Setting up environmental variables for JDK
Under 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.batCode:Remember to reboot! I can't stress this enough. Many readers make the changes, forget to reboot, and wonder why JDK doesn't work properly.
You'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)Code:Use a simple text editor, like notepad or the DOS command 'edit'.
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%;.\
shell=c:\command.com /p /e:32000Code:Sometimes, you may run out of environmental space, and see the message "Too many parameters" on boot up. If this happens, add the following line to config.sys (located in the root directory)
That done, reboot and you'll be ready to start writing your first Java application.
-
Writing your first Java application
Like 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.
Java compilers expect the filename to match the class name. Thus the class, myfirstjavaprog, must be saved as myfirstjavaprog.java. If you're using notepad, you'll need to rename if from myfirstjavaprog.java.txt back to myfirstjavaprog.java
-
Compiling your first Java application
While 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.
-
[b]Running your first Java application[b]
Now 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!You should not type java myfirstjavaprog.class. All Java classes end in this extension, it is not necessary, and will cause an error messages.
Summary
Writing 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.
-
04-23-2006, 10:57 PM #6kalareddy Guest
Thanks for posting nice tutorials... This really helping me with Java.
-
Your Welcome kalareddy
..
Will Add more tutorials soon
-
That's great stuff!

Making the "Hello World" program in a new language always make me feel so proud
thanks!
-
Sponsored Ads

Reply With Quote





