# Java Notes

# TextPad Editor + JDK

## 1. Download and install the Java JDK if necessary

Install the J2SE JDK. See Java Development Kit (JDK).

## 2. Download and install TextPad

- Download a free, trial version of TextPad from www.textpad.com . You will be reminded from time to time that you might pay for it, but don't worry, it won't expire.
- Install TextPad by double clicking on the downloaded file and following the prompts. Do this only after you have installed the Java JDK.

## 3. Entering, compiling, and executing your Java program in TextPad

1. Type or paste your Java program source code into the large text pane.
2. Create new directory. Putting the programs directly on your desktop is not a good idea. Create a directory (folder) for all of your course work, and a separate directory within that for each assignment. The tiny extra work to create separate directories prevents unwanted interactions between old and new programs. It can save endless hours of frustration. This is not just an academic neatness obsession - it comes from seeing students who couldn't complete their assignments because of old-file pollution.
3. Save the source in a file with EXACTLY the same name as the "class" with an extension of ".java". Assume the source code defines this class.

           public class InitialTest {
              . . .
           }

   It must be saved in a file called InitialTest.java . Put it in the directory you created previously.

4. Compile (translate from source code to machine code) by selecting the Tools > Compile Java menu item. If there are errors (and there usually will be at first), go back and forth between the "Command Results" and the source code by clicking on each of them in the top left pane. Continue with this process until it compiles correctly.
5. Run the program by choosing Tools > Run Java Application menu item. You will be using one of these to styles of interaction (Input-Output) at the beginning.
   - Console Output (System.out.println(...)). This will open a DOS Command Window and you will see your text output, followed by "Press any key to continue . . .". Your program is done!
   - Dialogbox Output (JOptionPane.showMessageDialog). Program output will appear in dialog boxes. The only weird thing is that TextPad creates an unnecessary DOS window -- that isn't something that the Java system does.

## Optional, but useful, TextPad preferences

There no critical preferences that need changing, but they can be set by selecting the `Configure > Preferences` menu item. Some things you might wish to set:

Tabs
  To conform to the Java indentation standards, you should set your tabs as follows. Go to
  `Configure menu > Preferences... > Document Classes > Java > Tabulation`
  Set the "Default tab spacing" and "Indent size" fields to 4.
  Check both the "Convert new tabs to spaces" and "Convert existing tabs to spaces when saving new files".
  Click OK.
Folders
  Set the `Startup` directory to where your files are normally located.
Document Classes > Java > Tabulation
  Set the default tab spacing to 4, indent size to 4, and check both the Convert new tabs to spaces and Convert existing tabs to spaces when saving files. Tabs should generally not be used in programming because the size of tabs is not universal so someone else looking at your program will often see a mess
Assertions
  To turn runtime assertion checking on in TextPad go to `Configure > Preferences... > Tools > Run Java Application`. Set the value in the Parameters text field to `-ea $BaseName` . "-ea" is the abbreviation for Enable Assertions. If you don't know what assertions are yet, don't bother with this.
Line numbers
  This isn't in the Preferences area, altho it should be. TextPad will show line numbers if you select View menu then choose Line Numbers. This is useful in coordinating error messages with your source code.

## TextPad Problems?

1. Q: TextPad can't find the Java compiler. What's wrong?

   A: When TextPad can't find the Java compiler, there are two common causes.

   1. You installed TextPad before the Java compiler. You MUST install Java first. Uninstall TextPad (from the System control panel) and reinstall it.
   2. You didn't install the Java JDK, maybe you only have the Java JRE. If you installed Java 5 is should install by default in

          C:\Program Files\Java\jdk1.5.0_04

      There will also be a "JRE" (Java Runtime Environment), which is appropriate, but be sure there is a "jdk" (Java Development Kit).

2. Q: I get a message about "javac ...". How can I fix it?

   A: Check the `Configure > Preferences > Tools > Compile Java > Parameters` field. You only need $File in it. Reinstalling TextPad might clear it up. When I reinstall it I get the following.

        -Xlint:unchecked  $File

   You don't need the -Xlint option (which notes situations where you could take advantage of Java 5 generics), but you do need the `$File`. Put blanks in front and after the `$File` value.

3. Q: This message comes out when I try to run my program in TextPad. What's wrong?

         Exception in thread "main" java.lang.NoClassDefFoundError:

   A: The most common cause at this point is that the file name doesn't match the class name. Make sure the names agree exactly first.

   Another, much less likely, problem could be that the parameters are set wrong in TextPad. Look at
   `Configure menu > Preferences... > open Tools > Run Java Application`.
   The Parameter field should have `$BaseName` (with blanks around it). You can skip the "-ea", which stands for "enable assertions". The Initial Folder should have `$FileDir` in it.

4. Q: Can I run my Java program like a normal application?

   A: No. TextPad translates the .java files into .class files which can not be run by double-clicking them. Some IDE's (eg, NetBeans) will produce double-clickable .jar files, and there are programs which will package your Java files into a .exe file.