Java-without-BlueJ

Document Sample
Java-without-BlueJ Powered By Docstoc
					                                                                                             BlueJ projects
                                                                          • A BlueJ project is stored in a directory on disk.


 Using Java without BlueJ                                                 • 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.


                                                                    1.0




The BlueJ directory structure                                                       The BlueJ file structure
                                        H:\...\projects\calculator\
          project: calculator                                                  • bluej.pkg - the package file. Contains information about
                                              bluej.pkg                          classes in the package. One per package.
 Calculator                                   bluej.pkh
                                              Calculator.java                  • bluej.pkh - backup of the package file.
                                              Calculator.class
                                              Calculator.ctxt                  • *.java - standard Java source file (text). One per class.
                        UserInterface
                                              UserInterface.java
                                              UserInterface.class              • *.class - standard Java code file. One per clasS
                                              UserInterface.ctxt
                  CalcEngine                  CalcEngine.java                  • *.ctxt - BlueJ context file. Contains extra information for
                                              CalcEngine.class                   a class. One per class.
                                              CalcEngine.ctxt
              Standard Java files                           The edit-compile-execute cycle
• source files: *.java                                                                                 011010
  Java source files contain the source code in readable          source file          class file       110101
                                                                                                        1001
  form, as typed in by the programmer.                                                    011010         10
                                                                                          110101          1
                                                                                          010001          1
                                                                                                          1
    class files: *.class
                                                                                                        0111
    Java class files contain byte code (a machine                                                      0110110
    readable version of the class). They are generated                         compiler
    by the compiler from the source file.                     editor                               virtual machine
                                                                                (javac)                 (java)




                           Editing                              Command line invocation
                                                          • compilation and execution of Java in JDK are
• A file can be edited in any text editor                   done from a command line
  – Notepad, Xemacs, ...
                                                          • On Microsoft systems: DOS shell
• careful with using Word: by default, Word
  does not save in text format                            • On Unix: Unix shell
• Make sure to save before compiling!                     • Must make sure that the commands for
                                                            compiler and runtime are in the command
                                                            path.
• Programming environments (BlueJ, JDEE,
  JBuilder, Eclipse, …) just make whole process
  easier
                   Compiling                                        Error messages
 • Name of the JDK compiler: javac
                                               H:\..\ExtraProjects\uglab> javac UGLab.java
 • To invoke:
   javac <source name>                         UGLab.java:14: ';' expected.
 • compiles <source name> and all classes it      private DrinksMachine machine
                                                  ^
   depends on                                  1 error
 • Example:                                    H:\...\projects\uglab>
   cd H:\...\ExtraProjects\uglab
   javac UGLab.java
                                                       The programmer has to open the file in the editor, find the
                                                       line number, fix the error and recompile.




                   Execution                                Problem: Execute what?
• H:\...\ExtraProjects\uglab> java UGLab          • If we try:

                                                     H:\...\ExtraProjects\uglab> java UGLab
• “java” starts the Java virtual machine.
• The named class is loaded and execution is      • Exception in thread "main"
  started.                                          java.lang.NoSuchMethodError: main
• Other classes are loaded as needed.
• Only possible if class has been compiled.       • The problem: how does the system know which
                                                    method to execute?
                 The main method                                                The main method (2)
                                                                   •   “main” must exist
  • The answer: The java system always executes a
    method called main with a certain signature:                   •   “main” must be public
                                                                   •   “main” must be static (class method)
       public static void main(String[] args)
       {                                                           •   “main” must have a String array parameter
           ...
       }                                                           •   Only “main” can be invoked
  • For this to work, such a method must exist!




           Main method - example                                   • Often it is simpler than this - the main method will
                                                                       – create an object
                                                                       – call the first method
 public static void main(String[] args)
 {
   UGLab lab = new UGLab();                                        • E.g.
   Student student1 = new Student(“Rudi”, 3);
   Student student2 = new Student(“Sue”, 7);                         public static void main(String[] args)
   lab.add(student1);                                                {
   lab.add(student2);
   lab.run(200);                                                          Game game = new Game();
 }                                                                        game.play();
                                                                     }
Does what you currently do by clicking etc on workbench in BlueJ
  What is the String[] args parameter
                                                         Using a Command Line Argument
                  for?
• For command line arguments                            public static void main(String[] args)
• These are arguments (parameters) you can type after   {
  the java <class> command                                int nTicks = Integer.parseInt(args[0]);
• For example you might want to be able to run the        UGLab lab = new UGLab();
  program as follows:                                     Student student1 = new Student(“Rudi”, 3)
                                                          Student student2 = new Student(“Sue”, 7);
    java UGLab 200                                        lab.add(student1);
 meaning run it for 200 timeticks                         lab.add(student2);
                                                          lab.run(nTicks);
                                                        }