Java and Javac


 

In the first lecture, we discussed how compilers are used to convert programs written in High-Level Languages to binary object programs that the processor in a given computer can actually understand and execute.

The problem with compiled code is that every processor has a different binary language that it recognizes. This makes it difficult to distribute programs for multiple machines. In traditional software distribution channels this hasn't been too much of a problem, since a company could just compile their program for each system they want to distribute a version for.

But the goal of having Java applets embedded within web pages presented new difficulties. Since a page might be read on many different machines, how could compiled Java programs be embedded in pages. One alternative would be to just embed the source code, rather than the executable binary. But this has two problems. First, the web browser would need to have the Java compiler built into it, and their would be a delay in displaying a page while the embedded program was compiled. Second, most commercial developers like to distribute their programs in compiled form because it makes it less feasible for a competitor to figure out how the program works. If source code was embedded in the web page, all their secrets would be out in the open.

The solution that was developed for Java is that the java compiler reads the Java source code you give it and produces a file of Java bytecode. Bytecode is just executable binary instructions for a fictitious but realistic processor. It is this bytecode file that is embedded in a web page. Java-compatible web browsers include a simulator for this non-existent processor. Because the commands in the bytecode are similar to those in most real processors, the bytecode simulator is a relatively simple program to build into a browser.

So, when you run javac, the Java compiler, it doesn't produce a true object file. Instead it creates a bytecode file. If the original program is an applet, then this file can be embedded in a web page. If it is an application, the bytecode can be run locally using the program java, which is just a standalone bytecode simulator.


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Tue, Sep 8, 1998 at 10:19:57 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_02/lecture/javaAndJavac.html