Download Using Java without BlueJ

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Using Java without BlueJ
5.0
BlueJ projects
• A BlueJ project is stored in a
directory on disk
• A BlueJ package is stored in several
different files
• Some files store the source code,
some store the compiled code,
some store additional information
• BlueJ uses standard Java format for
some files and adds some additional
files with extra information
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
2
The BlueJ directory structure
project: calculator
Calculator
UserInterface
CalcEngine
c:\bluej\calculator\
Calculator.java
Calculator.class
Calculator.ctxt
CalcEngine.java
CalcEngine.class
CalcEngine.ctxt
package.bluej
UserInterface.java
UserInterface.class
UserInterface.ctxt
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
3
The BlueJ file structure
• package.bluej – the package file.
Contains information about classes in the
package. One per package (project).
• *.java - standard Java source file (text).
One per class.
• *.class - standard Java code file. One
per class.
• *.ctxt - BlueJ context file. Contains
extra information for a class. One per class.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
4
Standard Java files
• source files: *.java
Java source files contain the source code
in readable form, as typed in by the
programmer.
• class files: *.class
Java class files contain byte code (a
machine readable version of the class).
They are generated by the compiler from
the source file.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5
The edit-compile-execute
Cycle
source file
class file
011010
110101
010001
011010
110101
1001
10
1
0111
0110110
editor
compiler
(javac)
virtual machine
(java)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
6
Editing
• A file can be edited in any text editor
– Notepad, emacs, jEdit, PFE, vi, …
• Don't use Microsoft Word: by default,
Word does not save in text format
– Includes formatting (i.e. fonts, shapes, …)
• Make sure to save with a .java file
extension before compiling!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
7
Command line invocation
• Compilation and execution of Java in
JDK are done from a command line
– Windows: DOS shell
– Unix: Unix shell
• Must make sure that commands for
compiler (javac) and runtime (java)
are in the command path
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
8
• JDK compiler:Compiling
javac
• To invoke: javac ClassName.java
compiles source file and all classes that it depends on
• To find commands in path … either:
Change directory to location of commands
– cd C:\Program Files\Java\jdk1.7.0_65\bin
– javac ClassName.java
Ensure commands are in your command PATH
– Control Panel -> System -> Advanced ->
Environment Variables -> PATH
– Add “C:\Program Files\Java\jdk1.7.0_65\bin”
– javac ClassName.java
• Creates byte file: ClassName.class
9
Compiler
error messages
C:\bluej\project> javac ClassName.java
ClassName.java:22: ';' expected.
private Parser parser
^
1 error
C:\bluej\project>
The programmer has to open the file in the editor,
find the line number, fix the error and recompile.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
10
Execution
C:\bluej\project> java ClassName
• java starts the Java virtual machine
• The named class is loaded and
execution is started
• Other classes are loaded as needed
• Only possible if class has been compiled
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
11
Problem
Execute what?
• If we try:
C:\bluej\project> java ClassName
• We get a compiler error:
Exception in thread "main"
java.lang.NoSuchMethodError: main
• The problem: there is NO instance object
How does the system know which method to
execute?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
12
The main method
• The java system will always execute a
method called main with a certain
signature:
public static void main(String[] args)
{
...
}
• If compiling and executing from the
command line, then the main method
must exist!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
13
The main method
• main must exist
• main must be public
• main must be static (class
method)
• main must have a String[]
parameter
• Only main can be invoked
Example of a class method
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
14
Main method
example
public static void main(String[] args)
{
Game game = new Game();
game.play();
}
• Consider placing in a separate class,
containing just this
• The main method should
– create an object
– call the first method
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
15
Demo
TestingMain.java
16
Command Line Execution
javac Driver.java
– compiles code in the Driver class
– produces Driver.class
java Driver
– executes the byte file Driver.class
– invokes main method (if it exists)
java Driver Hello world “that I live in”
– passes any String arguments into main
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
17
Related documents