Java cygwin

From TinyOS Wiki
Jump to: navigation, search

Common Java problems on Cygwin

When installing TinyOS on Cygwin, there are often problems with installing the TinyOS Java libraries and the TinyOS native libraries. We will look at how to setup the PATH and the CLASSPATH environment variable as well as how to properly install the TinyOS native libraries.

Environment variables in Cygwin

There are two ways of specifying environment variables in Cygwin. You can either specify a Windows environment variable or an environment variable that is specific to Cygwin. Typically the PATH environment variable is a Windows variable, while the CLASSPATH environment variable is specific to Cygwin. Since both variables are used by Java, and Java is not aware of the Unix-like environment of Cygwin, these variables need to be in the standard Windows style (Windows Path names that separate directories with a backslash (\) and that separate paths with a semicolon (;)).

Windows Environment Variables

Open the Control Panel, then select the System application. Select the Advanced tab. There is a button Environment variables. There are two types of environment variables: variables that are specific to a user and variables that are system-wide. The PATH variable usually is system-wide. To modify a variable, you can double-click on the variable. The PATH variable should include the directory to your Java executables (both the java.exe and the javac.exe executables). It is a good idea to put the PATH entries for Java as early in the variable as possible.

Cygwin Environment Variables

When you open a new Cygwin shell, the scripts located in the Cygwin directory /etc/profile.d are executed. Typically there should be a tinyos.sh file there (it might be named differently). If there is no such file, you might want to create one. You can define environment variables in the standard bash way, for instance: CLASSPATH="c:\cygwin\opt\tinyos-2.x\support\sdk\java\tinyos.jar;.". Make sure that you export the variable at the end of the file with a command like export CLASSPATH. If you create a new tinyos.sh file, make sure it is executable: chmod a+x /etc/profile.d/tinyos.sh. If you make modifications to the tinyos.sh script, be sure to close and reopen your Cygwin shell to activate the modifications.

Here is an example of the tinyos.sh file for TinyOS 1.x:

TOSROOT="/opt/tinyos-1.x"
TOSDIR="$TOSROOT/tos"
CLASSPATH=`$TOSROOT/tools/java/javapath`
MAKERULES="$TOSROOT/tools/make/Makerules"

export TOSROOT
export TOSDIR
export CLASSPATH
export MAKERULES

type java >/dev/null 2>/dev/null || PATH=`/usr/bin/tos-locate-jre --java`:$PATH
type javac >/dev/null 2>/dev/null || PATH=`/usr/bin/tos-locate-jre --javac`:$PATH
echo $PATH | grep -q /usr/local/bin ||  PATH=/usr/local/bin:$PATH

And here is the example for TinyOS 2.x:

TOSROOT="/opt/tinyos-2.x"
CLASSPATH="c:\cygwin\opt\tinyos-2.x\support\sdk\java\tinyos.jar;."
MAKERULES="$TOSROOT/support/make/Makerules"
TOSDIR="$TOSROOT/tos"

export TOSROOT
export TOSDIR
export CLASSPATH
export MAKERULES

type java >/dev/null 2>/dev/null || PATH=`/usr/bin/tos-locate-jre --java`:$PATH
type javac >/dev/null 2>/dev/null || PATH=`/usr/bin/tos-locate-jre --javac`:$PATH
echo $PATH | grep -q /usr/local/bin ||  PATH=/usr/local/bin:$PATH

TinyOS native libraries for Java

The command that installs the libraries is tos-install-jni. This command relies on another command tos-locate-jre to find the Java installation. tos-locate-jre again relies on proper configuration in the Windows registry. Unfortunately it seems that current versions of Java from different vendors do not properly configure the Windows registry. Configuring the registry properly solves all kinds of problems, therefore I recommend you make the effort to manually fix the Windows registry.

Before changing the registry, check whether it might already be configured probperly: tos-locate-jre --jni. If this command shows you a path and that path is correct, then you don't have to do anything. Otherwise you'll have to configure two keys in the registry. To open the registry, click on Start, then Run, then type regedit and click on ok. You will get an explorer-like interface that shows you the registry.

The current version of Java is determined by the registry entry \HKLM\SOFTWARE\JavaSoft\Java Development Kit\CurrentVersion (HKLM is a short-hand for Host_Key_Local_Machine). If this key does not exist, you might want to create it. If your Java version is 1.5.0, then the value of this registry key should also be 1.5.0.

The path to a given version of Java should be specified in the registry key \HKLM\SOFTWARE\JavaSoft\Java Development Kit\<VERSION>\JavaHome, where <VERSION> is the version of the Java installation. If your current Java version is 1.5.0, there should be a registry key \HKLM\SOFTWARE\JavaSoft\Java Development Kit\1.5.0\JavaHome. If it doesn't exist, you should create it. Its value is the path to the Java installation. For instance, if the java executables are located in c:\JAVA\jdk1.5.0\bin, then this value should be c:\JAVA\jdk1.5.0.

Once you have fixed the registry, check that the command tos-locate-jre --jni gives you the correct path. Then install the TinyOS native libraries for Java with the command tos-install-jni.