Programming without BlueJ Week 12
CONCEPTS COVERED THIS WEEK Programming without BlueJ CONCEPTS COVERED THIS WEEK Running Java without BlueJ The main Method
The BlueJ directory structure Programming with BlueJ The BlueJ directory structure project: clock-display package.bluej ClockDisplay.java ClockDisplay.class ClockDisplay.ctxt NumberDisplay.java NumberDisplay.class NumberDisplay.ctxt ClockDisplay NumberDisplay look at package in BlueJ then look at directory compare files in directory to classes in package
The BlueJ file structure 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 look at package in BlueJ then look at directory compare files in directory to classes in package
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. look at package in BlueJ then look at directory compare files in directory to classes in package
Standard Java Edit-Compile-Run Cycle Programming without BlueJ Standard Java Edit-Compile-Run Cycle 011010 110101 1001 10 source file class file 011010 110101 010001 1 1 1 0111 0110110 look at package in BlueJ then look at directory compare files in directory to classes in package compiler (javac) editor virtual machine (java)
Editing Java Source Files 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! look at package in BlueJ then look at directory compare files in directory to classes in package
Command Line Invocation 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 look at package in BlueJ then look at directory compare files in directory to classes in package
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 look at package in BlueJ then look at directory compare files in directory to classes in package
Java Compilation (Error Messages) 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> look at package in BlueJ then look at directory compare files in directory to classes in package The programmer has to open the Java file in the editor, find the line number, fix the error and recompile.
Java Program Execution 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. look at package in BlueJ then look at directory compare files in directory to classes in package
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? look at package in BlueJ then look at directory compare files in directory to classes in package
Java Program Execution – The Main Method 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. look at package in BlueJ then look at directory compare files in directory to classes in package
Java Program Execution – The Main Method 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! look at package in BlueJ then look at directory compare files in directory to classes in package
Java Program Execution – The Main Method 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(); } look at package in BlueJ then look at directory compare files in directory to classes in package 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/