Download presentation
Presentation is loading. Please wait.
1
Programming without BlueJ Week 12
2
CONCEPTS COVERED THIS WEEK
Programming without BlueJ CONCEPTS COVERED THIS WEEK Running Java without BlueJ The main Method
3
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
4
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
5
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
6
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 look at package in BlueJ then look at directory compare files in directory to classes in package compiler (javac) editor virtual machine (java)
7
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
8
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
9
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
10
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.
11
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
12
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
13
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
14
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
15
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
16
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.