Life Without NetBeans Part A Writing, Compiling, and Running Java Programs Almost every computer and device has a Java Runtime Environment (JRE) installed by default. This is the software that creates a virtual machine capable of running Java programs. To create our own Java programs, however, we also need the Java Development Kit (JDK). If you are doing these exercises on your own personal computer, you might need to install the JDK. The two commands that we need in these exercises are javac and java. The javac command is the Java compiler - the software that compiles Java source code into runnable form (bytecode). The java command runs a Java program, but it only understands bytecode. So, it s a two-step process: compile and then run. Setup The following exercises use a command-line interface. In Windows, you can start a command-line shell by click Command Prompt in Accessories, or you can run the command cmd. We also need to make sure that the javac and java programs are available. To check this, open a command prompt and enter javac. If you see a long message instructing you about all of the options available for using the compiler, then you re good to go. You can proceed to Example 1. If you see an error message that says javac is not a recognized command (or something like that), then the computer can t find the compiler, so we need to tell it where it is. Determine where your computer s JDK is installed and note the complete path the bin folder inside the JDK. Edit your system s PATH environment variable to include the path to the JDK s bin folder. Verify that you can now see the usage message when you enter javac. Life Without NetBeans - Part A Page 1 of 12
Example 1 - A Simple Program Objective : write, compile, and run a simple program The source code of a Java program is saved in one or more.java files. These are just plain text files, so they can be edited from any text editor. To run a Java program, the source code (.java files) must first be compiled into bytecode (.class files). This is done with the Java Compiler, called javac. The bytecode is then run with the Java Interpreter, called java. Exercise 1. Open a text editor, such as Notepad, type in the program below, and save the file as Hello.java. The main part of the file name must match the class name in the source code ( Hello ), and the file must have a.java extension. import java.util.calendar; import java.util.date; public class Hello { public static void main(string[] args) { System.out.println("Hello World!"); Date d = Calendar.getInstance().getTime(); System.out.println("The time is " + d); } } 2. Open a command window and navigate to the folder where the.java file is saved. 3. Display the contents of the folder to make sure that the file Hello.java is there. (In Windows, use the dir command; in Linux, the ls command.) 4. Display the contents of the file. (In Windows, enter the command type Hello.java ; in Linux, enter the command cat Hello.java.) 5. Now compile the program with javac. javac Hello.java Life Without NetBeans - Part A Page 2 of 12
If your source code contains any syntax errors, you will see error messages displayed to the console. If you don t see any error messages, then the compilation succeeded. 6. Display the contents of the directory and observe that a.class file has now been created. 7. Now run the program with java. java Hello Note that it s just java Hello, not java Hello.class - the java interpreter assumes (and requires) that the program has a.class extension. You should see two lines of output in the console: Hello World! followed by The time is with the current system date and time. 8. Once compiled, the bytecode is independent of the source code, so it doesn t need to be in the same location. Verify this by copying the class file to another folder, navigating to that folder, and running the program. Lessons You don't need NetBeans to write, compile, and run a Java program. Once a program is compiled into bytecode, the.class files can be moved anywhere. They are now independent of the.java files. Example 2 - How NetBeans Works Objective : learn how a NetBeans project compiles and runs a program NetBeans automatically configures and runs your application. But there is nothing special about this process - it is the same as we saw in Example 1. Exercise 1. Create a project in NetBeans called HelloApp. Inside the project, create a class called Hello, and do NOT specify a package. (In the source code, there should be no package statement at the top of the file.) 2. Run the program to make sure it works. 3. Close NetBeans. Life Without NetBeans - Part A Page 3 of 12
4. Open a command window, move the project s classes folder and run the program with the java command. 5. Navigate to the src folder, compile the.java file with the javac command, and then run the program with the java command. Lessons There is nothing special about the way NetBeans compiles and runs your program. It just does the same thing that you can do with the raw data, using javac and then java. NetBeans moves the class files to a separate folder, but this is purely to keep things organized. It is not a requirement (although it is a very good idea). Example 3 - Packages Objective : write, compile, and run a program that uses a package Packages are just folders used to organize your source code. If you use packages (and you should), you need to replicate the folder structure in the compiled bytecode. Exercise 1. Copy the file FizzBuzz.java from the FizzBuzzApp project to an empty folder. (You will find the file in the project s src\fbmain folder.) 2. Open a command prompt and navigate to your folder. 3. Display the contents of the.java file. Note the package name declared on the first line. 4. Compile the file and verify that the.class file has been created. 5. Enter java FizzBuzz - it doesn t work. 6. Enter java fbmain\fizzbuzz (or use / if on Linux or Mac) - it doesn t work. 7. Enter java fbmain.fizzbuzz - it doesn t work. 8. Create a folder called fbmain and copy or move the.java file inside. 9. Navigate to the fbmain folder. 10. Compile the.java file and verify that the.class file has been created. 11. Enter java FizzBuzz - it doesn t work. 12. Enter java fbmain\fizzbuzz (or use / if on Linux or Mac) - it doesn t work. Life Without NetBeans - Part A Page 4 of 12
13. Enter java fbmain.fizzbuzz - it doesn t work. 14. Navigate to the parent folder. 15. Enter java FizzBuzz - it doesn t work. 16. Enter java fbmain\fizzbuzz - it doesn t work. 17. Enter java fbmain.fizzbuzz - it works! Lessons The.class file must be inside a folder structure that matches the package structure in the source code. The program must be run from the parent folder. The run command must specify the folder name(s) followed by the program name, separated by the dot character (. ). Example 4 - Multiple Source Files (Same Package) Objective : compile and run a program containing multiple files (in the same package) Most applications contain more than one class. When the source code is compiled, there will be (at least) one.class file for each.java file. In order for the interpreter to run the application, you must tell it which class contains a main method. The.class files do not have to be in the same location as the.java files, but both sets of files must be in a folder whose name matches the package declared in the.java files. Exercise 1. Copy the src folder from the project FlightApp_SamePackage to an empty folder. 2. Navigate to the folder and then inside the src folder. 3. Display the contents of the demo folder - there should be three.java files. 4. Compile all of the files with the command: javac demo\*.java 5. Display the contents of the demo folder again - there should now be three.class files as well. Life Without NetBeans - Part A Page 5 of 12
6. It s a good idea to keep source files and compiled files separate (which NetBeans does). Let s do that now. 7. Go back to the root level of your folder. 8. Create a new folder called classes and inside that folder create a folder called demo. The folder structure should now look like this: 9. Copy all of the.class from src\demo to classes\demo folder. (Remember: the source code specifies a package called demo, so the source files and bytecode files must both be in demo folders - but they can be in different demo folders.) 10. To run a program containing multiple classes, you must select the class that contains a main method. In this case, it is the FlightDemo class. Navigate inside the classes folder and run the program with the command: java demo.flightdemo You should see five flights displayed to the console. Fortunately, the javac command allows you to specify a separate folder to hold the classes, and it will replicate the sub-folder package structure for you. Let s do that now. 11. Delete the classes from classes\demo and then remove the classes\demo folder (but not the classes folder itself). 12. Navigate to the src folder and compile the program with the command: javac d..\classes demo\*.java Now look in the classes folder. You will see that the demo folder has been created automatically, and the.class files have been compiled into that folder. 13. Run the program from the classes folder as you did before. Lessons Life Without NetBeans - Part A Page 6 of 12
Java can compile multiple source files at once. To run the program, you must know which class contains the main method. The source code (.java files) and the bytecode (.class files) both have to be placed in a folder that matches the declared package structure, but they can each be in separate folders. This is recommended, since it makes it easier to keep track of things, and makes bundling easier, as we will see later. The Java compiler (javac) can automatically create the necessary folders for the.class files and then move the files to that folder. Use the -d flag. Example 5 - Multiple Packages Objective : compile and run a program containing multiple files (in different packages) What if your source code files are in different packages? This is no problem - javac will figure it out, provided your folder structure matches the packages declared in the source code. The compiler will automatically seek out any dependencies. For example, if class Foo contains an object of type Goo, then when you compile Foo.java, the compiler will automatically compile Goo.java as well - if, that is, the folders match the declared packages. Exercise 1. Copy the src folder from the project FlightApp_DifferentPackages to an empty folder. 2. Create a folder called classes inside your folder (but outside the src folder). 3. Navigate inside the src folder. 4. Display the contents of the src folder - you should see a demo folder and a entity folder. Display the contents of each subfolder - you should see that FlightDemo.java is inside the demo folder, and Airport.java and Flight.java are inside the entity folder. 5. Compile the program with the command: javac d..\classes demo\*.java Life Without NetBeans - Part A Page 7 of 12
6. Now look in the classes folder. You will see that both the demo folder and the entity folder have been created automatically, and the.class files have been compiled into their correct matching folders. 7. Furthermore, the Java interpreter (java) will automatically figure out which folders to look in, based on the declared folder structure at compilation time. So, you should be able to run the program from the classes folder exactly as you did before. The interpreter will automatically look inside the entity folder to find the other classes. Try running it with the command: java demo.flightdemo Lessons The Java compiler (javac) can replicate complex folder structures and place the compiled.class files in the correct folders. The Java interpreter (java) can automatically find all the classes needed to run the program, provided that the folders the.class files are in matches the package names declared in the source code. Example 6 - Third-Party Libraries Objective : include a third-party library in your application Many of the classes you use are built into the Java environment - classes like String, Scanner, or the Calendar class used in the Hello program from Exercise 1. But sometimes you want to use a library of classes that another programmer has written. (If you have ever used FileUtils, for example, then you have used a third-party library.) These classes are usually bundled together and distributed in a single.jar file. (A.jar file is very similar to a.zip file - it is a set of files that have been compressed. We will learn more about.jar files in Part B.) As you probably know, to use the classes while you re writing your program, you must import those classes in your code, just as you import built-in Java classes, such as Scanner. In addition, however, you must also include the.jar file in the compilation process. This is because those classes are not part of the Java language itself, so you have to tell the compiler to include them. Finally, when you run the program, you must tell the interpreter where the classes are. This is because they are not in the same package as your classes. Life Without NetBeans - Part A Page 8 of 12
Exercise 1. Copy the src folder from the project EmailApp to an empty folder. 2. Create a folder called classes inside your folder (but outside the src folder). 3. Copy the file emailtools.jar from the EmailApp project folder to your folder. 4. Navigate inside the src folder. You should see a folder called demo and inside that folder you should see a file called EmailDemo.java. This is the file that needs to be compiled. 5. Inside the src folder, execute the command: javac d..\classes demo\*.java 6. This command worked in Exercise 5, but now there is another library that the compiler needs. This is why you see an error message informing you that the compiler can t find the package emailtools. So, we have to tell the compiler where to find this library. Recall that we put the file emailtools.jar in the root level of the folder (outside src). So, enter the command: javac d..\classes cp..\emailtools.jar demo\*.java The -cp flag is short for classpath - it is used to tell the compiler where to look any other classes that are needed. 7. Confirm that there is a.class file in the classes\demo folder. 8. Now let s run the program. Navigate inside the classes folder and execute the command: java demo.emaildemo You will see an error message because now the interpreter can t find the external library. 9. So, copy the file emailtools.jar into the classes folder, and execute the command: java cp emailtools.jar demo.emaildemo You will see another error message. The problem now is that when we specify the classpath as emailtools.jar, we are telling the interpreter to look ONLY in that jar file and not in the current folder. So, it s not looking in the current folder and therefore doesn t find the demo folder. 10. To fix this, we have to tell the interpreter to look in both places: Life Without NetBeans - Part A Page 9 of 12
java cp emailtools.jar;. demo.emaildemo The command works, and you should see the EmailDemo app running. The -cp string can specifiy multiple locations separated by semicolons. The dot character means the current folder. So, emailtools.jar;. means look in emailstools.jar and in the current folder. Lessons When using an external library, you must tell the compiler (javac) where the library is. When using an external library, you must tell the interpreter (java) where the library is, but you must also make sure you tell the interpreter to look for the folder that contains the regular classes. Example 7 - Non-Java Files Objective : include non-java files in your application Often your application uses non-java files, such as configuration files, data files, images, etc. These files are not compiled (since they are not Java), but when you run the program, you need to tell the interpreter where to find the files. Exercise 1. Create a new empty folder and copy all of the following from the project HangmanApp into your folder: The src folder - contains.java files. The images folder - contains.png image files. The file countries.txt. The file cputils.jar 2. Create a folder called classes inside your folder (but outside the src folder). 3. Take a moment to examine the contents of the folders. 4. Navigate inside the src folder and compile the program with the command: javac d..\classes cp..\cputils.jar hangman\*.java Life Without NetBeans - Part A Page 10 of 12
5. Verify that the.class files have been created inside classes\hangman. 6. Navigate to the classes folder. As in the previous exercise, we will also need to copy the third-party library to this location. 7. Copy the file cputils.jar from the root level of your folder to classes. 8. Execute the command: java cp cputils.jar;. hangman.mainform You will see a popup error message informing you that the application can t find the file countries.txt. This is a file that the application needs, but it s not a Java file, so it wasn t compiled. 9. Copy the file countries.txt from the root level of your folder to classes. 10. Try running the application again. This time you ll see a popup error message informing you that the application can t find the file Hangman-0.png. The application also needs all of the image files in the images folder, so copy that folder from the root level of your folder to classes. 11. The application should now run successfully. Life Without NetBeans - Part A Page 11 of 12
Comprehensive Exercise Perform the following tasks using only a terminal shell. Do not use NetBeans (or any other IDE). 1. Copy everything from the ImageBuilder folder to an empty folder. 2. Quickly read the source code in CreateImageFromBinary.java to get a sense of how the code works. In particular, note the locations of where it is reading its input and where it is saving its output. 3. Create a folder at the root level of your folder called classes. 4. Compile the application into the classes folder. 5. Inside classes create/copy whatever you need so that the application will run. 6. Run the application from the classes folder. 7. Verify that two images - big.jpeg and small.jpeg have been created. 8. Verify that the images are valid by opening them in an image viewer. Life Without NetBeans - Part A Page 12 of 12