Dr Java Tutorial |
 |
The
main goal of this tutorial is to show how to create / compile / run
Java program using DrJava.
Steps
- Create a folder called Program1
- All files for this tutorial can be found at this website and saved to the above folder you created
- Save the file Hello.java to Program1 folder
- Open Dr. Java IDE (see image)
- Click on KMenu > Applications > Seas Menu > drjava
- Open file Hello.java
- Open the hello.java file using the "Open" icon.
- Click
the “Save” icon that is located on the top left side
of the screen. (Or press Ctrl + S)
- Contents of Hello.java
- Every Java program begins with the word class followed by the class name and curly { } to to indicate the start and end of the class behavior
- Important
: The file name must have the same name as your class. (In this case, it
should be Hello)
- Notice color highlighting on certain words. The ones on blue color is the Java Syntax. The ones in green color are some comments for us to know details about the program or particular line. The comments start with "//" or start with "/*" and end with "*/", which tell the Java compiler that this should be ignored and not be part of the java program.
- For now ignore what the meaning the syntax such as "public static void main(String[] args)". The only thing you need to know is that when the program executes it prints "Hello World" in the "interactions" tab.
- Compiling
your program
- Click
the “Compile” icon that is located on the top of the
screen. (Or press F5)
- You
must see the Compilation complete message in the "Compiler Output" tab that is located at the bottom of the
screen (See Image)
- Hello.class contains machine code (known as byte code) that is independent of the any computer hardware
- If you open the Program1 folder/directory, you should be able to see Hello.class file in the folder.
- Executing/Running
your program
- Click
the “Run” icon that is located on the top right side
of the screen. (Or press F2)
- You should Hello World print in "Interactions" tab
- During the execution phase the Java Virtual Machine translates the byte code into the machine code that processor understands
- The machine code is then executed on your computer
- You can also execute your program using the command java Hello in the "interactions" tab in Dr Java
Welcome to DrJava. Working directory is S:\Javaprojects\Program1
> java Hello |
- What
if there are errors in your code?
- Try
erasing the semi colon (;) in your program. (Located at the end of the
“System.out.println(...)” line)
- Now
try to compile your program again
- In the "Compiler Output" tab you see:
1 error found:
File: S:\Javaprojects\Program1\Hello.java [line: 12]
Error: S:\Javaprojects\Program1\Hello.java:12: ';' expected
|
- Click on the "Reset" icon to clear the error message.
- Fix the semi colon and the program should compile again.
- Editing your files
- In file Hello.java, add another line that reads:
- System.out.println("We are in room Moore 207");
- Put the above statement after line System.out.printl("Hello World");
- Repeat steps 4-6 for program Square.java
- Remove the file Hello.java by clicking on the "Close" icon.
- Save Square.java to Program1 folder and open this file using "Open" icon. Then click save.
- Compile and Run the program (steps 4 and 5) and observe the output - What does the program do ?
- Adding and Removing Jar
- A Java Archive (JAR) file is a way of storing many pre-compiled Java classes (i.e. files with extension .class). If you want to run a pre-compiled program, Dr Java needs to know where to find the file with extension .jar
- Save HelloGui.jar to Program1 folder and do the following:
- Click on Edit > Preferences
- Under Resource Locations (at left) , click Add button under Extra Classpath
- Browse and chose theHelloGui.jar. Click Apply and then Ok.
- In the interactions tab type: java HelloGui. A small window with "Click Me" button should pop up. Press to see what happens.
- Making java archives is good way porting your java program from one machine to another
- Also remember what makes Java programs portable is that pre-compiled machine code (a.ka. byte code) in .class is independent of any machine (and or operating system)
- The Java Virtual Machine (JVM) then converts the byte code to appropriate machine code for that particular machine and hence able to excecute the same java program that was written on a different machine
- To remove jar file, go to Edit > Preferences and highlight the HelloGui.jar
- Then click Remove, Apply and OK buttons respectively.
- Changing Settings
- In
order to change any settings (fonts, colors, compiler options,
etc...) of DrJava programming environment go to Edit > Preferences
- For
example, if you want to display the line numbers, under Preferences > Display Options check the “Show
All Line Numbers” box.You should be able to see line numbers per line.
- For
any kind of help, consult
Working with DrJava at home
If
you want to work with DrJava at home, then there are various
resources that explain how to install Java & DrJava to any
operating system (Windows, Mac, Linux...).
- Download the Java Developement Kit ( JDK 6u2 ) for windows OS and install it on the your computer. Make sure that C:\ProgramFiles\Java\ contains jdk1.6.0_02 and jre1.6.0_02 directories after installation.
- Follow instructions to install Dr. Java for particular type of operating system.