The measures for compiling, running, and debugging Java code rely on your advancement environment and also operating system.We avoided placing these details in the key text, because they can be distracting.Instead, we provide this appendix with a brief introduction to DrJava – one integrated advancement environment (IDE) the is fine suited because that beginners – and also other advance tools, consisting of Checkstyle for code quality and also JUnit for testing.
You are watching: What development tool do you use to launch java bytecodes?
A.1 installing DrJava
The easiest way to begin programming in Java is to usage a website that compiles and also runs Java password in the browser.Examples incorporate jdoodle.com, compilejava.net, tutorialspoint.com, and others.
If you space unable come install software program on your computer (which is frequently the situation in public schools and also Internet cafés), you have the right to use this online breakthrough environments for almost everything in this book.
But if you desire to compile and also run Java program on your own computer, you will need:A straightforward text editor such together Notepad++ or Sublime Text, and/or an concept such together DrJava, Eclipse, jGrasp, or NetBeans.
The JDK us recommend is Java SE (Standard Edition), which Oracle makes accessible for free.The concept we recommend is DrJava, i m sorry is an open-source development environment written in Java (see Figure A.1).
To install the JDK, find the internet for “download JDK” which should take you come Oracle’s website.Scroll down to “Java Platform, traditional Edition” and also click the download switch under JDK.Then expropriate the patent agreement and select the installer for your operating system.Don’t forget to operation the installer after girlfriend download it!
To download DrJava, visit http://drjava.org and download the jug file.We recommend the you conserve it come your desktop or an additional convenient location.Simply double-click the JAR record to operation DrJava.Refer to the DrJava documentation (http://drjava.org/docs/quickstart/) for an ext details.
When to run DrJava for the very first time, us recommend you change three settings from the edit > Preferences food selection under Miscellaneous: set the Indent Level come 4, inspect the immediately Close Block comments box, and uncheck the keep Emacs-style backup Files box.
A.2 DrJava interactions
One of the most advantageous features the DrJava is the “Interactions Pane” at the bottom of the window.It offers the capability to shot out code quickly, without having actually to write a class definition and save/compile/run the program.Figure A.2 shows an example.
There is one subtle detail to note when using the interactions feature.If girlfriend don’t finish an expression (or statement) v a semicolon, DrJava automatically displays that value.Notice in Figure A.2 how the change declarations end with semicolons, but the logic expression in the adhering to lines execute not.This attribute saves girlfriend from having actually to type System.out.println every time.
What’s nice around this function is that you don’t have to develop a new class, declare a main method, compose arbitrary expressions within System.out.println statements, conserve the resource file, and get all of your password to compile in advance.Also, you can press the up/down arrows top top the key-board to repeat previous commands and experiment with incremental differences.
A.3 Command-line interface
One of the most powerful and useful skills you can learn is just how to use the command-line interface, additionally called the “terminal”.The command line is a straight interface to the operation system.It allows you to operation programs, manage files and also directories, and also monitor mechanism resources.Many progressed tools, both for software development and general purpose computing, are easily accessible only at the command line.
There space many great tutorials digital for discovering the command line for your operation system; just search the internet for “command line tutorial”.On Unix systems favor Linux and OS X, girlfriend can gain started v just four commands: readjust the working brochure (cd), list magazine contents (ls), translate into Java programs (javac), and also run Java program (java).
Figure A.3 shows an instance where the Hello.java source record is stored in the desktop directory.After an altering to the location and also listing the files, we use the javac command come compile Hello.java.Running ls again, we watch that the compiler created a brand-new file, Hello.class, which contains the byte code.We operation the program using the java command, which displays the calculation on the complying with line.
Note that the javac command needs a filename (or multiple resource files separated by spaces), vice versa, the java command calls for a single class name.If you use DrJava, it operation these commands for girlfriend behind the scenes and also displays the output in the interactions Pane.
Taking time to find out this efficient and also elegant method of communicating with your operating device will make you more productive.People that don’t use the command heat don’t recognize what castle missing.
A.4 Command-line testing
As explained in Section 1.8, it’s an ext effective to program and debug your code tiny by small than to attempt writing whatever all at once.And after did you do it completed programming an algorithm, it’s vital to test that it works effectively on a selection of inputs.
Throughout the book, we show techniques for trial and error your programs.Most if not all trial and error is based on a straightforward idea: does the program execute what we mean it to do?For straightforward programs, that not an overwhelming to operation them several times and see what happens.But at part point, you will certainly get tired of typing the very same test situations over and over.
We can automate the process of beginning input and also comparing “expected output” through “actual output” using the command line.The simple idea is to store the test cases in plain text files and also trick Java right into thinking they are coming from the keyboard.Here are step-by-step instructions:Make sure you can compile and also run the Convert.java instance in the ch03 magazine of ThinkJavaCode.In the same magazine as Convert.java, develop a level text document named test.in (“in” is because that input).Enter the following line and also save the file:
|193.04 centimeter = 6 ft, 4 in|
|java convert test.out|
On the command line, and > space redirection operators.The first one redirects the materials of test.in to System.in, together if the were gone into from the keyboard.The 2nd one redirects the contents of System.out come a new record test.out, lot like a display capture.In other words, the test.out file contains the calculation of her program.
By the way, that perfectly okay to compile her programs in DrJava (or some other environment) and also run them from the command line.Knowing both techniques allows you to usage the right tool because that the job.
At this point, we simply need to compare the components test.out through test.exp.If the files are the same, then the regime outputted what we intended it to output.If not, climate we found a bug, and also we deserve to use the output to begin debugging our program.Fortunately, there’s a simple way to compare documents on the command line:
|diff test.exp test.out|
The diff energy summarizes the differences in between two files.If there room no differences, climate it displays nothing, which in our instance is what we want.If the expected output differs from the yes, really output, then we need to proceed debugging.Usually the regimen is in ~ fault, and also diff offers some insight about what is broken.But there’s also a chance that we have a correct program and also the expected output is wrong.
Interpreting the outcomes from diff deserve to be confusing, however fortunately over there are plenty of graphical tools that display the differences in between two files.For example, on windows you can install WinMerge, on Mac you deserve to use opendiff (which comes v Xcode), and also on Linux yes meld, displayed in Figure A.4.
Regardless of what tool you use, the goal is the same.Debug your regime until the actual output is identical come the supposed output.
A.5 to run Checkstyle
Checkstyle is a command-line tool that have the right to be offered to determine if your source code complies with a set of style rules.It also checks for common programming mistakes, such as course and technique design problems.
You have the right to download the latest version as a JAR record from http://checkstyle.sourceforge.net/.To operation Checkstyle, move (or copy) the JAR record to the same magazine as her program.Open a terminal in the location, and also run the following command:
|java -jar checkstyle-*-all.jar -c /google_checks.xml *.java|
The * personalities are wildcards that complement whatever version of Checkstyle friend have and also whatever Java resource files are present.The output shows the document and line variety of each problem.This instance refers come a technique beginning on heat 93, shaft 5 that Hello.java:
|Hello.java:93:5: missing a Javadoc comment|
The paper /google_checks.xml is within the JAR record and represents many of Google’s layout rules.You can additionally use /sun_checks.xml or administer your own configuration file.See Checkstyle’s website for an ext information.
If you use Checkstyle to your source code often, you will most likely internalize an excellent style habits over time.But over there are boundaries to what automatic layout checkers can do.In particular, they can not evaluate the quality of your comments, the meaning of your variable names, or the structure of your algorithms.
Good comments do it easier for proficient developers to recognize errors in her code.Good variable names communicate the will of her program and how the data is organized.And good programs space designed to be efficient and demonstrably correct.
A.6 Tracing through a debugger
A good way come visualize the circulation of execution, including how parameters and also arguments work, is to usage a debugger.Most debuggers make it possible to:Set a breakpoint, a line wherein you desire the program to pause.Step with the code one heat at a time and also watch what the does.Check the values of variables and see when and also how lock change.
For example, open any kind of program in DrJava and move the cursor come the an initial line of main.Press Ctrl+B to toggle a breakpoint on the current line; it must now be emphasize in red.Press Ctrl+Shift+D to revolve on Debug Mode; a new pane should show up at the bottom the the window.These regulates are also accessible from the Debugger menu, in case you forget the faster way keys.
When you run the program, execution pauses in ~ the an initial breakpoint.The debug pane screens the call stack, v the current technique on top of the stack, as presented in Figure A.5.You could be surprised to view how numerous methods to be called before the main method!
To the best are number of buttons that enable you to step through the password at your own pace.You can additionally press Automatic trace to clock DrJava operation your password one heat at a time.
Using a debugger is like having the computer system proofread your code out loud.When the routine is paused, you deserve to examine (or even change) the value of any kind of variable utilizing the interaction Pane.
Tracing permits you to monitor the flow of execution and also see how data pass from one method to another.You could expect the code carry out one thing, however then the debugger reflects it act something else.At that moment, you get insight around what might be wrong v the code.
You can edit your password while debugging it, yet we nothing recommend it.If you add or delete multiple lines of password while the regime is paused, the results have the right to be confusing.
See http://drjava.org/docs/user/ch09.html for more information about using the debugger feature of DrJava.
A.7 testing with JUnit
When beginners start writing methods, they usually test them by invoking them native main and checking the results by hand.Writing code prefer this can acquire repetitive, however there room tools to make it easier.For instances where we know the ideal answer, we can do much better by creating unit tests.
For example, to check fibonacci from Section 6.9, we could write:
|public revolution void main(String<> args) if (fibonacci(1) != 1) System.err.println("fibonacci(1) is incorrect"); if (fibonacci(2) != 1) System.err.println("fibonacci(2) is incorrect"); if (fibonacci(3) != 2) System.err.println("fibonacci(3) is incorrect");|
This test password is self-explanatory, but it’s much longer than it demands to be and also it no scale very well.In addition, the error messages provide restricted information.Using a unit test frame addresses these and also other issues.
JUnit is a usual testing tool for Java program (see http://junit.org).To usage it, you have actually to produce a test course that consists of test methods.If the surname of your course is Class, the name of the test course is ClassTest.And if there is a an approach in course named method, there have to be a technique in TestClass called testMethod.
For example, expect that the fibonacci technique belongs to a class named Series.Here is the matching JUnit test class and test method:
|import junit.framework.TestCase;public class SeriesTest expand TestCase public void testFibonacci() assertEquals(1, Series.fibonacci(1)); assertEquals(1, Series.fibonacci(2)); assertEquals(2, Series.fibonacci(3));|
This instance uses the keyword extends, which suggests that the brand-new class, SeriesTest is based on an existing class, TestCase, i m sorry is imported from the parcel junit.framework.
Many breakthrough environments can generate test classes and test methods automatically.In DrJava, you have the right to select brand-new JUnit Test case from the record menu to generate an empty test class.
assertEquals is provided by the TestCase class.It takes 2 arguments and also checks even if it is they are equal.If so, the does nothing; otherwise it screens a detailed error message.Normally the first argument is the “expected value”, which we take into consideration correct, and also the 2nd argument is the “actual value” we desire to check.If they room not equal, the check fails.
Using assertEquals is an ext concise than writing your very own if statements and also System.err messages.JUnit provides additional assert methods, such together assertNull, assertSame, and also assertTrue, that have the right to be supplied to design a variety of tests.
To run JUnit straight from DrJava, click the Test switch on the toolbar.If all her test approaches pass, girlfriend will check out a eco-friendly bar in the lower-right corner.Otherwise, DrJava will certainly take you straight to the an initial assertion that failed.
See more: 2,3-Dimethyl-1-Butene