Monday, September 22, 2008

Compiling and Running Java Source Files Using EditPlus Tools

There is a way to compile and run your Java source code files without using the command prompt! You can set up some menu options in EditPlus that allow you to quickly compile and run your Java programs. You might want to print these instructions so you can follow these steps. Later we will add a tool or two to this group, so make sure you keep or bookmark these instructions.
Setting up the Tools

Open up EditPlus. It doesn't matter if you have a new/existing document open or not.

To follow these instructions, you need to know the drive and path where you installed your Java SDK. If you followed the default settings, this is probably c:\jdk1.5.0 or c:\Program Files\jdk1.5.0. If you have a different version of the Java SDK, then the folder name might vary slightly. It doesn't matter what it's called, as long as it contains the "bin" folder with all the SDK utilities. In the instructions, this folder will be referred to as the Java SDK folder.

1.Select "Configure User Tools" from the "Tools" menu.
2. You'll see "Groups and Tool Items" in the middle of the dialog box. Tool groups allow you to organize the different tools you want to create and maintain in EditPlus. We are going to create a tool group called "Java Tools".



a.You should see a dialog box like the image shown here. The default group name will probably be "Group 1". To change it, click on the Group Name button.
b. In the New Name text box, type Java Tools
c. Click OK.
d. You should now see "Java Tools" in the pull-down box under the caption "Groups and Tool Items"

3.Now you need to add some tools to your tool group. We'll start with a tool used to compile Java Source code files. If you don't have the Configure User Tools dialog box open, follow step 1 above.
a.Click the Add Tool>>> button and select Program from the pop-up menu.
b.In the Menu Text field, type "Compile Java Source". This is the text you will see in the menu for this tool.
c.Select the program that will run when you select the "Compile Java Source" menu item: Click the small button to the right of the "Command" field.
d.Browse to your Java SDK folder and go into the "bin" subfolder. Find and select the file called JAVAC.EXE. Make sure you select the right file - there are a lot of files in this folder and they have similar names. When you're sure you have the right file, click Open. You should see the path and file information in the "Command" field. Of course you could type this in by hand instead of using the Browse button if you wanted to.
e.When you run the Java compiler, you always give it the name of the source file as an argument. We can set this up for this tool: Click the button to the right of the "Argument" field and select File Name. You should see $(FileName) appear in the field.
f.If you are using Java SDK 1.5 or higher, type -Xlint, followed by a space, in front of $(FileName).
g.Click the button beside the "Initial Directory" field and select File Directory. You should see $(FileDir) appear in the field. This ensures that your compiler runs as if it were in the same folder as your Java source file.
h.Lastly, check off the box for Capture Output. This will show the output to your compile in a small window at the bottom of the screen. If you prefer that the output i.Click the Apply button to save your tool changes



4.The next tool we need to add is for running our Java class files. Again, if you aren't in the configure user tools dialog box, follow step 1 above.

a.Click the Add Tool>>> button and select Program from the pop-up menu.

b.In the Menu Text field, type Run Java Class. This is the text you will see in the menu for this tool.

c.Select the program that will run when you select the "Run Java Class" menu item: Click the small button to the right of the "Command" field.

d.Browse to your Java SDK's /bin folder and select the file called JAVA.EXE. Double check to make sure you have the right file selected, then click Open. You should see the path and file information in the "Command" field.

e.When you use the JAVA command to run a Java class file, you always specify the name of the class file without the extension. We can set this up for this tool: Click the button to the right of the "Argument" field and select File Name without Extension. You should see $(FileNameNoExt) appear in the field.

f.Click the button beside the "Initial Directory" field and select File Directory. You should see $(FileDir) appear in the field. This ensures that the java command interpreter runs as if it were in the same folder as your Java class file.

g.Lastly, check off the box for Capture Output. This will show the output of your program (if any) in the same output window where you see the output to the compile process. If you would rather see the output in a command-prompt window, leave this option unchecked.

h.Click the Apply button to save your tool changes.

To run a program using command-line arguments, set up a new tool menu just like the one in step 3, but make sure you check off the box labeled "Prompt for Arguments". This will cause a dialog box to appear where you can type your command-line arguments (in the exact same format you would type them at the Dos prompt).

You could also check off this box in the tool menu item you created in step 3, and when you run a program that does not require arguments, click the "Skip" button in the arguments dialog.


Dialog box with Run with Arguments settings


Using the Tools

To compile, go to the EditPlus window with the source file you want to compile and select "Compile Java Source" from the Tools menu. If you prefer using the keyboard, you can also compile the current program by pressing the Ctrl-1 keys.

To run a class file, go to the EditPlus window with the source file you want to run (rather, the source file that is associated with the class file you want to run), and select "Run Java Class" from the Tools menu. If you prefer using the keyboard, you can also run the class file by pressing the Ctrl-2 keys.

Note that the keyboard shortcuts may vary if you have already created user tools. You can see what shortcut keys are associated with your tools by looking at your tool menu items in the Tools menu.

No comments: