Skip to content

Using JISA in Java

William Wood edited this page Jul 10, 2019 · 10 revisions

Using JISA in Java

This page serves to show you how to create a project in IntelliJ IDEA to write a JISA based program in Java.

Contents

Creating a Project

First you need to bring up the new project wizard. You can do this either by going to File > New > Project... or by pressing Create New Project on the welcome screen.

Then select Java in the list on the left-hand-side and press Next. On the next screen, select Create project from template and select Command Line App then press Next:

Decide what to call your project and put that in Project name. The Base package is essentially the name of the project used inside Java, so keep it all one word.

Now click Finish and your new project should appear before your very eyes!

Including JISA

Before we can do anything towards creating a JISA application, you will need to include the JISA.jar library file into your project.

First, download the jar file:

https://github.com/OE-FET/JISA/raw/master/JISA.jar

Next, move it into your project folder by dragging it into the project tree on the left:

Now you can add it as a library by right clicking on it and selectin Add as Library... then just click OK:

Main Class, Main Method

After creating a new Java project you will notice that you get a class called Main with a public static method inside it called main(...). This is what's known as the "entry point" of your program, that is to say it is the code that will run first when you run the program.

The first thing to do is to let Java know that your program might throw an exception. To do this, add throws Exception to the end of the main() method declaration like so:

public class Main {

  public static void main(String[] args) throws Exception {

  }

}

This isn't ideal, as really you should be catching any and all exceptions in your code so that you can deal with them appropriately. This way, any exception that is thrown by main() will cause the program to crash. However, it will do to start with.

Test out that Java is working by creating a simple Hello World! application like so:

public class Main {

  public static void main(String[] args) throws Exception {

    System.out.println("Hello World!");

  }

}

Run it by clicking the green play button on the top toolbar. You should see the output at the bottom reading something like:

/usr/lib/jvm/java-8-oracle/bin/java...

Hello World!

Variables

Java is what's known as a "statically typed" language. This means that before using a variable in Java, it needs to be "declared". This is where you state its type and name. For example, if we wanted to store an integer, in a variable called myInteger, then we would have to declare our variable as an int like so:

int myInteger;

// Now we can use myInteger, for instance:
myInteger = 5 + 3;

In the example above we decalred myInteger and assigned it its first value on separate lines. However, we can actually do this all on one line like so:

int myInteger = 5 + 3;

The basic variable types are as follows:

Name Description
int Integer (whole numbers)
long Integer but can hold more digits (at the expense of more memory)
float Floating-point number (basically a non-integer number)
double Double-precision floating-point (same as float but higher precision)
boolean A simple true or false value
char A single character (eg a or K)
Clone this wiki locally