- PVSM.RU - https://www.pvsm.ru -
Hello dear readers. In this article I will try to demonstrate how to run a Java Virtual Machine [1] and Java applications on microcontrollers. This idea may sound quite outlandish by itself: why use Java on microcontrollers where each byte of RAM and each CPU cycle are precious commodity? There's nothing like native C for microcontrollers — even C++ is rarely used! Yes, I've heard that discussion (and took part in it) for many years. Well, I will try to explain «why», as well as «how», in this article. So, anyone who wants to understand how MCU implementation of uJVM [2] is ready to take off! People who are ready to criticize can stay near the runway and watch us climb (or crash) from a safe distance
As a starting point, please watch the video below.
As far as I understand, this video describes something that consists of OS, alternatives [3]. I've chosen uJVM [2] — source code is available, build instructions are good, and anyone who wants to participate in development is welcome.
In this article I will describe overall architecture, byte code loading process, environment setup and getting first results: compiled uJVM [2] on various microcontroller boards, port uJVM to new platforms, add new Java classes and native classes. So let's start.
uJVM [2] is designed in accordance to overall architecture requirements described in The Java Virtual Machine Specification Java SE 7 Edition [4] and consists of 3 parts:
As of now uJVM [2] is available for the following H/W platforms:
uJVM [2] can be ported as an application for a variety of RTOSes, for example:
— however, we will not discuss use of uJVM [2] under RTOSes in this article.
uJVM [2] is an open source implementation of uJVM [2] at a build time, disabling unneeded features to conserve resources or enabling support of specific H/W devices (for example, FPU support to accelerate calculations). Let's see how to build and run it…
To build uJVM [2] from source code, I use Ubuntu 16.04, but there are no strict Linux version requirements. Packages listed below should be installed:
$ sudo apt-get install git gcc make openjdk-8-jdk-headless gperf flex bison libncurses5-dev texinfo g++ curl pkg-config autoconf libtool libtool-bin libc6:i386 libc6-dev:i386 gcc-multilib doxygen doxygen-gui
JAVA_HOME
variable)Please note that Java compiler (javac) directory must be known to build system. To achieve that, you need (according to Oracle Java documentation [8]) set the value of environment variable specifying the compiler location. A simple way to set this variable is to use shell commands:
$ export JAVA_HOME = _java_compiler_directory_
Here _java_compiler_directory_
must not include last bin
directory in the path; e.g., if compiler is located in /usr/bin
directory, JAVA_HOME
value must be /usr
. If you want to assign JAVA_HOME
value permanently for every user in the system, you can add this variable into /etc/environment
file by using the following command:
$ echo "JAVA_HOME="/usr"" | sudo tee -a /etc/environment
Same effect can be achieved by adding JAVA_HOME
variable into /etc/profile
file by using the following command:
$ echo "export JAVA_HOME="/usr"" | sudo tee -a /etc/profile
After executing either of these commands, you need to log out of shell and log in again.
kconfig-frontends
for Linux (optional)
Building kconfig-frontends
for Linux is a simple and straightforward process using autotools
. Usually you just need to download the source archive, unpack it and start build:
$ ./configure && make && sudo make install
$ curl -O http://ymorin.is-a-geek.org/download/kconfig-frontends/kconfig-frontends-3.12.0.0.tar.xz
$ tar -xf kconfig-frontends-3.12.0.0.tar.xz
$ cd kconfig-frontends-3.12.0.0
If your system uses gperf 3.0.4
or earlier, just go to part 3.3. Otherwise, read on.
gperf 3.1
(released on January 5 2017) changed the type used for specifying data length in generated functions — instead of unsigned int
, it started to use size_t
. This causes the build to fail with a following message:
CC libkconfig_parser_la-yconf.lo
In file included from yconf.c:234:0:
hconf.gperf:141:1: error: conflicting types for 'kconf_id_lookup'
hconf.gperf:12:31: note: previous declaration of 'kconf_id_lookup' was here
static const struct kconf_id *kconf_id_lookup(register const char *str, register unsigned int len);
^~~~~~~~~~~~~~~
make[3]: *** [Makefile:456: libkconfig_parser_la-yconf.lo] Error 1
make[2]: *** [Makefile:350: all] Error 2
make[1]: *** [Makefile:334: all-recursive] Error 1
make: *** [Makefile:385: all-recursive] Error 1
To fix this issue, execute the following commands:
$ curl -O https://gist.githubusercontent.com/KamilSzczygiel/d16a5d88075939578f7bd8fadd0907aa/raw/1928495cfb6a6141365d545a23d66203222d28c0/kconfig-frontends.patch
$ patch -p1 -i kconfig-frontends.patch
$ autoreconf -fi
Recommended configuration for kconfig-frontends
is:
$ ./configure --enable-conf --enable-mconf --disable-shared --enable-static
$ make
$ sudo make install
$ sudo strip /usr/local/bin/kconfig-*
$ git clone https://github.com/Samsung/uJVM.git
Select PLATFORM x86_64_linux
. After that, create default build context by executing following command:
$ cd uJVM/
$ make PLATFORM=x86_64_linux create_context
Include HelloWorld.java into build. To do that, open configuration util:
$ make menuconfig
Go to Java application Configuration menu
and select «Hello world example»
This will include HelloWorld.java
into build sequence:
/**
* @file japps/hello_world/HelloWorld.java
* @brief Example how to use String and SysLog classes
*
* @copyright Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved.
* @author Taras Drozdovskyi t.drozdovsky@samsung.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import ujvm.lang.*;
public class HelloWorld {
public static void main() {
SysLog.log("Hello world!n");
String hello = "Hello world!n";
SysLog.log(hello);
byte[] bytes = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', 'n' };
String str = new String(bytes);
SysLog.log(str);
}
}
Don't let the SysLog.log(str)
confuse you — it's simply a low-level analog of System.out.println(str)
. Standard Java library is not fully implemented in uJVM [2]yet, but I hope someone will do that someday.
Save and compile:
$ make
And, finally, run the application:
$ make run
Other applications listed in build system can be built and run in the same fashion.
Next article will cover building and running uJVM [2] on microcontroller boards. However, if you're impatient to try, just read the documentation — it covers the usual sequence of operations pretty well.
Links:
The Journal of Open Source Software — uJVM: Lightweight Java Virtual Machine for embedded systems; [9]
uJVM Documentation. [10]
Автор: TarasDrozdovsky
Источник [11]
Сайт-источник PVSM.RU: https://www.pvsm.ru
Путь до страницы источника: https://www.pvsm.ru/java/323413
Ссылки в тексте:
[1] Java Virtual Machine: https://en.wikipedia.org/wiki/Java_virtual_machine
[2] uJVM: http://github.com/samsung/uJVM
[3] alternatives: https://en.wikipedia.org/wiki/List_of_Java_virtual_machines
[4] The Java Virtual Machine Specification Java SE 7 Edition: https://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf
[5] ZephyrOS: https://www.zephyrproject.org/
[6] FreeRTOS: https://www.freertos.org/
[7] Nuttx: https://nuttx.org/
[8] Oracle Java documentation: https://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/)
[9] The Journal of Open Source Software — uJVM: Lightweight Java Virtual Machine for embedded systems;: https://joss.theoj.org/papers/23161710ad8996910f329efc48c2f1a1
[10] uJVM Documentation.: https://github.com/Samsung/uJVM/tree/master/docs
[11] Источник: https://habr.com/ru/post/459358/?utm_campaign=459358&utm_source=habrahabr&utm_medium=rss
Нажмите здесь для печати.