Java Development To-Go: Compiling with a Text-Editor


Intro

At school, we are generally encouraged to develop our applications using JGrasp. Some more resourceful individuals may have noticed that the school systems also offer Eclipse or Visual Studio if you are so inclined to use them. I have requested Java Studio (which is essentially a NetBeans clone) to be installed in the labs.

So you might be wondering: Why would I want to give up my nice IDE and settle for a text-editor? If not...imagine doing ALL of your coding in Window's Notepad utility.

IDE's are very useful and have a lot of features that most text-editors (even the really really nice ones) will probably just never have. IDE's like Visual Studio, NetBeans, Borland, etc. are made specifically FOR programmers by very capable teams that often have excellent resources. Most text-ditors are just, well, simple in comparison. But this isn't always a downside.

The point I'm about to make is this: IDE's are useful and packed with features on the project scale and, as projects become larger and larger, text-editors can't really compete. At the same time, all those features of an IDE can slow it down a bit. So for smaller projects or short coding exercises text-editors can make a big difference. The problem is that (until you learn otherwise) you generally have to switch back to the IDE to actually compile the code.

As I show in this page, you can do all of your compiling outside of an IDE. Some IDEs do have the compiler built in, but most of them just make calls to one. Many times the compiler isn't even made by the same people who created the IDE. Some of the more advanced text-editors let you use macros or other commands to do the same thing.

This began when a friend showed me how the editor TextPad does this out-of-the-box. I was a little dissappointed that my favorite text-editor Notepad++, did not. However, N++ did give me the ability to create macro/command ("Run") to do it myself.




Using Batch Files

The basic idea is to use the text-editor's command/macro to call a batch script file (.bat or .cmd) which will in turn call the compiler. For some of the simpler commands it may be possible to do make the entire call from the text-editor's Macro/Command, but for the more complicated actions it becomes easier to call a batch script.

For the examples below, all the scripts will be stored in:

C:\Program Files\Notepad++\plugins

The scripts I will demonstrate are as follows:

compileCurrent.cmd
compileDir.cmd
runApp.cmd
viewApplet.cmd

Also, the batch scripts are written to take commandline arguments that will specify certain information such as the file or directory path. Whatever text-editor you use should be able to provide some type of variable in it's macro statements that will represent this information. For Notepad++, the command feature is called a "Run" command, and a list of variables can be found here.




Creating Commands

Next you'll have to actually create the commands in your editor of choice.

For Notepad++, this will be under the "Run" menu (as opposed to the "Macros" menu which is used to record as sequence of actions/keystrokes).

Alternatively, you can enter the data directly into the file "shortcuts.xml" in NPP's Program folder (make sure npp isn't running). If you choose to enter commands this way be sure to place them under the heading "<UserDefinedCommands>".

An example is displayed below. Note that "&quote;" is used in place of a real quote (") because the file is in XML. List of other escapable characters can be found here.



Example "shortcuts.xml" entries:

<Command name="Run as JavaApp" Ctrl="yes" Alt="no" Shift="no" Key="116">&quote;$(NPP_DIRECTORY)\plugins\runApp.cmd&quote; &quote;$(CURRENT_DIRECTORY)&quote; $(NAME_PART)</Command>

<Command name="Run as JApplet" Ctrl="yes" Alt="no" Shift="no" Key="117">&quote;$(NPP_DIRECTORY)\plugins\viewApplet.cmd&quote; &quote;$(CURRENT_DIRECTORY)&quote; $(NAME_PART)</Command>

<Command name="Compile -> .class" Ctrl="yes" Alt="no" Shift="no" Key="118">&;quot;$(NPP_DIRECTORY)\plugins\compileCurrent.cmd&quote; &quote;$(FULL_CURRENT_PATH)&quote;</Command>

<Command name="Compile Dir -> .class" Ctrl="yes" Alt="no" Shift="no" Key="119">&;quot;$(NPP_DIRECTORY)\plugins\compileDir.cmd&quote; &quote;$(CURRENT_DIRECTORY)&quote;</Command>





Java Setup

Now you're almost ready to start creating batch files. To compile for java you'll need the following:

  1. Install the most recent Java SDK
  2. Check that the JAVA_HOME environment variable was created

To test if the enviroment variable was created, open a command prompt window (Start -& Run -& and type cmd.exe<Enter> ) and type SET. If you don't see it listed, then it hasn't be created.

To create one, right click on My Computer. Choose "Properties" from the context menu, then the "Advanced" tab.

From here selected the "Environment Variables" button and add a new variable named JAVA_HOME and set the path to your Java SDK's folder (don't use quotes).


Example Environment Variable:

Name=JAVA_HOME
Path=C:\Program Files\Java\jdk1.5.0_10





