The Java Programming Environment compiling to bytecode Classe
The Java Programming Environment: compiling to bytecode Classe 1. java The programmer write source code with a text editor. Classe 2. java One source file for each public class The source code is submitted to a compiler. The compiler translates the source code into Java bytecode. Classe 1. class Classe 2. class One class file for each class Bytecode is a low level language similar to machine language. 1
The Java Programming Environment: executing the code Classe 1. class Classe 2. class A Java Virtual Machine (JVM) interprets (executes) the bytecode. A JVM is a program which simulates a machine A Java enabled browser contains a JVM. A Java “program” that can run inside a browser is called an applet. It is also possible to make stand-alone applications. The class loader, which is part of the JVM, will load classes as they are needed. Before the bytecode is interpreted it will be inspected by a bytecode verifier. The bytecode verifier will make sure that the bytecode is legal and that it don’t break Java’s security restrictions. 2
The Java Programming Environment: compiling to native code There are some native code compilers (compilers that generates code for a particular machine platform) available for Java, but just-in-time (JIT) compilers are more common. A just-in-time (JIT) compiler will generate native code the first time a class is used, the code is cached for later use. Some hardware support for bytecode is also available. 3
JDK - Java Development Kit • The Java Development Kit contains the software and tools that you need to compile, debug, and run applets and applications that you've written using the Java programming language. • JDK is available free from SUN Microsystems, the creators of the Java programming language (http: //java. sun. com/products/jdk/). 4
Some of the basic command-line tools in the Java Development Kit (JDK) • javac -Is the Java compiler. Java source code (file with a. java extension) is compiled into Java bytecodes (class file, extension. class). • java - Is the Java Interpreter, it executes Java class files. • jdb - Is a command-line debugger for Java classes. • appletviewer - Is the Java applet viewer, it allows you to run applets without a web browser. • javah - Is used to integrate C and Java. • javap - Disassembles class files. • javadoc - Generates documentation from a Java source file. 5
javap the Java Class Disassembler • It is possible to do some reverse engineering from Java bytecode classes. • With javap you can get a human-readable version of the specified Java bytecode classes. Usage: javap <options> <classes> where options include: -c Print bytecode for each method (disassemble the code). -private Shows all classes, variables, and methods. 6
javap In the given example the file Menu. Applet. class is disassembled C: Javamyclasses>javap -private Menu. Applet Compiled from Menu. Applet. java public synchronized class Menu. Applet extends java. applet. Applet implements java. awt. event. Action. Listener /* ACC_SUPER bit set */ { public Menu. Applet(); public void init(); public void action. Performed(java. awt. event. Action. Event); private void show. Page(java. lang. String); } 7
javadoc - Generates API documentation in HTML format from Java source code. • The documentation describes the class, its inheritance hierarchy, and each nonprivate variable and method in the class. • Comments in the source code that begin with /** and end with */ is also included. Each separate line of the comment must start with an *. • Special tags placed in the source code are recognized. These doc tags enable you to autogenerate a complete, wellformatted API from your source code. The tags start with an "at" sign (@). 8
javadoc - Example from a source file /* * Menu. Applet. java * Copyright (c) 1998 Hi. A, All Rights Reserved. */ import java. applet. *; /** * This applet will display two buttons, each leading to a given URL. * @author Jan Pettersen Nytun * @version 1. 1, 98/08/13 */ public class Menu. Applet extends Applet implements Action. Listener { /** * Initialize the applet. * @return <code>void</code> */ public void init() {. . . . 9
- Slides: 9