reJ :: "Hello World" -tutorial

This tutorial shows how to create a simple Java application from scratch using the reJ GUI to create bytecode for the Java virtual machine.

When run, the class prints out "Hello, world" on the console.

It is probably not very sensible to create Java applications with reJ, instead the intention of this tutorial is to give a tour of the basic funcionalities of the reJ GUI.

1 Define the location of rt.jar

From the reJ menu, select Tools / Preferences

In the Preferences dialog, click on Add to classpath..

Navigate to the folder of a Java Runtime Environment installed on your computer and select the jar/zip file containing the runtime classes for the JRE.

In the case of a Sun Microsystems JRE this would be the rt.jar file in the lib folder.

reJ Preferences

2 Create the project

From the reJ menu select File / New / Project..

Select a folder where to create the new archive and give the file a name.

For example:

  helloworld.jar

3 Create a new class into the archive

From the reJ menu select File / New / Class..

In the Create new class dialog, leave the package as [Default] and in the Class field type:

  Hello
Create New Class

Click OK.

4 Create a main method in the class

If you're not on the Files tab, navigate to it.

Double-click on the Hello.class entry in the tree (it should be the only entry in the list in an empty node)

Open Class

After a brief processing, the title of the Files tab should now read Selected file: Hello.class and the selected Hello.class should appear in bold.

Navigate to the Editor tab.

You should see an empty class definition for your Hello class:

Bytecode Editor

Click on Insert.. and in the menu which appears, choose Insert Method..

4.1 Set method access, name and return type

In the Method editor, check the following access options: public and static

Make sure the rest of the access-definition checkboxes are unchecked.

In the name field, type:

  main

Leave the Return type -field as void.

4.2 Set Parameters

Click on the button(...) next to the Parameters -field.

Setting Method Parameters

In the parameter Chooser dialog, click Add..

In the Type Chooser dialog, click on the button "..." next to the type -field.

In the Choose Class dialog, type String in the search box, and select the String in the list for which the package is java.lang.

Choose String

If there are no classes listed in this dialog, verify you did the step number 1 as instructed.

Click OK to close the Choose Class -dialog.

Select (or type) 1 for the Array Dimensions -value.

Type Chooser

Click OK to close the Type Chooser -dialog.

The Parameter Chooser -dialog should now have one entry in the list:

  java.lang.String[]
Parameter Chooser

Click OK to close the Parameter Chooser -dialog.

4.3 Set Max Stack Size, Max Locals

You should now be back in the Method Editor -dialog.

In the Max Stack Size field, set the value as 2

Ensure that the Max Locals value is 1

Method Editor Done

Click OK to finish creating the new method.

Your Hello -class should now have an empty main method (with the standard main-method signature).

Editor With main-method

5 Add code to the main method

5.1 Get the System.out field and push it to the stack

Select the method signature line (the line that reads: public static void main(String[] p0))

Right click on the mouse and select Insert instruction..

Inserting Instruction

The Instruction Editor -dialog should appear.

In the Instruction field, select getstatic.

In the parameters section, click on the "..." button next to the Field -field.

The Choose Field -dialog should appear.

Click on the "..." button next to the Class -field.

The Choose Class -dialog should appear. Choose the class System (in the java.lang package) by typing all or a part of the name, or by choosing it from the list.

Click OK to return to the Choose Field -dialog.

In the Field -field, select the following entry:

  java.io.PrintStream out
Choosing System.out

Click OK to close the Choose Field -dialog.

The selected field is now visible in the parameters section of the instruction editor.

Click OK to close the Instruction Editor.

Instruction Done

5.2 Create a String constant

Navigate to the Constant pool -tab

Click on the "Insert.." button.

Insert Into Constant Pool

In the dialog that appears, choose String constant.

In the "Insert String Info" -dialog, type:

  Hello, world.

Click OK to close the dialog.

Notice two new entries in the constant pool:

String Constant Created

5.3 Push the String constant to the stack

Navigate back to the Editor -tab.

Select the getstatic instruction that you created in the step 5.1

Right click on the mouse and select Insert after.. from the pop-up menu.

In the Instruction -field, select ldc

Verify that in the parameters section, in the constant field, the String constant you created in the step 5.2 is selected.

ldc Done

If the field is empty and there are no values to select, go back to step 5.2 and verify you did everything as instructed.

Click OK to close the Instruction Editor.

5.4 Invoke the println method of the PrintStream class

Select the newly created ldc instruction.

Right click on the mouse and select Insert after.. from the pop-up menu.

In the Instruction -field, select invokevirtual

In the parameters section, click on the "..." button next to the Method -field.

The Choose Method -dialog should appear.

Click on the "..." button next to the Class -field.

The Choose Class -dialog should appear. Choose the class PrintStream (in the java.io package) by typing all or a part of the name, or by choosing it from the list.

Click OK to return to the Choose Method -dialog.

In the Method -field, select the following entry:

  void println(java.lang.String)
Method Selected

Click OK to close the Choose Method -dialog.

The selected method is now visible in the parameters section of the instruction editor.

Click OK to close the Instruction Editor.

5.5 Add a method return which doesn't return any value (void)

Select the newly created invokevirtual instruction.

Right click on the mouse and select Insert after.. from the pop-up menu.

In the Instruction -field select return

Click OK to close the Instruction Editor.

Click the Save button to save your archive file project.

Your class should look like the following:

Finished Class

6 Test the class

In your operating system commandline mode, navigate to the folder where you created the archive file.

Ensure that the java command is in the path (instructions for this are out of the scope of this tutorial)

If you followed the naming suggestions in this tutorial, type the following command:

  java -cp helloworld.jar Hello

The output should be similar to the following:

  C:\Documents and Settings\Sami\Desktop>java -cp helloworld.jar Hello
  Hello, world.