Start Programming JAVA for the RPi.


18 posts
by 2ko » Thu Nov 22, 2012 2:48 pm
I wanted to know if someone can provide a clear and understandable manual about starting programming on JAVA and building applications that would run on the RPi ?

I have already understood that the actual "coding" should be done on another machine and that only the execution will be over the RPi, but I haven't understood what should I do in order to make those programs and waht application should I install .

Thanks for your help.
Regards,
2ko.
Posts: 20
Joined: Thu Nov 22, 2012 9:55 am
Location: Israel
by xranby » Thu Nov 22, 2012 3:25 pm
2ko wrote:I wanted to know if someone can provide a clear and understandable manual about starting programming on JAVA and building applications that would run on the RPi ?

I have already understood that the actual "coding" should be done on another machine and that only the execution will be over the RPi, but I haven't understood what should I do in order to make those programs and waht application should I install .

Thanks for your help.
Regards,
2ko.


To get started with Java programming all you need is a text editor and a java compiler, both can be installed and run directly from the raspberry pi.

To install a java compiler run:
sudo apt-get update
sudo apt-get install openjdk-6-jdk

This will give you access to the javac compiler and the java runtime.

The most simple demo is to compile a HelloWorld.java application
Type in the following code in a text editor like LeafPad, pico, vi or a lightweight code editor like Geany and save it to HelloWorld.java
Code: Select all
class HelloWorld

        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}


compile it using javac
javac HelloWorld.java
this will produce a compiled HelloWorld.class
You can then run the class file using java
java HelloWorld
and it will output
HelloWorld! on the screen.

You can use this javac compiler to compile large projects like Blocky and run it using java directly on the Pi:
viewtopic.php?f=78&t=22341


If you prefer you can compile the application on a different machine and simply copy the compiled class file to the Pi and then run it using java.
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby
User avatar
Posts: 401
Joined: Sat Mar 03, 2012 10:02 pm
by 2ko » Thu Nov 22, 2012 4:00 pm
xranby , Thanks a lot for your reply.
Another question , If I develop on my Eclipse on my laptop (Windows ) , do I have to use some cross platform or cross compilation software ?

Moreover, what about this manual of Oracle http://www.oracle.com/technetwork/articles/java/raspberrypi-1704896.html#Java%20,
they talk about downloading a Java SE for embedded , and I bit confused about how it combines with your answer .
Thanks a lot for your help.
Posts: 20
Joined: Thu Nov 22, 2012 9:55 am
Location: Israel
by ghans » Thu Nov 22, 2012 6:13 pm
The point of Java was platform-independence , if i remember correctly :D
No cross-compilers needed. Just make sure that you use
one openjdk version on your lappy , and exactly the same on the Pi.

Oracle supports a official proprietary JDK for Linux - your article.
They also support a FLOSS version called OpenJDK.
We a referring to latter (popular on Linux ) - OpenJDK.

Is it true that the next official Java JRE will be the OpenJDK JRE ,
and the proprietary one will be dropped , xranby ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 3910
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by xranby » Fri Nov 23, 2012 10:16 am
2ko wrote:xranby , Thanks a lot for your reply.
Another question , If I develop on my Eclipse on my laptop (Windows ) , do I have to use some cross platform or cross compilation software ?

Pure Java applications compiled to .class files or .jar files that in turn only contains .class files do not require a cross compiler, you can compile these applications on your laptop and then copy the generated .cass or .jar files to the Pi and run them.

If your Java application call platform dependent system libraries using Java Native Interface, JNI, then you will have to use a cross compiler to compile the JNI code.
http://en.wikipedia.org/wiki/Java_Native_Interface
For many purposes you can use Java Native Access, JNA, instead of JNI and thus only need to write Pure Java code that can access platform dependent system libraries on both your laptop and the Pi without using a cross-compiler.
http://en.wikipedia.org/wiki/Java_Native_Access

In short, if you use Eclipse to write and compile C/C++/Assembly applications then you will need a cross-compiler to make these applications run on the Pi.

2ko wrote:Moreover, what about this manual of Oracle http://www.oracle.com/technetwork/articles/java/raspberrypi-1704896.html#Java%20,
they talk about downloading a Java SE for embedded , and I bit confused about how it combines with your answer .

My answer and the Oracle tech-network article is two separate pieces of work:
My answer above describe how you use the OpenJDK packages provided by Rasbian and Debian community, for use on your Pi.
Bill Courington, The Principal Technical Writer at Oracle, and Gary Collins, Software engineer for Java Embedded & Realtime at Oracle, have written an introduction article for the Oracle tech-network on how to setup and use the Oracle "Java SE for embedded" product.

The license terms of the OpenJDK packages and the Oracle Java SE for embedded is quite different:

OpenJDK is community supported and is released under the GPLv2+classpath exception license:
http://openjdk.java.net/legal/gplv2+ce.html

Oracle Java SE for embedded is released under the OTN License Agreement:
http://www.oracle.com/technetwork/licenses/distribution-license-152002.html

ghans wrote:Is it true that the next official Java JRE will be the OpenJDK JRE ,
and the proprietary one will be dropped , xranby ?

