Tim Evans Java Tips Page

Tim Evans Computing Tips Main Page Tim Evans Informal Home Page | Tim Evans Imperial College page

I use the free Netbeans IDE for programming. Eclipse is the other good option.

The CERN Colt routines are good for lists (vectors, variable length arrays) linear algebra and data analysis. I have yet to find a readable introduction to this package or simple examples but they must be out there.

The JUNG java graph library looks very good for networks but I have yet to break into it. There are good example routine provided but the manual lags well behind the implementation. I have also found it to be very slow on some occasions. I suspect I do not know how to use it efficiently and that my Java needs to be improved to get the most out of this. The JUNG wiki might provide the answers to my problems.

David Eck's Java Notes are an excellent and free introduction. I even bought the printed version as I liked them so much. I also use the input routines described in his book which are also provided free, such as textreader.java.

Outputting graphics to bitmap files is easy. For instance look at the GraphEditorDemo.java in the JUNG package. Postscript or other vector file output is less trivial. The VectorGraphics library of FreeHEP Java Libraries looks very useful and claims to contain PostScript, PDF, EMF, SVF, and Flash SWF, along with the image formats GIF, PNG, JPG and PPM. I have also seen recommended jlibeps which is a fork of the no longer free (if cheap) EpsGraphics2D.

Options parsing looks easy using the ritopt package but I have yet to try it.

Running java code

I use the following in a batch file
java -Xmx2000m  -classpath "%PRGDIR%\JAVA\TheProject\dist\project.jar";. project.MakeNetworks %1 %2 %3 %4 %5 %6 %7 %8 %9
here this show the use of an environment variable PRGDIR which is set using the set command in Windows batch files. The -Xmx2000m sets the virtual memory to be 2GB. This is the amount of memory you give your code. Note that I found that 1.4Gb was about the most I could get under 32bit versions. I found I could expand it to 3Gb on a 4Gb machine only if I used the 64bit version of java.

Unfortunately it appears it is easy to have several versions of java of different ages and types (32 or 64 bit) so be careful. It is not always obvious which one plain java calls when entered from the command line. The Program Files holds the java files in a java directory however it is possible that other programmes have their own versions and that these are being run. I found QuickTime on my system was messing things about. Some commands I find useful to work out what is going on, all run in a command window are as follows

"FULLPATH\java" -version
"FULLPATH\java" -XshowSettings:all
The version should display 64bit in its message if its a 64 bit version. If you do not see this then you are running a 32bit version. I uninstalled the older versions I could see version and found the explicit path to the latest one and copied this into the command window to be sure I was running a 64 bit version e.g.
C:\>"C:\Program Files\Java\jre7\bin\java" -version
java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
This was how I found typing just java was running something else.

Output Formatting

To format output of numbers etc in a manner similar to C, you can look at the Formatter class of java. In practice a number of other classes call this automatically, so you can work pretty much as in C and not refer to the details unless you have special needs. For instance
String outString = String.format("%6.3f",doubleValue);
gives a string representation of the doubleValue which fills 6 spaces, and keeps 3 digits after the decimal point. Remember that this means the sign, decimal point and the integer part must all fit in 6-3=3 characters.
System.out.printf("%9.3e",doubleValue);
gives the scientific notation. Again the 3 is the number of digits after the decimal point but now there is always just one integer between 1 and 9 in front of the decimal point. The power of ten is then given after an e character. So you need to leave room for up to two signs, the e and the decimal place. The g flag will choose between f and e flags as needed.

For integers don't use i, the flag is d

String outString = String.format("%6d",intValue);
For hexadecimal the flag is x.

String Processing

The java classes String and Character have some useful routines, especially when combined with regular expressions (regex). It is worth googling for help on regular expressions. Note that many regex use the backslash character. To get a backslash into a java string you need to use two backslash characters to represent one backslash. In the examples below line is a string.

JavaDoc

Very useful to provide basic documentation. Search for javadoc to find details but here are some basic examples. The see tag useful for links to other code. below are the most general forms but you should have these first for the current class (no package.Class needed) and then for the current class (no Class needed). and then for the full form given below.
   @see package.Class
   @see package.Class#field
   @see package.Class#Constructor(Type, Type...)
   @see package.Class#Constructor(Type id, Type id)
   @see package.Class#method(Type, Type,...)
   @see package.Class#method(Type id, Type, id)
If you want a link to code in the main text then use
{@link package.class#member label}

For URL, in the main text of a javadoc you can use the URL and any text you like (e.g. the full URL)

<a href="http://netplexity.org">http://netplexity.org</a>
Alternatively at the end you can add a
@see <a href="http://netplexity.org">http://netplexity.org</a>

For constants it is useful to use the value tag

{@value package.class#field}

If you want to have text in the font used for code without the text being interpreted as HTML markup, e.g. for equations, then try

{@code text}

Java File Separator

This is a system-dependent character, used to separate directories in paths (not sure why this is called the file separator). This is '/' on UNIX and '\' on Windows. It is given by the system property java.io.File.separatorChar or as a string string java.io.File.separator or using
System.getProperty("file.separator");
. However java seems to be tolerant and at least on windows paths can contain a mixture of both forward and backwards slashes and they still work.

Java Path Separator

This is a system-dependent character, used to separate filenames (that is the full path to a file) in a sequence of files given as a path list, e.g. in the classpath argument of the java command line. On UNIX systems, this character is ':'; on Microsoft Windows systems it is ';'. In java it is accessible as a char java.io.File.pathSeparatorChar or as a string java.io.File.pathSeparator or through
System.getProperty("path.separator");

Java and System Environment Variables

Java can access the environment variables of a system through
System.getenv(env);
where String env contains the name of the environment variable. null is returned if it does not exist.

Java System Properties

Java knows several key things about the system, see System Properties in the Sun documentation. For instance to test if a directory name ends in the right type of slash, and then add the character if not present, we could use
 final String fs = java.lang.System.getProperty("file.separator");
 if (!directoryName.endsWith(fs)) directoryName=directoryName+fs;
though the file separator is available as constants discussed above. Perhaps more useful are items which could vary on one machine. In particular Might be useful. A longer list of useful system properties is as follows:- java.lang.System.getProperty(String key) returns the following system properties:-
Key Description of Associated Value
java.version Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
java.home Java installation directory
java.vm.specification.version Java Virtual Machine specification version
java.vm.specification.vendor Java Virtual Machine specification vendor
java.vm.specification.name Java Virtual Machine specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor Java Virtual Machine implementation vendor
java.vm.name Java Virtual Machine implementation name
java.specification.version Java Runtime Environment specification version
java.specification.vendor Java Runtime Environment specification vendor
java.specification.name Java Runtime Environment specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path List of paths to search when loading libraries
java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory

Java Versions

Errors such as java.lang.UnsupportedClassVersionError generally indicate that the java classes or jar files, or parts of them, have been created by a version of java which is later than the one being used to run that java. To solve this you can either upgrade the java used to run it or, add an older version to the system being used to build the classes or jar files. For the latter, SUN keeps all the old versions. Download and several versions can live alongside each other. Under netbeans, go to Tools - Java Platforms and then add platform. Select the directory where the version of java you want resides, typically in Program Files/Java. Then for each project go to its properties, select libraries and change the Java platform from the dropdown menu.

Odds and Ends