Java Tools: CS 310 Lecture notes 
  
  
     
  
Outline 
  
  
  
 Java environments.
    
  
  Java Tools.
    
    
 IDEs 
    
  Analysis and testing.
    
  Non-Sun Java.
    
  Scripting
      
  
   
    
  
 
The Java Environment 
  
Java Components 
  
Java Versions 
  
  
 Use the -version option to find the installed version.
$ java -version
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode)
$ javac -version
javac 1.6.0_03
$  
  
  Relevant versions are:
1.1  - Obsolete; stubborn web developers. 
    
Java Environments 
  
  
 Java has four main environments:
    
    
 The server (J2EE); Enterprise edition.
    
  The desktop (J2SE); Standard edition.
    
  The browser; Plug-in part of J2SE.
    
  Portables (J2ME); Micro edition.
      
  
  This course uses the standard edition (J2SE 5 or 6).
    
Documentation 
  
  
 The Java system has extensive documentation.
  
  From Sun via the web 
  or  
by download .
    
  
  You can also buy documentation (a.k.a. books).
    
    
 They go out of date distressingly fast. 
      
  
  Become adept with some form of documentation.
    
Documentation Types 
  
  
 The two main documentation sets are for
  the class library APIs  and
  the JDK .
    
    
 Other documentation includes sample programs, tutorials, and white
    papers.  
      
  
  The javadoc tool helps document class libraries in the Java style.
    
    
 Any class library you use or create should be documented. 
      
  
  
IDEs 
  
  
 An integrated development environment (IDE) is a hopped-up editor.
    
  
  
Java IDEs 
  
DrJava 
  
  
 DrJava is a simple IDE for Java.
    
    
 BlueJ < DrJava < Eclipse. 
      
  
  Available in the class directory
/export/home/class/cs-310/drjava
 
  
  Run it with
$ java -jar /export/home/class/cs-310/drjava/drjava-*.jar 
  
  
Example. 
  
Example.. 
  
Example... 
  
Example.... 
  
Analysis and Testing 
  
  
 Java analyzes and tests well.
    
  
  
FindBugs 
  
  
 FindBugs is a static analyzer for errors and infelicities in Java code.
    
    
 “Static” means the analyzed code isn’t executed. 
      
  
  Your assignments will be run through FindBugs.
  
  Available in the class directory
/export/home/class/cs-310/findbugs-1.2.1
 
  
  
Example. 
  
Example.. 
  
Example... 
  
Example.... 
  
Example..... 
  
Example...... 
  
FindBugs on the Cheap 
  
  
 The Java compiler’s -Xlint command-line option performs checks
  similar to FindBug’s checks.
    
    
 Similar to but not as detailed or as thorough. 
    
  On the other hand, what’s it cost you?
      
  
  
Oh, One Last Thing 
$ ls
AnonymousInnerClassTest.java  ForEachTest.java      StaticInnerClassTest.java
CloneTest.java                InnerClassTest2.java  TimerTest.java
EmployeeSortTest.java         InnerClassTest.java
$ findbugs -textui *.class
Exception in thread "main" java.io.IOException: IOException while scanning codebases
    at findbugs.FindBugs2.execute(FindBugs2.java:187)
    at findbugs.FindBugs.runMain(FindBugs.java:1521)
    at findbugs.FindBugs2.main(FindBugs2.java:731)
