Skip to main content Skip to navigation

MA117 Help

Avoiding the MaInput library - Creating more portable code

If you've been writing your code on the mimosa or primrose computers, you've probably been using the MaInput class to read input from the terminal. When you start writing code on your laptop, you may notice that suddenly the Java compiler complains about not being able to find MaInput... If this is the case you can either acquire the MaInput library and add this to your classpath, or you can write some more portable code to do the same job.

Here is some example code using the MaInput library to read in an integer and then print it back out.

class Test {
public static void main(String[] args) {
MaInput minput = new MaInput();
int value = minput.readInt("Enter an integer: ");
System.out.println("The number you entered was: " + value + ".");
}
}

This will behave like so:

mimosa> javac Test.java
mimosa> java Test
Enter an integer: 4
The number you entered was: 4.

Now we can recreate this behaviour without using the MaInput library. Assuming you have a version of Java newer than 1.5 (sometimes called 5.0) this code will run on any installation. We will be using the Scanner class to do the reading. First we need to import the Scanner class by inserting: "import java.util.Scanner;" at the very top of our Java file (before the class declaration).

We can then use this in a similar way:

import java.util.Scanner;

class Test { public static void main(String[] args) {
// Instead of using MaInput, we use Scanner instead on the systems input stream (System.in) Scanner sinput = new Scanner(System.in);
// Scanner doesn't print out the message you tell it to like MaInput, so we print this ourselves first
System.out.print("Enter an integer: ");
// Here we use a function called nextInt() (instead of readInt()) to take the next integer from the terminal.
// Search google for "javadoc Scanner" to find more functions for reading in different kinds of values int value = sinput.nextInt(); System.out.println("The number you entered was: " + value + "."); } }

This should work in exactly the same as the MaInput example but it much more portable.

Using X-Forwarding on Windows

In the lab the application links to start mimosa or primrose sessions also creates an X-Forwarding session (where the display is forwarded over SSH to the local machine). If you use Linux or a Mac when logging into mimosa or primrose you can enable X-Forwarding by using "ssh -YC mimosa.warwick.ac.uk" to enable trusted and compressed forwarding (or you can use -X to use untrusted fowarding with no compression, etc.).

On windows you will need an SSH client and an X11 client. I suggest PuTTY and Xming for these two tasks. You can either install Xming and PuTTY separately, or use the Xming installer (http://sourceforge.net/project/downloading.php?group_id=156984&filename=Xming-6-9-0-31-setup.exe) to install plink.

Using XLaunch and plink

Now you can use XLaunch to launch Xming and an SSH client. When it asks how to start Xming, select "Start a program" which will also start a plink ssh session.

Then choose "Run Remote" from the next menu and select "Using PuTTY (plink.exe)". Connect to computer location will be either mimosa.. or primrose.warwick.ac.uk.

The next menu screen can remain unchanged. You may choose to save your settings for easy start-up next time or you can follow the instructions again.

When you connect, an xterm window should be started that you can use as your SSH client.

Using Xming and PuTTY

To use true PuTTY as your SSH client, you will need to install it first. Then start XMing (but this time on XLaunch choose "Start no client".

Then you can launch PuTTY and type in the hostname of the machine you wish to connect to. Then goto the X11 menu screen (Connection -> SSH -> X11).

Select "Enable X11 forwarding" and enter the X display location as: localhost:0

Then connect and it should open a session to mimosa/primrose.

Testing your X11 session

To test your session run a simple program such as xclock and see if it appears on your screen correctly.