Java Development To-Go: Compiling with a Text-EditorIntroAt 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 FilesThe 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:
The scripts I will demonstrate are as follows:
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 CommandsNext 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 ""e;" is used in place of a real quote (") because the file is in XML. List of other escapable characters can be found here.
Java SetupNow you're almost ready to start creating batch files. To compile for java you'll need the following:
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).
Compiling Java FilesFirst, 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).
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.
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.
Running Java ApplicationsTo 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.
Example of "runApp.cmd":
Running Java AppletsApplets 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:
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).
Downloadable ExamplesHere is a downloadable archive that contains all of the sample code from above: Made: April 5th, 2007 by myself.Last Updated: September 23rd, 2008 |