Testing, Debugging, and Verification TDA566/DIT082, LP2, HT2011

OpenJML - JML syntax and type checker

This page describes how to use OpenJML, a toolset for handling JML annotations. In this course you will mainly use the parser, however the tool is also capable of type checking, static checking and run-time checking (by converting your JML specifications to run-time assertions).

An Eclipse plugin is also available, however this requires you to run Java 1.7, which might not yet be available for your operating system. The tool supports Java programs written in versions 1.4, 1.5, 1.6 and 1.7. For a complete overview of its features, you can go here.

In the descriptions below we use a simple test file that can be found here: JMLTest.java

On the Chalmers Windows StuDAT

The Chalmers Windows machines run Java 1.6, therefore you can not use the Eclipse plugin. To use the command line, do the following:

To check whether java is available and the command line, press Win+R, enter cmd and press enter. Type java -version. If you get the response that the command is not available, add it to the path:

set PATH=%PATH%;"C:\Program Files (x86)\Java\jre6\bin"

where the exact installation path of java may differ. Check whether the path is correct now by the command echo %PATH% and if java -version gives a response now.

Download the OpenJML package here or, since tar.gz might not be handled, we have a local copy in ZIP format here. In this tutorial we assume that you extract this file to the same folder as where JMLTest.java resides, if not, you need to change the paths in the following instructions accordingly.

Navigate the windows commands prompt to the folder with the extracted jars and the JMLTest.java. To check the JML annotation run:

java -Xbootclasspath/p:openjml.jar -jar openjml.jar -noInternalSpecs JMLTest.java

The tool will find one error that we left in the file on purpose, fix it, save the file, and run the same command again to see that everything is correct now. Note that if you forget to include the -noInternalSpecs flag you will get a bunch of warnings related to the internal JML specification in OpenJML.

On the Chalmers Linux StuDAT

The Chalmers linux machines (RedHat Enterprise 5.6) run SUN's java development kit (JDK) version 1.6. Hence you are not able to use the Eclipse plugin when working on these platforms. The command-line tool does work fine, following the instructions below.

Start by opening a terminal and navigating to the directory where the JML-annotated .java file is located (in our example the JMLTest.java file). We will place the required OpenJML files in this same directory, however if you want a more generic solution you could place them in a different location (but note that you need to adjust the paths below accordingly).

Download the OpenJML command line toolset:

wget http://jmlspecs.sourceforge.net/openjml.tar.gz
Extract:
tar xzvf openjml.tar.gz
Of main importance are the .jar files. The user guide might be of interest as well, however if you open it you will find many sections are still "to be added later". Now, assuming that you have the JMLTest.java file in this same directory, we can check the JML syntax in this file:
java -Xbootclasspath/p:openjml.jar -jar openjml.jar -noInternalSpecs JMLTest.java
The tool will find one error that we left in the file on purpose, fix it, save the file, and run the same command again to see that everything is correct now. Note that if you forget to include the -noInternalSpecs flag you will get a bunch of warnings related to the internal JML specification in OpenJML.

This should be enough for the labs and exercises, however if you want to play a bit more with OpenJML, you can try its runtime assertion checking (rac) tool. We can request from OpenJML to compile the java file with inserted assertions that check the properties you specified in the JML:

java -Xbootclasspath/p:openjml.jar -jar openjml.jar -rac -noInternalSpecs -source 1.6 -target 1.6 JMLTest.java
Note in particular the additional flags for the 1.6 JDK. You might get a warning that the bootstrap class path is not correctly set, which has something to do with compiling against the incorrect run-time platform. You can ignore this warning, or use -Xlint:-options as an additional flag to not even see it.

Once compiled, you can run the program (with the OpenJML jars in the classpath). Look at the sourcecode of the program and provide it with arguments that break the invariants and preconditions, e.g.:

bash-3.2$ java -classpath ':*' JMLTest 12
JMLTest.java:5: JML invariant is false
JMLTest.java:5: JML invariant is false
Balance: 13
JMLTest.java:5: JML invariant is false
Done
bash-3.2$ java -classpath ':*' JMLTest -3
JMLTest.java:10: JML precondition is false
Balance: -2
Done

On your own Linux system

If you are running java version 1.6 (check with terminal command java -version) you can follow the same instructions as above. However, if you have or want to install version 1.7 (via your package manager or via Oracle) you might prefer to use the Eclipse plugin. Note that Eclipse has to be running in Java 1.7 virtual machine. You can change the default machine (on Debian/Ubuntu based systems) by the command

sudo update-alternatives --config java
And similar for javac. If you run the command line tools instead of the eclipse plugin, note that you then can leave out the -Xbootclasspath/p:openjml.jar from the instructions for the StuDAT system, as well as the -source and -target flags.

Install the Eclipse plugin (requires Eclipse 3.6 or later). Go to the menu Help -> Install New Software... Click on the button Add and enter the following information:

