Getting Started with Oxygene for Java

Introduction

Read this guide before using our tutorial. You can use Oxygene for Java to obtain applets (for running in browsers), Java applications and Android apps. This allows you to maximise the use of your Pascal code.

Get a 30 day full functioning license of Oxygene for Java via remobjects.com/downloads to get you started. Student rates are under discussion. The command-line version is free. Documentation (for developers) is available, and we are helping to provide tutorials and demonstrations for school students.

Brian Long provides excellent instructions and demonstrations and those of you with Android devices can download his sample apps. There is now an ADT Bundle for Windows that is straightforward to use. Download it from the page that Brian links to. Later you can supply Oxygene with the path of the ADT folder on request.

Our checklist of necessary software for complete functionality is:
  • Java SDK (JDK)
  • Eclipse (supplied in the ADT Bundle)
  • Android SDK (supplied in the ADT Bundle)
  • Android plug-in for Eclipse. (supplied in the ADT Bundle)
  • Visual Studio IDE (for Oxygene; can be installed as part of Oxygene for Java)
  • Oxygene for Java

(If this list seems too daunting at first, you can download just the JDK and command-line compiler and write code for applets or Java applications. We wrote most demonstration programs in Notepad++ or in the Lazarus editor for the command-line compiler.)

Any of you still using Windows Vista can learn from our experience. Visual Studio 2012 will not run in Vista, so we downloaded the Visual Studio 2010 Shell with the following details:

Version 4.5.50709 RTMRel
Security Update for Visual Studio 2010 Shell (Integrated) - ENU (KB2251489)   KB2251489
Security Update for Visual Studio 2010 Shell (Integrated) - ENU (KB2644980)   KB2644980
This security update is for Visual Studio 2010 Shell (Integrated) - ENU.

We then installed Oxygene for Java, which worked well in Visual Studio 2010.

In March 2013 our installation on Windows 7 from the downloaded .iso image burned to a CD-ROM proceeded smoothly. In order to make Android apps work, we followed advice to set a Java option as follows.

Go to Start->Control Panel->System->Advanced->Environment Variables->System Variables->New:
Variable name: _JAVA_OPTIONS
Variable value: -Xmx512M

This came into effect after a reboot.

After a standard installation, the folder C:\Program Files\RemObjects Software\Oxygene contains a hotlink to folders of samples. These provide a great start to developing your own programs or porting existing Pascal code. There are sections for Android applications, applets, command line programs, visual programming (with buttons etc.) and language features.

Examples

You can start a new TINY program in the Visual Studio IDE by selecting menu option FILE > New > Project > Tiny Project    Oxygene for Java. Name it tiny_hello_world and edit the code to give the following program.
namespace tiny_hello_world;

interface

uses
  java.util;

implementation

begin
  System.out.println('Hello World!');
  System.in.read;
end.

With Oxygene 6 you can use Pascal's writeln instead of System.out.println. Usually Oxygene programs contain at least one class. The Oxygene for Java code for a Hello World console program with a Main class follows.

namespace hello_world;

interface

type
  ConsoleApp = class
  public
    class method Main(args : array of String);
  end;

implementation

class method ConsoleApp.Main(args : array of String);
begin
  System.out.println('Hello World!');  
  System.in.read;
end;

end.  

Oxygene is based on classes so you should already be familiar with the material in Object-Oriented Pascal. The Main method is required and must have an array of string parameter (to be available when supplying command line arguments on start-up). Notice that the code begins with the keyword "namespace" instead of "program". Each code file can only contain types from one namespace (which you supply in lower-case letters at the top of each code file). Conventionally, only one class is defined per code file.

