Java and SCons – First steps to Android development
Current repository revision: 40 (Testing)
Code highlight key
Grey: Code unchanged
Red: Code removed
Green: Code added
Blue: Code not shown→ Denotes the current line and the next are really one line
The next series of blog posts is going to highlight the path followed to reach the goal of using SCons to build a simple Android application. The steps taken to achieve this are:
- Building Java with SCons
- Using JUnit for testing in Java/SCons builds
- Using Buildbot to run Java/SCons compile/test cycles
- Integrating Java/SCons builds with Eclipse
- Testing a GUI application with Java/SCons and JUnit
- Including the Android API in a Java/SCons build
- Building an Android app with Java/SCons
- Dual GUI (Android and Swing) development
Java and Android are subjects that are new to us, so these blog posts will follow the learning process rather than being the advice of experts. As such we hope they will highlight and solve many of the problems people who are also coming to Java and Android for the first time may encounter. Hopefully the series will help anyone wanting to start out.
SCons is to be used for this development as it is a build system we have worked with before (and blogged about) which handles Java simply (or so it seems at the moment) and saves time on learning a new build system like Ant or Maven.
Now onto our first experience of building Java with SCons!
Hello, Java World!
To create Java programs using SCons it is necessary to have Java, the Java Development Kit (JDK), SCons and Python installed. Other requirements will be covered when they are first used.
Most computers will already have Java installed. To check whether it is installed, and whether it’s the latest version, simply go to the Java website and click on the Do I have Java? link (ensuring Java is enabled in the browser!). From that page Java can be downloaded and installed. At the time of writing our installed version is Version 6 Update 26.
To program in Java the JDK also needs to be installed. This includes the core packages of interfaces and classes used in the Java language, as well as the javac Java compiler and other development tools.
Now, for the first program (and a test that the Java and JDK are correctly installed), the Java version of Hello, World! can be written. In a directory called scons_test a directory named src was created. The src directory is created as it will be used by SCons later. Inside this src directory, a file named HelloWorld.java was edited with a text editor to contain:
/*
* Java version of Hello, World!
*/
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello, Java World!");
}
}
At a command prompt, in the src directory containing the HelloWorld.java file, the code was compiled using the command:
javac HelloWorld.java
This created a class file HelloWorld.class. The program was run with the command:
java HelloWorld
And the output was:
Hello, Java World!
Hello, SCons World!
Now we want to build our Hello, Java World! program using SCons. To use SCons requires a Python installation. Whether or not Python is installed on a system can be checked by opening a command prompt or shell and typing:
python --version
This will respond with either the Python version or a command not found style error. If Python is not installed already, then it can be downloaded and installed from the Python website.
A SCons installation can be checked with:
scons --version
Again, this will either return a SCons version number (and some other information) or a command not found style error message. If SCons is not installed, then it can be downloaded and installed from the SCons website. The version of SCons installed for this blog is 2.0.1.
Now we’re ready to build the HelloWorld.java code using SCons. A small change was made to the code to change the message output to refer to SCons rather than Java, but this was not really necessary
:
/*
* Java version of Hello, World!
*/
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello, Java World!");
System.out.println("Hello, SCons World!");
}
}
Also, the HelloWorld.class file created earlier in the src directory was deleted. Then, in the directory above src (the scons_test directory) a new text file named SConstruct was created and edited in a text editor. It only contains a single line:
Java('build','src')
A command prompt was opened in the directory containing the SConstruct file, and SCons was run with the command:
scons
The output should have been something like:
scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... javac -d build -sourcepath src src/HelloWorld.java scons: done building targets.
However, running it on a SuSE Linux based system, on the first run our output was more like:
scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... javac -d build -sourcepath src src/HelloWorld.java javac: unrecognized option '-sourcepath' src: file not recognized: Is a directory collect2: ld returned 1 exit status scons: *** [build/HelloWorld.class] Error 1 scons: building terminated because of errors.
After a little investigation, the program javac turned out to be calling the Gnu program gcj instead of the JDK version of javac. To fix this gcj was uninstalled, and the JDK installed properly. After this was done the SCons run worked as above, and the HelloWorld.class file was generated in the automatically created build directory.
The newly compiled program was run by changing into the build directory and typing:
java HelloWorld
It could also be run by staying in the directory containing the SConstruct file and typing:
java -classpath build HelloWorld
Both will output:
Hello, SCons World!
Note that the directory containing the class has to be set using the -classpath option. Trying to just specify the directory as follows:
java build/HelloWorld
will not work, and will result in a long error something like:
Exception in thread "main" java.lang.NoClassDefFoundError:
build/HelloWorld (wrong name: HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.securoty.SecureClassLoader.defineClass(
SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(
URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(
Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: build/HelloWorld. Program will exit
Summary
This post describes building Java with SCons. It shows a simple Hello, World! style example compiled both manually, and with a SConstruct file, and shows how to run the resulting class.
None of this code has been committed to the MereIdea Testing Repository, as it was only a simple example. The next post, however, will create Java code that is the equivalent to the C++ example code and will include a couple of packages, archived into jar files, and some tests created using JUnit, which will be stored in the repository for readers to check out.
Frequent updates on Twitter
Leave a Reply
You must be logged in to post a comment.