Name:     openjml  
Location: http://jmlspecs.sourceforge.net/openjml-updatesite
Select the OpenJML package, click Next and accept the license agreements presented (if you choose to). After installation, Eclipse needs to be restarted. If everything went okey, you should now find the following buttons somewhere in your main menu bar:

Clicking the coffeecup will make OpenJML check your specification and mark red what is incorrect (you do need to manually save the file before). The ESC button can be used for static checking, but we will not cover that now. The RAC button compiles the programs and inserts Run-time Assertion Checks based on your JML annotation. Try the JMLTest.java file mentioned on the top of this page. Change the main function to not use the argument from the user but supply in directly in the code if you like, and see if you can break the invariants (and whether the RAC detects them). Note that everytime you save you have to press RAC first and Run after that, otherwise the 'standard' compiler (without assertions) is used.

If the OpenJML icons do not appear, you are most likely not running Eclipse in Java 1.7.

On your own Windows system

Checking your syntax under Windows works with both JDK 1.6 and 1.7.

We assume that you have the java command available at the command line. To check this, press Win+R, enter cmd and press enter. Type java -version to see if and which version of java is there. If you know java is installed but it does not show up on the command line, you can add it to the path as follows:

set PATH=%PATH%;"C:\Program Files\Java\jdk1.7.0_01\bin"

where you should of course use the right path to your installation. For syntax checking only a jre (not compiler) is also sufficient. Check whether the path is correct now by the command echo %PATH% and if java -version gives a response now.

Download the OpenJML package here and extract it to the same location as JMLTest.java (if you place it in a different directory you have to update the paths below accordingly). Download 7-zip if your current installation does not support this type of compression.

Navigate the windows commands prompt to the folder with the extracted jars and the JMLTest.java. To check the JML annotation run:

java -Xbootclasspath/p:openjml.jar -jar openjml.jar -noInternalSpecs JMLTest.java

The tool will find one error that we left in the file on purpose, fix it, save the file, and run the same command again to see that everything is correct now. Note that if you forget to include the -noInternalSpecs flag you will get a bunch of warnings related to the internal JML specification in OpenJML.

This should be enough for the labs and exercises, however if you want to play a bit more with OpenJML and you are running JRE/JDK 1.7, you can try its runtime assertion checking (rac) tool. We can request from OpenJML to compile the java file with inserted assertions that check the properties you specified in the JML:

java -jar openjml.jar -rac -noInternalSpecs JMLTest.java
We tried to do the same trick to get it working with 1.6 as in Linux (see higher up this page) but it didn't work. If you can find a way let us know.

Once compiled, you can run the program (with the OpenJML jars in the classpath). Look at the sourcecode of the program and provide it with arguments that break the invariants and preconditions, e.g.:

C:\Users\Bart\Desktop\openjml>java -classpath .;* JMLTest -1
JMLTest.java:9: JML precondition is false
Balance: -1
Done

C:\Users\Bart\Desktop\openjml>java -classpath .;* JMLTest 14
JMLTest.java:4: JML invariant is false
JMLTest.java:4: JML invariant is false
Balance: 14
JMLTest.java:4: JML invariant is false
Done

If you are running 1.7, you can also try the eclipse plugin.

Install the Eclipse plugin (requires Eclipse 3.6 or later). Go to the menu Help -> Install New Software... Click on the button Add and enter the following information:

Name:     openjml  
Location: http://jmlspecs.sourceforge.net/openjml-updatesite
Select the OpenJML package, click Next and accept the license agreements presented (if you choose to). After installation, Eclipse needs to be restarted. If everything went okey, you should now find the following buttons somewhere in your main menu bar:

Clicking the coffeecup will make OpenJML check your specification and mark red what is incorrect (you do need to manually save the file before). The ESC button can be used for static checking, but we will not cover that now. The RAC button compiles the programs and inserts Run-time Assertion Checks based on your JML annotation. Try the JMLTest.java file mentioned on the top of this page. Change the main function to not use the argument from the user but supply in directly in the code if you like, and see if you can break the invariants (and whether the RAC detects them). Note that everytime you save you have to press RAC first and Run after that, otherwise the 'standard' compiler (without assertions) is used.

If the OpenJML icons do not appear, you are most likely not running Eclipse in Java 1.7.

Note: if you get errors saying that packages don't exist, eclipse probably has the problem that is cannot parse the path (linux vs windows issue) correctly because of the whitespace in "Program Files". The easiest options is to add C:\Program Files\eclipse\plugins\org.jmlspecs.OpenJMLUI_0.2.5\jmlruntime.jar (or a similar path) to your project's build path. You will still get the failure messages but at least the tool works :-)

General issues

If you get the message "Unsupported major.minor version 51.0" this means that the version of the compiler you used differs from the version of the virtual machine you are running. Fix this in your OS / IDE.




Home | Course | Schedule | Exam | Exercises | Labs | Evaluation | Tools | Links W. Ahrendt, V. Klebanov, Nov 18, 2011