Caused by: findbugs.classfile.ResourceNotFoundException: Resource not found: 
    /home/rclayton/new-public-html/s08-310/code/java/chapter-6/*.class
    at findbugs.classfile.impl.SingleFileCodeBase.getClassDescriptor(SingleFileCodeBase.java:222)
    at findbugs.classfile.impl.SingleFileCodeBaseEntry.getClassDescriptor(SingleFileCodeBaseEntry.java:61)
    at findbugs.classfile.impl.ClassPathBuilder.build(ClassPathBuilder.java:229)
    at findbugs.FindBugs2.buildClassPath(FindBugs2.java:432)
    at findbugs.FindBugs2.execute(FindBugs2.java:160)
    ... 2 more
$ for i in *.java ; do javac $i ; done
$ findbugs -textui *.class
M B Eq: Employee defines compareTo(Employee) and uses Object.equals()  At EmployeeSortTest.java:[line 9]
Warnings generated: 1
$  
    
But Rather 
$ ls
AnonymousInnerClassTest.java  InnerClassTest2.java  	  
CloneTest.java                InnerClassTest.java   	  
EmployeeSortTest.java         StaticInnerClassTest.java 
ForEachTest.java      	      TimerTest.java            
$ findbugs -textui *.class
Can't find *.class.
$ for i in *.java ; do javac $i ; done
$ findbugs -textui *.class
M B Eq: Employee defines compareTo(Employee) and uses Object.equals()
At EmployeeSortTest.java:[line 9]
Warnings generated: 1
$  
JUnit 
  
  
 JUnit  is a unit-testing framework for Java.
  
  Unit tests are tests on small portions of a system.
    
    
 In Java the unit’s usually a class.
      
  
  Available in the class directory
/export/home/class/cs-310/junit4.1
 
  
  When possible, assignments will have JUnit tests.
    
Example. 
  
Example.. 
  
Example... 
  
Example.... 
  
Example..... 
  
Testing Tips 
  
  
 Do it.
  
  Write lots of small, simple tests and run them all the time.
  
  Replicate every error as a test.
    
    
 Learn thyself. 
    
  Make sure your fixes stay fixed.
      
  
  Develop code in measured steps and keep the tests up-to-date (and
  passing).
    
Non-Sun Java 
  
  
 Sun encourages independent implementations of Java parts.
    
    
 Specs and reference implementations are freely available. 
    
  Names are still protected and licensed.
      
  
  Alternative implementations may have more appropriate properties.
    
    
 It won’t make much difference for this course. 
      
  
  
Non-Sun JVMs 
  
  
 There’s a fair number of JVM implementations.
    
    
 Particularly true with non-PC-like hardware. 
      
  
  Motivated by research, curiosity, or licensing.
  
  Examples
    
    
 Jikes, originally from
    IBM ,
    now open source . 
    
  Kaffe , a clean-room implementation of the
    JVM and class libraries. 
      
  
  
Non-Sun Libraries 
  
  
 Gnu Classpath  is an almost
  complete reimplementation of the class libraries.
    
    
 Other reimplementations include GNU Classpath. 
      
  
  Third parties provide libraries to extend the standard class libraries.
    
    
 The SWT from the Eclipse project. 
      
  
  
Non-Sun Java Compilers 
  
  
 The main non-Sun compiler is the GNU
  compiler for Java  (GCJ).
    
    
 Compiles Java to bytecode or native code. 
    
  Fast compile and execution.
    
  Perhaps familiar debugging with gdb.
      
  
  Many bytecode-to-native compilers.
  
  Many compilation- and execution-oriented tools.
    
Scripting Languages 
  
  
 A scripting language 
    
 This is different from a language that can be interpreted (or, maybe,
    it’s not).
      
  
  The absence of a edit-compile-link-execute cycle is the mark of an
  interpreted language.
    
    
 Instead it’s edit-execute. 
      
  
  Relaxed syntax and pragmatic semantics is another. 
    
JVM-Based Languages 
  
  
 Hey! The JVM’s a (bytecode) interpreter.
  
  Translate a language to bytecode and run it (interpret it) on the JVM.
    
    
 JVM bytecode is high-level for straightforward (maybe) translation.
    
  The JVM environment is portable and feature-full for free.
    
  Free and immediate access to available class libraries.
      
  
  
JVM-Based Languages 
  
  
 There are many JVM-based scripting languages; some familiar
    
    
 JPython, Jacl (TCL/TK), JScheme, JRuby, …
      
    and some less so
    
    
 Groovy, BeanShell, Pnuts, …
      
  
  Choice is a matter of features and taste.
    
BeanShell 
  
  
 The BeanShell is an example of a scripting language.
    
  
  
On-Demand Variables 
  
  
 Variables can be introduced as needed.
    
    
 The scripting environment keeps track of them as used. 
      
  
  Variables are also dynamically typed; no declarations needed.
    
    
 Type declarations can be used to improve script robustness (and
    perhaps efficiency). 
      
  
  
Variables Example 
  
Dynamic Environment 
  
  
 A scripting environment can be updated dynamically and iteratively.
    
    
 This is the basis of a scripting language’s exploratory nature.
      
  
  
BeanShell Example 
  
  
 Create and add an action listener to the button.
    
  
  
Extended Syntax  
  
  
 The scripting language doesn’t have to be faithful to Java language
  syntax. 
    
    
 Just as long as there’s a consistent interpretation to the variant
    syntax. 
      
  
  A scripting language can implement variant syntax to simplify Java
  features. 
    
    
 Really friendly scripting languages let you define your own syntax. 
      
  
  
Extended-Syntax Example 
$ java -classpath bsh-2.0b4.jar bsh.Interpreter
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % map = new java.util.Hashtable();
bsh % print(map.get("one"));
null
bsh % map.put("one", 1);
bsh % print(map.get("one"));
1
bsh % print(map{"two"});
null
bsh % map{"two"} = 2;
bsh % print(map{"two"});
2
bsh % button = new JButton();
bsh % print(button.getLabel());
bsh % button.setLabel("Click me!");
bsh % print(button{"label"});
Click me!
bsh % button.label = "Press to play";
bsh % print(button.label);
Press to play
bsh %  
Interactive Run-Time 
  
  
 Interactive environments can demand less up-front information.
    
    
 This another way scripting languages can be more terse than Java.
      
  
  And can also spend the time to recover (or discover) information.
    
    
 Scripting languages are mostly dynamic, while Java is mostly static.
      
  
  
Run-Time Discovery Example 
$ java -classpath bsh-2.0b4.jar bsh.Interpreter
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % 
bsh % add(a, b) { return a + b; }
bsh % 
bsh % print(add(1, 2));
3
bsh % print(add("hello ", "world!"));
hello world!
bsh % 
bsh % print(add("hello ", 1));
hello 1
bsh %  
Static-Typing Example 
bsh % 
bsh % int add(int a, int b) { return a + b; }
bsh % 
bsh % bsh % print(add(1, 2));
3
bsh % print(add("1", "2"));
// Error: EvalError: Command not found: 
   add( java.lang.String, java.lang.String ) : 
   at Line: 3 : in file: <unknown file> : 
   add ( "1" , "2" ) 
bsh %  
The Environment as this 
  
  
 The definitions given the environment can be used to implement
  interfaces or extend classes.
  
  The environment is treated as a giant class instance.
    
    
 For some anonymous class. 
      
  
  The environment’s available as this.
    
This-Environment Example 
$ java -classpath bsh-2.0b4.jar bsh.Interpreter
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % button = new JButton("Click me!");
bsh % frame = new JFrame();
bsh % frame.getContentPane().add(button);
bsh % frame.pack();
bsh % frame.setVisible(true);
bsh %
bsh % actionPerformed(e) { print("Thanks!"); }
bsh %
bsh % button.addActionListener(this);
bsh % Thanks!
Thanks!
Thanks!
bsh %
 
BeanShell Example. 
dragText() {
  f = new Frame("Drag in the box");
  f.setFont(new Font("Serif", Font.BOLD, 24));
  f.setSize(300, 300);
  f.show();
  gc = f.getGraphics();
  gc.setColor(Color.cyan);
  mouseDragged(e) {
    gc.drawString("Drag Me!", e.getX(), e.getY());
    }
  mouseMoved(e) { }
  f.addMouseMotionListener(this);
  } 
BeanShell Example.. 
dragText() {
  f = new Frame("Drag in the box");
  f.setFont(new Font("Serif", Font.BOLD, 24));
  f.setSize(300, 300);
  f.show();
  gc = f.getGraphics();
  gc.setColor(Color.cyan);
  mouseDragged(e) {
    gc.drawString("Drag Me!", e.getX(), e.getY());
    }
  mouseMoved(e) { }
  f.addMouseMotionListener(this);
  } 
Java Debuggers 
  
  
 Assertions and testing should reduce the need for debuggers.
    
    
 They find problems found early and exactly. 
      
  
  Debuggers are usually integrated with an IDE.
    
    
 Either stand-alone or IDE-custom debuggers. 
      
  
  
Java Debugging 
  
  
 The JVM defines the
  Java
  Platform Debugger Architecture  (JPDA).
    
    
 A set of related top-, middle-, and botom-level protocols.
      
  
  JDB is an simple text-based debugger.
    
    
 It mostly illustrates how the JPDA is used. 
      
  
  
Summary 
  
  
 Use the Java 5 or 6 Standard Environment (J2SE 5 or 6).
  
  Java is a relatively easy language to write tools for.
    
    
 Avail yourself of them. 
    
  Maybe write a tool yourself.
      
  
  Learn how to use FindBugs and JUnit (and then use them).
    
References 
  
Credits