Pratik Bahl Running Java Programs Introduction to Java Simple Java Program ● As a text file, you can print it, display it on the monitor, copy it to another text file, or alter it with a text editor. ● A file name is case sensitive ● A source program is a text file that contains a program (such as above) written in a programming language. Since it contains ordinary text (stored as bytes) it cannot be directly executed (run) by the computer system. Bytecodes ● To run a Java program the source file is first translated into a file of bytecodes. ● A Java bytecode is a machine instruction for a Java processor. A file of bytecodes is a machine language program for a Java processor. ● a Java processor is a silicon processor chip that directly executes a machine language called Java bytecodes. This is like an Intel processor that directly executes Intel machine language. ● An entirely new file Hello.class containing bytecodes is created by the compiler. (If there already is a file named Hello.class, it will be replaced by a new one. Usually, this is what you want.) ● In this picture, the source program Hello.java is examined by a program called javac running on your computer. The javac program is a compiler (a translator) that translates the source program into a bytecode file called Hello.class. ○ Important Idea: The bytecode file will contain exactly the same bytecodes no matter what computer the javac compiler runs on. ● The architecture of the processor that executes Java bytecodes is well-documented and is available to anyone. Java Virtual Machine ● Since people normally use PCs and Macintoshes, they do not have hardware java processor chips. However, the Java processor can be implemented as a program that reads the bytecodes and performs the operations they specify. ● Some interpreters run source code written in a high level language like Basic ● Some interpreters run bytecodes (like the Java interpreter) ● One of the interpreters is called an emulator because it emulates hardware, but it's software. Eg// A Java bytecode interpreter can be created for any computer system. ● Once you have a Java compiler and a Java interpreter you can run any Java program no matter what type of computer you have. ● The actual processor of the computer does not run java code. The java interpreter program, which is part of the computer software, runs the code. The actual processor is the intel or mac processor chip. ● Important Idea: When the Java interpreter is running on a computer system, that system acts just like a hardware Java bytecode processor. It is a Java Virtual Machine. (thats why the java interpreter is like an emulator). Pratik Bahl ● After a java interpreter is specifically written for a particular computer system, that computer system can now become a Java virtual machine. It is almost like the computer has a hardware Java processor chip. ● When a Java program is translated into bytecodes, the bytecodes are the same no matter what computer system is used. ● A Java source program (such as Hello.java) can be written and compiled on one computer (say a Windows computer) to produce bytecode (say Hello.class). Now that bytecode can run on any computer that has a Java interpreter. Portability ● Apple developed a new Java interpreter for their new system. That interpreter can run any java bytecode program. ● Java programs are portable, meaning that the same bytecode program can run on any computer system that has a java interpreter. ● A source program can be compiled into bytecodes on any computer that has a Java compiler.No matter what computer you have, you can write the same Java programs. ● Many other languages, such as JavaScript, Python and PHP are portable like Java. Your web browser contains a JavaScript interpreter and can run JavaScript source code embedded in a web page, regardless of where the page is hosted. JavaScript is not Java. Its name is intended to draw attention to its Java-like portability. Running on Windows ● In order to access a file that is in a folder on the c drive, you need to find the folder on file explorer and right click to open the address on command prompt. ● Another way to do that would be through the DIR command. The DIR command lists the files in the DIRectory. ● Every character of a text file encoded as one byte or end-of-line characters. Compile the File ● The command Javac Hello.java runs the Java compiler on the source file. Make sure that the upper and lower case characters match. ● For MAC dir is actually ls and to find a certain folder you have to use cd. Running the Program ● The command java Hello starts up the Java Virtual Machine with the bytecode file Hello.class. ● Aggravations: don't use the extension class with the file name. Also, the command is java without a "c" at the end. ● You don't have to use javac on mac because it is one OS. ○ Pratik@Pratiks-MacBook-Pro ~ % java /Users/Pratik/Documents/ICS4U/Hello.java Contents of the Bytecode File ● The command TYPE is used in command prompt to type a text file to the monitor ● The TYPE command accepts bytes that encode characters. It does this by sending those bytes to the graphics board which puts each character on the monitor. ● The TYPE command does not work with a bytecode file. ● Usually a file that contains bytes that do not correspond to characters is called a binary file. It is named this even though all files are composed of bytes, and all bytes contain binary patterns. Pratik Bahl Questions: 1. Can the processor of a computer system directly execute source programs written in Java? a. No 2. What are the two ways that a source program (written in any programming language) could be run on a computer system? a. Translation (into machine instructions, which are then directly executed by the processor) b. Interpretation (by an interpreter program running on the processor. 3. Could a processor chip be built that executes Java bytecodes directly, just as an Intel processor chip executes its machine language directly? a. Yes. Hardware Java processor chips have been created out of silicon and can execute bytecode files directly. 4. Say that Apple has just come out with a new computer and wants this computer to run Java programs. What must Apple do? a. Apple must write a Java interpreter for their new system. 5. Can bytecodes be sent from computer to computer over the Internet? a. Yes. One of the advantages of portability is that Java bytcodes can be copied unaltered to many types of computers and those computers can run the program. 6. Will you see exactly the same things as above on your computer system? a. No. Probably not. b. On recent Windows, the differences should be small. 7. What is the mistake a. The source file contained a syntax error (in this case a missing ") and could not be compiled. b. Notice that the compiler says there are "3 errors" even though there is really only one. This is common. Sometimes when there is a syntax error the compiler gets confused and prints messages that are misleading. c. Find the first error, use the text editor to correct the program, save it (to the same filename) and compile again. 8. Could Hello.class be sent to a printer? a. No. Pratik Bahl b. The bytes in the file Hello.class are machine language for the Java Virtual Machine. These bytes of machine language do not usually correspond to printable characters (and when they do, it is only by accident.) 9. What happens if you try to run the Java Virtual Computer on the source code?