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":
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.
-Sets the title of the console window that the batch file will open to execute your commands.
-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.
-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.
-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.
Excellent post, good info!
Sooner or later, though, you'll probably find that the capabilities of batch files will fall short of what you want to accomplish.
It is for this very reason that build tools like Apache Ant and Apache Maven exist.
In fact, we have a dedicated subforum for tools like these right here on the ol' 'Ranch: Ant, Maven and Other Build Tools.
So if you find that your batch file approach simply doesn't cut it anymore, I highly recommend having a look at such tools!
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Here are a few pointers for those who can't be bothered with batch files regardless of Jack's explanation:
- If you hold shift and right-click a folder, you can click a menu item with a caption like "open command prompt here".
- In the command prompt, the up and down keys will move backward and forward through the command prompt history, so you can summon commands you typed before.
Good stuff Jack! Batch files do come in handy and most are easy to write. I use two that I wrote a long time ago on a daily basis ... one that opens all the programs I use and the second one is a fast shutdown that clears my temp folders. Oh and I wrote a third one that enables/disables Windows update services at my convenience instead of Microsoft's -- I plan on converting that one into a Java project.
As a side note, batch files written for windows vista and higher should be saved as .cmd as .bat will sometimes not work.
Knowing how to write batch files is a good skill for a programmer to possess.
Stephan van Hulst
Joined: Sep 20, 2010
Buenny Fry wrote:As a side note, batch files written for windows vista and higher should be saved as .cmd as .bat will sometimes not work.
Only on operating systems that use command.com. cmd.exe will run either .bat or .cmd just fine. Saving as .cmd only avoids accidental execution on an older OS.
Joined: Jun 04, 2012
Stephan van Hulst wrote:Only on operating systems that use command.com. cmd.exe will run either .bat or .cmd just fine. Saving as .cmd only avoids accidental execution on an older OS.
That is true. I only added that side note because .bat files kept failing on vista but as soon as I renamed them to .cmd they executed just fine. So when I transitioned to windows 7, I kept the habit of using the extension .cmd instead of .bat.
subject: Compiling Java programs through .bat files