Beginner's Guide to NetRexx

Introduction

NetRexx is the best computer programming language available. You might suspect that the writer is biased and you would be correct. However, if you program for a little while in NetRexx, you may just end up with the same bias! I say that after spending over 30 years programming in COBOL, FORTRAN, various assembly languages, APL, BASIC, REXX, JCL, CLIST, C, PERL, etc. Some NetRexx programmers I know have far more language experience than mine.

This guide assumes that the reader has some familiarity with computer programming and knows what computer languages are, as well as why there are compilers or translators for those languages.

You may have heard that NetRexx is "compatible" with the Java programming language or that NetRexx is a "dialect" of Java or some similar statement. While there is truth in those statements, they can also be misleading. This document will try to clarify the relationship between NetRexx and Java as well as giving an idea of what can be done with NetRexx.

If you want to verify that the examples in this guide actually work, you will need to first wade through the NetRexx installation document to get a minimal development environment running. An easier way to try things if you are familiar with the "jEdit" file editing environment or have a few minutes to try it, is to install the NetRexx plugin from the jEdit Plugin menu and cut and paste the examples into that environment.

This document is not intended for experienced Java programmers. Those readers should simply know that NetRexx is an alternative JVM language like Groovy or JPython which can be used to produce Java source code, class files or jar files, and take a look at this document comparing NetRexx and Java syntax: NetRexx at Once

What is NetRexx?

In a practical sense, NetRexx is a completely unique language that just happens to be able to use any Java objects and to run anywhere that Java can run. Under the hood, NetRexx code can be converted into Java code by the NetRexx Translator and compiled to create Java objects that are just like those produced by Java programmers. But NetRexx code can also be directly interpreted as a scripting language by the NetRexx Translator which runs in a Java Virtual Machine (JVM).

With a suitable IDE, NetRexx code can even be compiled to run in environments similar to the JVM such as the Dalvik VM used on Android devices (like cellular phones and tablet computers) or the leJOS NXT VM for robots. NetRexx was created by Mike Cowlishaw, at that time working for IBM, who also wrote the primary NetRexx book ("The NetRexx Language") as well as the NetRexx Translator.

Why use NetRexx?

The NetRexx language was inspired by the REXX programming language and follows the same principles. The main principle is that the language should be easy for programmers to read and write without regard for how difficult it might be for a computer to understand. For example, NetRexx does not use unneeded special characters (like braces) to signal program structure to the Translator as some other languages do.

What can you do with NetRexx?

The short answer is: Anything! More precisely, because NetRexx can produce Java programs and run in most Java environments, you can do anything with NetRexx that a Java programmer could do in Java. Can you write GUI (graphical user interface) programs with NetRexx? Yes! Can you write major applications in NetRexx? Yes! Can you write NetRexx programs which call Java code? Yes - all of the Java classes and objects in any Java libraries available can be used by NetRexx programs.

What can you NOT do with NetRexx?

Not much. But you cannot directly mix Java source code with NetRexx source code - they are different animals and the translators for each language will choke if they see code from the other language. However, once Java code and NetRexx code have been separately compiled into machine language (JVM code), they can access each other with no problems.

Enough propaganda - show me

Every programming language intro is obligated to start with the traditional "Hello World" program so here it is in NetRexx:

say "Hello World"

OK, no big deal - lots of languages can do that program in one line. But how many can do an Internet browser in one line? Here is one in NetRexx (Pass a web address to this program):

say URL(arg).getcontent

Granted, that program is of limited use since it only tells you if a web document exists but that is more than enough to do stress testing or access verification for web sites, so it can be considered a practical application and an interesting NetRexx example.

Some readers may be interested in examining the content of the web document obtained above, so here is a program that will list the document source code:

pagereader=BufferedReader(InputStreamReader(InputStream URL(arg).getcontent ))
loop forever
   line=pagereader.readLine
   if line = null then leave
   say line
   end

Let's get graphical

Maybe you want to know how easy it is to do something more complex, perhaps with a GUI interface. Try this program (move your mouse pointer in the window while pressing the mouse button):
class pencilapp extends Applet

x =int 0 ; y =int 0

method mouseDrag(e=event,i=int,j=int) public returns boolean -- if mouse is moved with button pressed, draw a matching line
getGraphics.drawLine(x,y,i,j)
x = i ; y = j
return 1

method mouseDown(e=event,i=int,j=int) public returns boolean -- when mouse button is pressed, record it's location
x = i ; y = j
return 1

method main(argv=String[]) public static -- if no Applet window is available, build a window frame and put this object in it
p=pencilapp()
f=Frame("pencil test")
f.resize(400,300)
f.add("Center",p)
f.show()
The above graphical drawing program can run all by itself or can be embedded in a web document.

What about Java library objects?

The examples above make use of various classes or objects from the Java libraries, such as the URL class, the Applet class and the Frame class. Java items like these can be used directly in NetRexx programs without doing anything special because they are simply external objects to NetRexx. These library objects (much like external functions) should not be considered to be part of the Java language but simply as part of the virtual machine environment that Java and NetRexx programs run in. (The assumption here is that you have a valid Java environment available to run programs in, of course.)

For example, the Java library documentation tells us that there is a "Date" class which can be used to create a Date object for the current time:

Date()
          Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

This Java object can be created and displayed as a string by NetRexx with the following statement:

say Date()

In fact that one line is a complete NetRexx program that creates and then displays a Java object in a text format . The NetRexx program loads the Java library object at run time and calls it as needed. The conclusion that you should draw is that it is very easy to work with Java items from within NetRexx. Often easier than it would be from Java itself.

A quick glossary