Dr Java Tutorial |
![]() |
|---|
The goal of this tutorial is to introduce Dr Java IDE, learn to type Java Syntax and execute Java Program.
Steps
- Open Dr. Java IDE
- See Desktop for shortcut icon OR
- Click on KMenu > Applications > Seas Menu > drjava
![]()
- In the interactions pane write a sequence of Java commands that will calculate area of circle
- Declare a constant called PI equal to 3.14
- Declare variable called radius and intialize radius to 3.0
- What are data types for identifiers PI and radius?
- Print answer as follows: Area of the circle is =
- Create a folder called Program1. Download and Save the file Hello.java to your created folder.
- Open file Hello.java
- Open the Hello.java file using the "Open" icon.
- Contents of Hello.java
- Every Java program begins with the word class followed by the class name which is same as filename i.e. Hello 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, which is a Java Syntax rule.
- 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 main is the entry point of your program and when it executes it prints "Hello World" in the "interactions" pane.
- 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 folder/directory where you saved Hello.java, select "All Files" and then you should be able to see Hello.class file in the folder. Do not try to open this file using Dr Java or any text editor, if you do you will see a bunch of garbage.
- 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" pane 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, edit you file such that program output says "Java Rocks!!"
- Compile and Run your program to test your output.
- Adding and Removing jar file
- 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.
- Reset interaction pane and then 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
- 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
- Help > Help Or press F1 (see image)
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...).
- Follow instructions to install Dr. Java for particular type of operating system.
- Note:
- Make sure jdk and jre have the same version type., e.g. 1.5.0_09 and jre1.5.0_09, otherwise Dr Java does not work properly.
- WindowsVista OS has some trouble with version 1.6, so download version 1.5 on your machines