Ask Oracle. Please post a link if Oracle have made this promise. I do not think the proprietary one will be dropped.

OpenJDK 7 is already the reference implementation for JDK 7.
https://blogs.oracle.com/henrik/entry/moving_to_openjdk_as_the
http://jdk7.java.net/java-se-7-ri/
http://openjdk.java.net/

When Oracle create their JDK and JRE releases they take the OpenJDK source-code and combine it with some extra proprietary closed source code like their client JIT for ARM and the Oracle java browser plug-in code on some platforms. The compiled code is then tested and released by Oracle under the Oracle Binary Code License Agreement for Java SE:
http://www.oracle.com/technetwork/java/javase/terms/license/index.html
http://www.oracle.com/technetwork/java/javase/downloads/index.html

When Debian/Rasbian create their OpenJDK packages they take the OpenJDK source-code and combine it with some extra free software parts from the IcedTea project like the ARM assembler interpreter ,a Thumb2 JIT and a free software browser plugin, icedtea-plugin, from the IcedTea-web project. The compiled code is then tested and released under the same license as used by the OpenJDK sourcecode the license of the code used for the extra IcedTea free software parts.
http://icedtea.classpath.org/wiki/Main_Page
http://icedtea.classpath.org/wiki/IcedTea-Web
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby
User avatar
Posts: 401
Joined: Sat Mar 03, 2012 10:02 pm
by ghans » Fri Nov 23, 2012 12:05 pm
Thanks very much for the detailed answer , xranby :D


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 3910
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by 2ko » Sun Nov 25, 2012 10:07 am
OK, thanks a lot guys for your help.

I have managed to create a simple java program on my Windows 7 Laptop and run it on the
Raspberry ( I have made simply program which opens socket for communication via port 80 ) .

The only obstacle that I see is that I can't use external packages in Java (External JAR files) because I have no way of adding them on the RPi .
Posts: 20
Joined: Thu Nov 22, 2012 9:55 am
Location: Israel
by ghans » Sun Nov 25, 2012 12:31 pm
I don't understand your problem. Can't you just add the JARs to the Classpath while launchning your app ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 3910
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by 2ko » Thu Nov 29, 2012 7:15 am
Hi ghans ,
You mean to add the JARs to the classpath in the RPi ? or while i program in the Eclipse ?
Of course I add the JARs while I am building my code inside Eclipse.

The problem is to run the Runnable JAR file in the RPI .
If you mean adding the JARs in the RPi then I will be happy to hear how exactly I do that.

Regards,
2ko .
Posts: 20
Joined: Thu Nov 22, 2012 9:55 am
Location: Israel
by xranby » Thu Nov 29, 2012 9:06 am
2ko wrote:You mean to add the JARs to the classpath in the RPi ?
...
The problem is to run the Runnable JAR file in the RPI .
If you mean adding the JARs in the RPi then I will be happy to hear how exactly I do that.


You have three options:

A) If you want to start your application using
java -jar yourApp.jar
and have it find extra classes in
lib/extension.jar
then you have to specify
Class-Path: lib/extension.jar
inside yourApp.jar Manifest.txt file.
http://en.wikipedia.org/wiki/Classpath_%28Java%29#Setting_the_path_in_a_Manifest_file
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

B) You can always run your application by specifying all jars using -classpath and your MainClass.
java -classpath yourApp.jar:lib/extension.jar MainClass

C) Use javaws JNLP deployment
JNLP deployment is currently only supported by OpenJDK and IcedTea-web on the Raspberry Pi.
You get javaws by installing the icedtea-plugin package.
javaws http://example.com/yourApp/yourApp.jnlp
http://docs.oracle.com/javase/7/docs/te ... es/javaws/
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby
User avatar
Posts: 401
Joined: Sat Mar 03, 2012 10:02 pm
by 2ko » Thu Nov 29, 2012 11:10 am
Thanks for your advice xranby ,
I will certainly try it .
Does anybody knows if RPi is capable of running JAVA GUI applications ,
those containig (javax.swing) and built with Window Builder ?
Posts: 20
Joined: Thu Nov 22, 2012 9:55 am
Location: Israel
by xranby » Thu Nov 29, 2012 11:20 am
2ko wrote:Thanks for your advice xranby ,
I will certainly try it .
Does anybody knows if RPi is capable of running JAVA GUI applications ,
those containig (javax.swing) and built with Window Builder ?


OpenJDK and Oracle JDK is able to run AWT and Swing applications on the Pi.
It is possible to get around 20-28fps using pure AWT if you target the composite out on the Pi (or run in a 640x480 pixel window mode).
viewtopic.php?p=178262#p178262

Large Swing GUI applications, such as Netbeans, is known to run sluggish.

The Oracle Java SE for embedded product is not able to run AWT and Swing since it is a "headless" JRE with removed GUI support.

