Transcript Document

Java tools
Java programming ID1006
20 Sep 2010
javac – Java compiler
• Java source code files: *.java
• Java byte code files: *.class
• Java resource archives: *.jar
• javac compiles .java files into .class files
• Can be run interactively or from build tools
javac – The Java compiler
.java
Source code text
javac
Compilation
.class
java
(JVM)
Byte code
Execution
javac – The Java compiler
• javac [options][sourcefiles][classes][@argfiles]
• i.e.: javac Parser.java
• -classpath
• or environment variable CLASSPATH
Classpath
/
asg1/
Token.class
WordToken.class
PunctToken.class
TextMeter.class
Parser.class
asg2/
asg3/
LixTest.class
LixMeter.class
SET CLASSPATH=.;..\asg1;..\asg2;..\asg3
FleschTest.class
FleschMeter.class
SyllableCounter.class
javadoc
• Compiles documentation from Java source code
.java
Source code text
javadoc
.html
.css
...
Web
browser
Compilation
HTML pages
Reading
javadoc
• Comments /** ... */ before the documented
item
/**
* Computes the square of an integer.
* @param i The integer to square
* @return The squared value of its argument
*/
public int square(int i) {
return i*i;
}
javadoc
• @param name desc – documents arguments
• @return desc – documents return value
• @throws exception desc – documents the
conditions under which an exception is thrown
javadoc
• http://download.oracle.com/javase/6/docs/t
echnotes/guides/javadoc/index.html
• http://download.oracle.com/javase/6/docs/t
echnotes/tools/windows/javadoc.html#java
doctags
apt
• annotation processing tool
• Allows the adding of metadata to code
• Annotations do not alter the meaning of
the code, but...
• ...libraries and other tools may alter their
behaviour in response to annotations
apt
/**
* Describes the Request-For-Enhancement(RFE) that led
* to the presence of the annotated API element.
*/
public @interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date() default "[unimplemented]";
}
This is an annotation type
apt
@-sign
/**
* Describes the Request-For-Enhancement(RFE) that led
* to the presence of the annotated API element.
*/
public @interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date() default "[unimplemented]";
}
apt
@-sign
/**
* Describes the Request-For-Enhancement(RFE) that led
* to the presence of the annotated API element.
*/
public @interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date() default "[unimplemented]";
}
Default values
apt
@RequestForEnhancement(
id
= 2868724,
synopsis = "Enable time-travel",
engineer = "Mr. Peabody",
date
= "4/1/3007"
)
public static void travelThroughTime(Date destination) { ... }
apt
@RequestForEnhancement(
The annotation
id
= 2868724,
preceeds the
synopsis = "Enable time-travel",
annotated
engineer = "Mr. Peabody",
method
date
= "4/1/3007"
)
public static void travelThroughTime(Date destination) { ... }
apt
@RequestForEnhancement(
The annotation
id
= 2868724,
preceeds the
synopsis = "Enable time-travel",
annotated
engineer = "Mr. Peabody",
method
date
= "4/1/3007"
)
public static void travelThroughTime(Date destination) { ... }
Ordinary Java method
apt
• Annotations can be used to control
– Automated software testing
– Documentation tools
– Automated software configuration
– and more...
apt
• http://download.oracle.com/javase/6/docs/techno
tes/guides/language/annotations.html
jar
• Java archive
• Stores class files, images, sounds etc in a
single file
• Supports compression
• Used to package libraries and applications
• Fetch-and-run over the Internet
jar
• Create an archive: jar cf jarfile inputfile ...
• jar cf Foo.jar *.class *.jpg
Create
Next argument
is the file name
of the archive
Files to be copied
into the archive
jar
• Create an archive: jar cf jarfile inputfile ...
• jar cf Foo.jar *.class *.jpg
• Unpack an archive: jar xf jarfile
• jar xf Foo.jar
jar
• Create an archive: jar cf jarfile inputfile ...
• jar cf Foo.jar *.class *.jpg
• Unpack an archive: jar xf jarfile
• jar xf Foo.jar
• Run from an archive*: java –jar jarfile
• java –jar Foo.jar
* Requires jar manifest meta-data: main class and classpath
jar
• http://download.oracle.com/javase/6/docs/t
echnotes/guides/jar/index.html
• http://download.oracle.com/javase/6/docs/t
echnotes/tools/windows/jar.html
javap
•
•
•
•
•
•
Java disassembler
Shows class fields and methods
javap [options] classname
-public|-protected|-private|-package
-s show internal signatures
-c show byte code
javap
• http://download.oracle.com/javase/6/docs/t
echnotes/tools/windows/javap.html
RMI
• Remote Method Invocation
• One JVM calls methods on a remote JVM
• Client and service
Remote Method Invocation
Jvm A
Jvm B