Programming without BlueJ

advertisement
Programming without BlueJ
Week 12
Programming without BlueJ
CONCEPTS COVERED THIS WEEK

Running Java without BlueJ

The main Method
Programming with BlueJ
The BlueJ directory structure
project: clock-display
ClockDisplay
NumberDisplay
package.bluej
ClockDisplay.java
ClockDisplay.class
ClockDisplay.ctxt
NumberDisplay.java
NumberDisplay.class
NumberDisplay.ctxt
Programming with BlueJ
The BlueJ file structure
package.bluej
Stores BlueJ project details
ClassName.ctxt
BlueJ class context file
ClassName.java
Java source code file
ClassName.class
Java bytecode file
Programming without BlueJ
Standard Java Files
ClassName.java
Java source files contain the source
code in readable form, as typed in
by the programmer.
ClassName.class
Java class files contain bytecode (a
machine readable version of the
class). They are generated by the
compiler from the source file.
Programming without BlueJ
Standard Java Edit-Compile-Run Cycle
source file
class file
011010
110101
010001
011010
110101
1001
10
1
0111
0110110
editor
compiler
(javac)
virtual machine
(java)
Programming without BlueJ
Editing Java Source Files
 A Java
file can be edited using any text
editor e.g. Notepad
 Careful
with using Word as, by default,
Word does not save in text format
 Be
sure to save changes before
compiling!
Programming without BlueJ
Command Line Invocation

Compilation and execution of Java in JDK are done from a command line.

Windows: DOS shell, i.e. Command Prompt:

Ensure that the directory for the Java compiler (javac) and Java runtime
(java) is set in the command path e.g.
set path=C:\Program Files (x86)\Java\jdk1.6.0_33\bin
Programming without BlueJ
Java Compilation

The name of the JDK compiler is javac

To compile a Java class definition:
javac <source name>

javac compiles the source file as well as all other classes
the source file depends upon.

Example usage of javac:
X:\>cd My Documents\Java\Picture
X:\My Documents\Java\Picture>javac Picture.java
Programming without BlueJ
Java Compilation (Error Messages)
Example usage of javac:
X:\>cd My Documents\Java\Picture
X:\My Documents\Java\Picture>javac Picture.java
Picture.java:14: ';' expected.
private square wall
^
1 error
X:\My Documents\Java\Picture>
The programmer has to open the Java file in the editor,
find the line number, fix the error and recompile.
Programming without BlueJ
Java Program Execution
Example usage of java:
X:\My Documents\Java\Picture>java Picture

“java” starts the Java virtual machine (JVM).

The named class is loaded and execution is
started.

Other classes are loaded as needed.

Only possible if class has been compiled.
Programming without BlueJ
Java Program Execution – Problem: Execute What?
Example usage of java:
X:\My Documents\Java\Picture>java Picture
Exception in thread "main"
java.lang.NoSuchMethodError: main
How does the system know which of the
methods of the class to execute?
Programming without BlueJ
Java Program Execution – The Main Method
Example usage of javac:
X:\My Documents\Java\Picture>java Picture
Exception in thread "main"
java.lang.NoSuchMethodError: main
The solution: The Java system always
executes a class method called main with a
specific signature.
Programming without BlueJ
Java Program Execution – The Main Method
Fixed signature for the main method:
public static void main(String[] args)
{ ...
}

For this to work, a main method must exist in
one (and only one) of the classes being called!
It is possible to place all of an application’s
code in a class with just a main method!
 But this is absolutely NOT RECOMMENDED!

Programming without BlueJ
Java Program Execution – The Main Method
Example of a main method in a class called Game:
public static void main(String[] args)
{
Game game = new Game();
game.play();
}

In general the main method should ONLY
– create an object
– call the first method
Creating a Java program
• Writing the Java code
– Any editor (such as Notepad) can be used
to write a Java program. However, most
professional programmers use a software
application called an Integrated
Development Environment or IDE
–JCreator
– http://www.jcreator.com/
Download