Chris Thompson - AC2CZ - Amateur Radio Station

Just enough Java

Back to Index

2018-Feb-18 - Tutorial 0: Java programming

There are many good Java tutorials online, so it is not my intention to repeat those. If you know nothing about Java, if you have never compiled a Java program, then you may need to start with the official Java Tutorials and go from there. If you know a bit about Java or have used lots of other programming languages, then here are some notes to get you started with these tutorials.

Firstly you need to install the Java Development Kit or JDK. This includes the compiler needed to turn Java code into compiled files that can be run. You can install the latest JDK from here. As of 2020 I would install Java 11.

With Java installed you need to decide if you are going to use an Integrated Development Environment (IDE). If you have never used an IDE then there is a learning curve and I would skip it for now. If you have used lots of IDEs before then I would install Eclipse. Eventually you will need an IDE because it makes development much faster.

If you have not worked through Tutorial 1, then do that in parallel with this. This tutorial tells you how to organize, compile and run the Java code in Tutorial 1.

First of all you need to know that Java stores its code in lots of files. Typically we put one class in each file. Each class defines a Java object. You should be able to cut and paste the code examples I give into separate files, or if you are confused, grab them from the github.com repository to see how I have stored them in files.

If you work your way through Tutorial 1 and put the classes in the same Java packages as I did (which is not required) then they will be in sub-folders as follows:


tutorial1/signal/Oscillator.java
tutorial1/SdrTutorial1.java

Note that I also put all the files for each Tutorial in its own package. In this case Tutorial1. I suggest you rename that to Sdr or some other name. I only put them in separate packages so I could show progress as the classes develop. Note that if you change the name of the tutorial1 directory you need to change the package name at the top of each file.

If we look at the start of the SdrTutorial.java files we see the following:


package tutorial1;
import org.jtransforms.fft.DoubleFFT_1D;
import tutorial1.signal.Oscillator;

We then get the line that defines the class, which has exactly the same name as the file that it is in. The lines above mean that the class, SdrTutorial1 is in the package called tutorial1. By convention we give packages names that start with a lower case and classes a name that start with upper case. We then "import" two other classes so we can use them in this class. We need to do that each time we use another class. A good IDE like Eclipse automates that for you. I can't remember which packages things are in but I know the names. When I type them in Eclipse it allows me to right click and pick the package that I want to import.

If we are including a third party Library that did not come with Java, such as the JTransforms library, then they are stored in files ending with .jar. These are actually just a zip file full of compiled Java classes. When we compile our Java code the compiler needs to be able to find them. They need to be in a folder that is on the "class path". If you are using an IDE then you need to add the libraries to the class path. I'll tell you below how to add it on the command line. To add JTransforms in eclipse, put the Jar file in a suitable directory that you can find. Then go to Project > Properties > Java Build Path > Libraries and click "Add External JARs". Select the JTransforms Jar file.

If you are running from the command line then you will need to run javac for each file with a classpath that includes JTransforms when that is imported. I found that a simple batch file like this worked on windows. A bash script would work on Linux or MacOs.


@echo off
echo Building SDR Java
set path=%path%;C:\Program Files\Java\jdk1.8.0_144\bin
set CLASSPATH=%CLASSPATH%;.;C:\Users\chris\Desktop\workspace\Sdr\lib\jtransforms\JTransforms-3.1-with-dependencies.jar
javac tutorial1\signal\Oscillator.java
javac tutorial1\SdrTutorial1.java

A couple of things to note about the build file above. This is a windows ".bat" file. If you are on Unix then the directory separator is a forward slash vs a back slash. Also replace "@echo off" with something like #!/bin/bash, assuming that is the path to your shell. Make the build script executable with "chmod +x . I have also specified the full path to the Java binaries and the JTransforms library. These will need to match the location of those files on your computer.

Also remember that I put all of my classes in a package called tutorial1. If you did not, then you won't need that part of the path when you specify each Java source file. Or if you used a different top level package, then change the directory name in the build script.

If everything compiles, you can run it with another short script. At run time java needs to know where to find the JTransforms classes, so we again need to include it in the classpath


@echo off
echo Running SDR Java
set path=%path%;C:\Program Files\Java\jdk1.8.0_144\bin
set CLASSPATH=%CLASSPATH%;.;C:\Users\chris\Desktop\workspace\Sdr\lib\jtransforms\JTransforms-3.1-with-dependencies.jar
java tutorial1.SdrTutorial1

Note that I have not miss-typed the slash as a dot in the line above. We are not passing the filename to java. We are passing the name of the class including the package it is in. I put it in the package tutorial1 and the class is called SdrTutorial1. Java can also use the Unix style forward slash to specify its class names, so you can also use "tutorial1/SdrTutorial1" but you can not use the back slash! If you did not use a top level package then no need for it or the dot.

You can also run this with a single command like this (using a relative path to the library in the class path and making sure we also include the current directory because we are referencing the main class from there):


java -cp ".;..\lib\jtransforms\JTransforms-3.1-with-dependencies.jar" tutorial1.SdrTutorial1

Or, if you have already set up the classpath by setting the environment variable CLASSPATH, then you can run the program with the command:


java tutorial1.SdrTutorial1

Which is just the java run time environment followed by the location of the main class.

Of course if you know what you are doing then you can create a Make script with a program like Ant or Maven.

You might want to also look at the following slightly more advanced Java topics:

Back to Index

Copyright 2001-2021 Chris Thompson
Send me an email