Introduction to Java : Building Your First Application part-1

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…

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 that, 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:

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

The process is demonstrated below:

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:

Try it out by compiling and running the Test application. Don’t forget that you have to make two .java files. One will be and the other will be

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.

“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 reference the executable exactly with something like:C:\Java\Sun\jdk1.1.4\Bin\javac
“ClassDefNotFound Error”You have not defined your CLASSPATH correctly and the java compiler cannot find the java class library it needs.
“Public class Test must be defined in a file called ‘'”You have to be careful about case-sensitivity when programming in Java. You probably typed “javac” Try typing the javac command again with a capital “T”
“Can’t find class Test/class”You were probably trying to execute your compiled test class and typed java Test.class. You should not use the .class extension. Try typing
“Can’t find class test”Case sensitivity again! You probably typed “java test” and it should be “java Test”
“ ‘;’ expected”.You forgot to end that line with a semi-colon
“Return required at end of java.lang.String getAnnouncement()”You forgot to say

at the end of this method. Remember that if you say you are going to return something, you have to do it.
“ 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 methods in the same class, you must declare them outside of the method.