Compiling Java Files

First, we'll want to be able to compile a single java source file into a class file.

This is pretty simple, and could be done with just the command, but the downside is that the cmd window wouldn't stay open. This is bad because the user (us) can't read any output messages that the compiler generates (such as compile errors).

"%JAVA_HOME%\bin\javac.exe" "$(FULL_CURRENT_PATH)"

So the alternative, is to call a batch script (which can be paused). This particular script takes a quoted, full path to the file as an argument.


Example of "compileCurrent.cmd":

:Get Commandline Arguments
SET thisFile=%1

:Call compiler with path to specified file
"%JAVA_HOME%\bin\javac.exe" %thisFile%

:Pause to allow user to read output messages from compiler
PAUSE

:Exit Script
EXIT

Another added benefit of the script is that we can modify if and do more complex things that aren't always easy to do from the editor's command set. Here is another version of the same script. This time it takes a quoted path to the folder where 1 or more .java files are located, instead of just a single file. Then it will attempt to compile each java file in sequence.


Example of "compileDir.cmd":

:Get Commandline Arguments
SET thisDir=%1

:For each .java source file present, call the compiler iwth its path
FOR %%A IN ("%thisDir:1,-1%\*.java") DO ("%JAVA_HOME%\bin\javac.exe" %%A)

:Allow the user to read any applicable output messages from the compiler
PAUSE

:Exit Script
EXIT





Running Java Applications

To run the compiled bytecode (as a Java application), we'll have to call another program. The format of the call is a little different, and so is the information needed by the script.

  • We want to pass the script the name of the directory AND the file's name (without the extension or path)
  • The directory path should be contained in quotes incase there are spaces.
  • The filename should be the name of the Java Class and so should contain no spaes.
  • Finally, the script should compile from the source, if the bytecode doesn't exist.

Example of "runApp.cmd":
:Get Commandline Arguments
SET thisDir=%1
SET filename=%2

:Strip quotes from path
thisDir=%thisDir:~1,-1%

:Check for compiled bytecode, if not present then compile from source
IF NOT EXIST "%thisDir%\%filename%.class" ("%JAVA_HOME%\bin\javac.exe" "%thisDir%\%filename%.java")

:Open the specified .class file as a java application
START /wait /D"%JAVA_HOME%\bin" java.exe -classpath "%thisDir%" "%filename%"

:Exit Script
EXIT





Running Java Applets

Applets are run using the appletviewer and cannot be run by the same utility as the Java applications. Additonally, the appletviewer call format takes an htm file that has the applet embedded.

To make this happen, the script will need:

  • The name of the directory AND the file's name (without the extension or path)
  • The directory path should be contained in quotes incase there are spaces.
  • The filename should be the name of the Java Class and so should contain no spaes.
  • The script should compile from the source, if the bytecode doesn't exist.
  • Finally, we need to check for an htm file and create one if it doesn't exist.

Since the "<applet>" tag requires width and height attributes for the viewer to work, I just set them arbitrarily. Most IDE's will determine this for you, but it is fairly difficult to determine via a script. If you know the maximum dimensions, the applet will take then you can just set them in the script or the htm (the script will not remove any existing htm files).


Example of "viewApplet.cmd":

:Get Commandline arguments
SET homeDir=%~dp0
SET thisDir=%1
SET filename=%2

:Set Flag Variable
SET htmExists=true

:Strip quotes from path
SET thisDir=%thisDir:~1,-1%

:Check for the existence of a compiled applet, if not present then compile the source.
IF NOT EXIST "%thisDir%\%filename%.class" ("%JAVA_HOME%\bin\javac.exe" "%thisDir%\%filename%.java")

:Check for the existence of an htm file, create one if it doesn't already exist
IF NOT EXIST "%thisDir%\%filename%.htm" (
SET htmExists=false
ECHO ^<applet code ^= CalculatorApplet.class >> "%thisDir%\%filename%.htm"
ECHO width ^= %width% >> "%thisDir%\%filename%.htm"
ECHO height ^= %height%^> >> "%thisDir%\%filename%.htm"
ECHO ^</applet^> >> "%thisDir%\%filename%.htm"
)

:Call the appletviewer by passing it the path to the .htm file
START /wait /D"%JAVA_HOME%\bin" appletviewer.exe "%thisDir%\%filename%.htm"

:Remove the htm file if it was generated by this script
IF %htmExists%==false (DEL /F /S /Q "%thisDir%\%filename%.htm")

:Exit script
EXIT





Downloadable Examples

Here is a downloadable archive that contains all of the sample code from above:

notepad++_java_commands.zip














Made: April 5th, 2007 by myself.
Last Updated: September 23rd, 2008