The trimmed-down xml project file hello_world.oxygene is:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <ProductVersion>3.5</ProductVersion>
    <OutputType>Exe</OutputType>
    <Configuration Condition="'$(Configuration)' == ''">Release</Configuration>
    <Name>Hello World</Name>
    <RootNamespace>org.me.hello_world</RootNamespace>
    <AssemblyName>hello_world</AssemblyName>
    <StartupClass />
    <DefaultUses />
  </PropertyGroup>  
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <Optimize>true</Optimize>
    <OutputPath>.\bin\Release</OutputPath>
    <GenerateDebugInfo>False</GenerateDebugInfo>
    <GenerateMDB>False</GenerateMDB>
    <EnableAsserts>False</EnableAsserts>
    <TreatWarningsAsErrors>False</TreatWarningsAsErrors>
    <CaptureConsoleOutput>False</CaptureConsoleOutput>
    <StartMode>Project</StartMode>
    <RegisterForComInterop>False</RegisterForComInterop>
    <CpuType>anycpu</CpuType>
    <RuntimeVersion>v25</RuntimeVersion>
    <XmlDoc>False</XmlDoc>
    <XmlDocWarningLevel>WarningOnPublicMembers</XmlDocWarningLevel>
    <EnableUnmanagedDebugging>False</EnableUnmanagedDebugging>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="rt.jar" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="hello_world.pas">
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\RemObjects Software\Oxygene\RemObjects.Oxygene.Cooper.targets" />
</Project>
We put both files in a folder C:\Oxygene, then typed cd C:\Oxygene at the command prompt. We then typed the commands oxygene hello_world.oxygene and java -jar bin\Release\hello_world.jar:
Hello World

Hello World

RemObjects now favours the use of msbuild hello_world.oxygene instead of Oxygene hello_world.oxygene. If this command does not work, we suggest that you find the folder (such as C:\Windows\Microsoft.NET\Framework\v4.0.30319) containing msbuild.exe and add it to your list of paths (in the Environmental Variables dialog).

In Visual Studio, you obtain very useful content completion when coding and achieve compilation with a click on the green triangle. You can click on each reported error to be taken to the suspect line of source code. The following screenshots show an Oxygene program in the IDE before and during execution.

Oxygene program in the Visual Studio IDE

Oxygene program in the Visual Studio IDE

 

Oxygene program during execution

Oxygene program during execution

See our tutorial on Oxygene for Java for guidance, many demonstrations, and links to this and other student programs that we have converted to Oxygene.

AndroBOX in VirtualBox for fast Emulation

Tests with the inbuilt android emulator were painfully slow so we installed Android VMs in VirtualBox. We followed instructions on Chang's excellent page, but used some newer files as follows.

  • We installed VirtualBox 4.2.10 for Windows hosts x86/amd64 (accepting all defaults) then "VirtualBox 4.2.10 Oracle VM VirtualBox Extension Pack All supported platforms" from the same page.
  • We downloaded AndroBOX-2.3.r.1-generic.iso and mounted it on VirtualBox, setting 512 MB of RAM and a 2GB virtual hard disk.
  • Our network settings were the same as Chang's when we had access to a local network, but we selected the following when working on a standalone computer.

    Attached to: Host-only Adapter
    Name: VirtualBox Host-Only Ethernet Adapter

  • We found the IP address as per Chang with netcfg at the Android command prompt and connected without a problem with adb connect 192.168.0.5 for a computer on a home network or adb connect 192.168.56.101 on a standalone PC.
  • We then selected the IP from the CrossBox local menu options in Visual Studio:
    CrossBox local menu options

    CrossBox local menu options

    Once selected, the IP address and port (5555) show on the menu bar in Visual Studio:

    CrossBox value after selection

    CrossBox value after selection

  • Alternatively, we could install the apk file after compilation from the Windows command line with adb install demo.apk (or adb -r install demo.apk to replace a previously installed version).
  • We tested the VM with several sample apps from RemObjects including OpenGLApplication and were pleased by the performance.

Button Summary

  • Alt+F1 displays Android console;
  • Alt+F7 returns from console to Android GUI;
  • Right hand Ctrl key releases cursor from VM window;
  • Click in VM window then on Capture button makes mouse clicks work in this window;
  • Right mouse button quits app.

New Android Virtual Machine

Some of you may be looking for greater functionality in an Android VM. This page gives features of a new virtual machine and a link for download of its image file, android-x86-4.2-20130228.iso. We mounted it on VirtualBox as for AndroBOX (except this time selecting the default 8GB virtual hard drive). The sample OpenGLApplication ran impressively on the new VM.

Programming - a skill for life!

Getting Started, Links and other resources