Compile & Run Java Files With TextWrangler On Mac


Compiling & Running Java Code On Mac TextWrangler

TextWrangler is a great free text editor by Bare Bones Software. It provides color-coding for almost every computer language out there, which is extremely helpful for troubleshooting programs and scripts. This article will show you how to add Java compile and run scripts to TextWrangler, which will allow you to run Java files via the Terminal. This will allow you to compile and run Java files much like TextPad on PC.

Sources for the information and scripts provided in this article are courtesy of Gerry Jenkins, a professor at Long Beach City College, and George Benainous, a computer science teacher at Hollywood High. Although I have tested these scripts and they run on my Mac OS 10.7.5 system running TextWranger 4.0, I cannot guarantee that the scripts will run on your system, nor can I guarantee that it will run on earlier or later operating systems or versions of TextWrangler. If you are unfamiliar or uneasy with using the Terminal portion of your OS, please seek outside assistance or do some quick online research.

This method is designed to give Mac Java beginners an alternative to TextPad. The following guideline will create two scripts that will allow you to compile and run Java files and programs using TextWrangler and the Mac Terminal.

STEP 1
TextWrangler is a free text editor developed by Bare Bones Software. Download this software and install on your system.

STEP 2
Once installed, open up TextWrangler. Navigate to the Scripting menu (the second menu to the left of Help menu). Select the "Open Script Editor" option.

STEP 3
Next, you will copy and paste the following block of code into the Script Editor.

tell application "TextWrangler"
save text document 1
set the_file to file of text document 1
end tell

set AppleScript's text item delimiters to ":"
set source_file to the last text item of (the_file as string)

tell application "Finder"
set the_folder to container of file the_file
end tell

tell application "Terminal"
activate
set p to POSIX path of (the_folder as string)
set shell_script to "cd " & (quoted form of p) & ¬
"; javac " & source_file
if (count windows) is 0 then
do script shell_script
else
do script shell_script in the front window
end if
end tell

STEP 4
In the Script Editor, go to File/Save As. Make sure that File Format is set to "Script". Navigate to your MainDrive/Users/User/Library/Application Support/TextWrangler/Scripts directory. Save this file as Compile java.scpt.

STEP 5
Once saved, go to File/Close. You will need to create another script, so go to File/New. Next, copy and paste the following block of code into the Script Editor.

tell application "TextWrangler"
set the_file to file of text document 1
end tell

set AppleScript's text item delimiters to ":"
set source_file to the last text item of (the_file as string)
set compiled_file to text 1 thru -6 of source_file

tell application "Finder"
set the_folder to container of file the_file
end tell

tell application "Terminal"
activate
set p to POSIX path of (the_folder as string)
set shell_script to "cd " & (quoted form of p) & ¬
"; java " & compiled_file
if (count windows) is 0 then
do script shell_script
else
do script shell_script in the front window
end if
end tell

STEP 6
In the Script Editor, go to File/Save As. Make sure that File Format is set to "Script". Navigate to your MainDrive/Users/User/Library/Application Support/TextWrangler/Scripts directory. Save this file as Run java.scpt .

STEP 7
Once saved, you can quit the Script Editor. Now, restart TextWrangler. Navigate to the Scripting menu (the second menu to the left of Help menu). You should now see the 2 scripts we just created.

STEP 8
Start writing a Java program. Running the Compile and Run scripts will open up the Terminal. If there are errors in your file, the Compile command will show you what lines the errors are occurring. If no errors are present, a .class file will be compiled in the same folder as the file currently open. When everything is good to go, run the “Run java” script to see the result of your program.

Leave a Reply

Other Insights

Let's Work Together

Please enable JavaScript in your browser to complete this form.