For hardware accelerated fullscreen hdmi 60fps Java applications then you will have to use OpenGL ES 1 or 2 using
JogAmp JOGL 2
viewtopic.php?p=157222
or
LWJGL
viewtopic.php?p=211087
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby
User avatar
Posts: 401
Joined: Sat Mar 03, 2012 10:02 pm
by khooke » Thu Nov 29, 2012 8:53 pm
At JavaOne this year there were multiple Java Embedded sessions and demos in the keynotes showing Java Embedded SE on small devices like the Raspberry Pi. Some of these demos included rich UIs built using JavaFX.

I don't know which Java SE Embedded version comes with or supports JavaFX - I did some reading around and couldn't find it mentioned - the docs are here if you're interested:

http://www.oracle.com/technetwork/java/ ... index.html

The common message though was that JavaFX is being positioned as the replacement API/technology for Swing since it can be supported cross platform on multiple hardware devices, including embedded.
Posts: 19
Joined: Sun Oct 07, 2012 6:38 am
Location: CA, USA
by xranby » Fri Nov 30, 2012 10:03 am
khooke wrote:At JavaOne this year there were multiple Java Embedded sessions and demos in the keynotes showing Java Embedded SE on small devices like the Raspberry Pi. Some of these demos included rich UIs built using JavaFX.

At JavaOne Oracle demonstrated viewtopic.php?f=63&t=19040 :
java version "1.8.0-ea"
Java(TM) SE Embedded Runtime Environment (build 1.8.0-ea-b00, headless)
Java HotSpot(TM) Embedded Client VM (build 24.0-b11, mixed mode)
This build is closed source and not public available.


khooke wrote:I don't know which Java SE Embedded version comes with or supports JavaFX - I did some reading around and couldn't find it mentioned.

Oracle have only released a JavaFX Developer Preview for ARM v7 :
http://jdk7.java.net/fxarmpreview/javaf ... eview.html
This build contain two hardware accelerated back-ends, one is hard linked to the Texas instruments OMAP Imaginative Technologies OpenGL ES drivers and one is hard linked to the Nvidia Tegra OpenGL ES drivers. It is not possible to use this build on the Raspberry Pi.

khooke wrote:The common message though was that JavaFX is being positioned as the replacement API/technology for Swing since it can be supported cross platform on multiple hardware devices, including embedded.


JavaFX 1.2 and 1.3 back in 2008-2009 was already working on embedded devices, it used JOGL and was cross platform on multiple platforms including embedded and mobile.

The key people like Ken Russell and Sven Gothel behind the hardware acceleration logic provided by JOGL left Sun before Oracle took over in 2009
http://labb.zafena.se/?p=656

JOGL is now community maintained by the JogAmp community.
http://www.jogamp.org

The new JavaFX 2.0 provided by Oracle now uses a closed source implementation for its hardware acceleration and is only available on a limited number of platforms that Oracle choose to support.
http://www.oracle.com/technetwork/java/ ... 06746.html
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby
User avatar
Posts: 401
Joined: Sat Mar 03, 2012 10:02 pm
by 2ko » Sun Dec 02, 2012 10:42 am
I have managed to run the application but it always crashes at the following line :

connection = DriverManager.getConnection("jdbc:sqlite:/home/pi/Desktop/links.db");

I am trying to create a db to which I will insert internet links that my crawler program finds.
It seems that this line causes the application to crash , I simply get "killed" message on the console and that's it , the GUI window of the application closes and it exits ...

Someone knows what might cause this crash ?
I am pretty sure that compiling process is successful because I can see the GUI window of the application.


To make it more clearer I have two JARs :
Crawler.jar --> mine program
sqlite-jdbc-3.7.2.jar --> the jar file of the jdbc connection .

I put those two files on my Desktop and run them with :
java -jar Crawler6.jar

At this stage the GUI window of my app opens and crashes after few seconds at the line I have wrote earlier .

Appreciate your help.
Posts: 20
Joined: Thu Nov 22, 2012 9:55 am
Location: Israel
by 2ko » Sun Dec 02, 2012 12:35 pm
OK , now its working .
It appears that I must create a .db file to which the data will be inserted (touch links.db) .
Unlike the situation in WIN7 where this file is created automatically.
Posts: 20
Joined: Thu Nov 22, 2012 9:55 am
Location: Israel
by dms » Sun Dec 02, 2012 10:15 pm
Hi,
I have just finished writing a Java JNI that enables Java programs to access the OpenGL ES on the
RP. The libraries and example programs are available on SourceForge and documentation and
installation instructions can be found at http://www.spanglefish.com/dmsconsulting/
I hope you find this useful.
Don
Posts: 12
Joined: Sun Jun 24, 2012 11:31 am
by khooke » Mon Dec 03, 2012 10:26 pm
Xranby - thanks for the links and info. So the current JavaFX ARM preview is currently only targeted to specific ARM and graphics chipsets, but from your understanding is Java SE Embedded 8 with Java FX 8 expected to run on more than one embedded hardware platform? The roadmap seems to imply so:

"JavaFX 8 will be released as part of Oracle’s JDK 8 implementation and will be the default UI toolkit for Java SE 8 Embedded, providing a consistent programming environment for embedded system applications and desktop applications alike"

http://www.oracle.com/us/corporate/press/1854982
Posts: 19
Joined: Sun Oct 07, 2012 6:38 am
Location: CA, USA