I have found that typing
javac Program.java, followed by
java Program, every time I want to run a program is annoying. However, there's no need to swap over to a GUI just for that one feature, since I'm still learning the language. I suspect that this is a common gripe among Java beginners. Fortunately, there is a solution that lets you sharpen your coding skills while saving yourself the trouble of typing commands into the console over and over again! Batch files are very easy to adjust to your needs, and let you reduce all that repetitive typing down to a little programming and one click.
What are batch files? Essentially a set of commands that the Windows console runs. Batch files let you save all those commands you type over and over again into a list that executes when you click it. Batch files can be extremely useful, but they can also mess up your computer! For this reason
you should avoid running batch files made by other people, especially if you do not understand what the batch file's commands will do. With that disclaimer out of the way, let's get on to a nifty way to save some keystrokes!
If you've ever coded a Java program in Notepad, you have the skills to write a batch file. The format is very simple, no headers or footers, all you have to do is save the commands with the ".bat" extension and Windows will recognize it as a batch file. Commands are not case-sensitive, but it's good form to type your batch commands in UPPERCASE so you can easily distinguish batch commands from commands that will be executed by the console. Batch files will also run out of the directory they are placed in, so put your batch file in the same directory as your .java program that you want compiled and run.
On to the good stuff, here's a .bat file I made for a program called "Converter":
@ECHO OFF
TITLE Converter
javac "Converter.java"
PAUSE
ECHO.
java "Converter"
PAUSE
Most of the code is easy to understand, the javac statement is one we've all typed before, and "PAUSE" seems self-explanitory enough. Let me break this down line-by-line.
@ECHO OFF
-Tells the batch file not to print (or in the language of batch files, echo) all the commands in the file while it's executing them.
title <title>
-Sets the title of the console window that the batch file will open to execute your commands.
javac <program.java>
-We all know what javac does, compiles a .java file. That's the meat of what this batchfile does, except it saves you the bother of typing it over and over every time you make a small change.
PAUSE
-Pauses command execution until it recieves keyboard input. I put one right after javac so the batch file does not charge on before I can read the debugging messages. That way I know if compiling was successful, or what I need to fix. Best of all, if the program does not compile, I can click the "X" button in the upper-right corner and make some adjustments, without being forced to run the rest of the commands.
ECHO.
-ECHO with a period directly after it creates a blank line. An excellent spacer, much like System.out.println(""); in Java.
java <program>
-Runs your program. A cool thing to note is that if your program did not compile properly, the previous version of your program is still saved as the .class file. The .class file is what is actually run, so you can
test out your old iteration if you so desire, or just close the window.
PAUSE (again)
-Yet another pause. I have this so if my program is simple, such as a math formula that calculates a result and prints it, the window will remain open until I press a button.
This simple batch file can be easily changed for any program you are writing. Replace every time the
word "Converter" is mentioned with the title of your program, save it as a .bat, and you can now compile and run your java program in a single click. No more opening the console and searching for a directory, no more typing the same code over and over again just to debug a glitch, and you even get a nice named console window!
These commands work on Windows 7, and since they are based on Microsoft's MS-DOS, you should be able to run them on XP as well. Mac has files that work similarly to batch files, but since I don't have a Mac I can't advise on those. Again, be careful running batch files you haven't written or do not fully understand! Batch files can be used to screw up your computer, just like many other types of code. I take no responsibility for what you do on your own computer, and share this information only with the hope that it might be helpful to other aspiring Java programmers.
If you are interested in using more advanced batch files to launch your programs, such as conditional statements to launch or compile different programs depending on the situation, I found this site to be very helpful:
http://www.computerhope.com/batch.htm
Hope this is of use to someone! The Coderanch community has been very helpful to me, and I'd like to give back a mite.