eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Web Programming
Building an Application  
  • Okay, so now let's look at the rest of the Test class because the Test class is pretty special. The Test class is actually a very small Java Application that can be compiled and executed!

  • Here is the code again...
    public class Test
      {
      public static void main(String[] args)
        {
        Announcer a = new Announcer();
        a.printAnnouncement();
        }
      }
    

  • So you should already be comfortable with the definition of the class from the last section. You should also be familiar with the contents of the main() method since we just discussed it. But what is the main() method and why is there no constructor for Test?

  • Well, the main() method is a very special method because it works without an instantiation of the class. It does this because the main() method is static. static is a keyword which, when applied to classes means that no instantiation is necessary in order to run it.

  • Notice also that the main method may take an array of strings as arguments. This is used so that we can pass information to our application from the command line if we want. However, in this case we won't be passing any arguments.

Compiling an Application

  • It is very easy to compile your application once you have installed and configured your JDK. You simply go to the directory in which you have saved your .java file and use javac to compile it into bytecode using the command:

    javac filename.java
    (or sometimes javac *.java to compile all java files in one directory)

Running an Application

  • Once your code is compiled, you can execute the application using the java virtual machine. To do so, you type the following in the same directory:

    java filename

  • Try it out by compiling and running the Test application. Don't forget that you have to make two .java files. One will be Annoucner.java and the other will be Test.java. To make your job quicker, you can click here to copy the two java files.

It Didn't Work!

  • Okay, so since these are computers we are dealing with, it is possible that you had some trouble compiling and executing your test case. Here are a few possible solutions.

ErrorSolution
"Bad command or filename"Did you spell everything right? If so, you [probably have not set your PATH environment variable correctly and the computer cannot find the javac or java virtual machine executable. Either set your path correctly, or referencethe executable exactly with something like:

C:\Java\Sun\jdk1.1.4\Bin\javac Test.java

"ClassDefNotFound Error"You have not defined your CLASSPATH correctly and the java compiler cannot find the java class library it needs in order
"Public class Test must be defined in a file called 'Test.java'"You have to be careful about case-sensitivity when programming in Java. You probablytyped "javac test.java." Try typing the javac command again with a capital "T"
"Can't find class Test/class"You were probablytrying to execute your compiled test class and typedjava Test.class. You should not use the .class extension. Try typing

java Test
"Can't find class test"Case sensitivity again!You probably typed "java test" and it should be "java Test"
"Test.java:7: ';' expected".You forgot to end that line with a semi-colon
"Return required at end of java.lang.String getAnnouncement()"You forgot to say

return _announcement;

at the end of this method. Remember that if you say you are going to return something,you have to do it.

"Announcer.java:22: Undefined variable: _announcement"In this case, _announcement is spelled wrong.However, it could also mean that you have defined the_announcement variable in a method that is not accessible.Remember that if you want variables to be available to other methodsin the same class, you must declare them outside of the method.

Previous Page |Next Page