How to use Java to write a NEO smart contract

Smart contracts are written in high-level languages such as Java, C#, Python, Kotlin (and more...) and compiled into AVM (Neo's Virtual Machine bytecode) so that they can run on the Neo network.

We currently recommend C# for developing smart contracts. The Java compiler is still in development but the current version (neoj) can handle basic methods.

This section contains a tutorial that guides you in configuring the Java development environment for NEO smart contracts. It also gives you an idea of how to create a smart contract project and how to compile it.

The process involves the following steps:

Write Java code (.java) for classes that extend FunctionCode or VerificationCode which is part of the Neo Framework Library (JAR)

Use the normal Java compiler to compile code into Java bytecode (.class)

Detailed Instructions

Tools

The most efficient way of getting these steps done is to download and compile all the tools you will need:

Download Neo's Node GUI. At the time of writing, it is recommended you use the BETA developer GUI as it has some extra debugging features which are helpful. CoZ NEO GUI. It will have default presets to Testnet and you will have to wait (up to a few hours) for it to fully sync up.

The installation process is very simple, follow the operation prompts step-by-step, it should be noted that you need to check the installation of .NET Core cross-platform development, otherwise you will not be able to open neo-vm project in step #3. The installation takes about ten minutes or up to an hour.

neo-compiler

Installation and configuration steps:

Windows:
Download the neo-compiler project on Github, open the solution with Visual Studio 2017, and publish the neoj project.

NOTE: At this point, after having launched the build command, you could get the following error:
It was not possible to find any compatible framework version
The specified framework 'Microsoft.NETCore.App', version '1.0.4' was not found.

If this is the case, open neoj.csproj and change the RuntimeFrameworkVersion's tag with your dotnet version.
For example <RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>

We now need to add this directory to our execution path. The PATH is the system variable that your operating system uses to locate needed executables from the command line or Terminal window.

Windows 10 and Windows 8:

In Search, search for and then select: System (Control Panel)
Click the Advanced system settings link.
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

Windows 7:

From the desktop, right click the Computer icon.
Choose Properties from the context menu.
Click the Advanced system settings link.
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

Now run Command or PowerShell, and enter neoj.exe. If there is no error and the output shows the version number (as shown) the environment variable configuration is successful

NOTE. Windows 7 SP1 users might encounter an error "Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'api-ms-win-core-console-l2-1-0.dll': The specified module could not be found". The required 'api-ms-win-core-console-l2-1-0.dll' file is only found in Windows 8 or later versions. This error can be resolved by obtaining a copy of 'api-ms-win-core-console-l2-1-0.dll' and putting it in the directory C:\Windows\System32. This dll can potentially be found in a number of places throughout one's system(search your computer and copy/past it into \System32), but alternatively can be found online.

Linux

Add this to your ~/.profile or ~/.bashrc file:

export PATH=$PATH:/path/to/neo-compiler

then execute source ~/.profile or source ~/.bashrc

Create project

After the above installation is complete you can create a Java project (e.g. using Eclipse or IntelliJ).

Compile the .jar package of smart contract from the neo java devpack project (neo-devpack-java) and add it as an external library.

Export the package of org.neo.smartcontract.framework as the .jar package org.neo.smartcontract.framework.jar and put it under the directory ..\neoj\bin\Release\netcoreapp1.1\win10-x64\publish\

Create a class, e.g. Go.java, and paste the following code into the class file.