Showing posts with label Java 8. Show all posts
Showing posts with label Java 8. Show all posts

Thursday, September 10, 2015

Remote run Java on Raspberry Pi, host from NetBeans on Windows 10

This video show how to develop Java application with NetBeans IDE 8.0.2 running on Windows 10, create Remote Java SE platform, remote run/debug on Raspberry Pi/Raspbian Wheezy .




The following java code list system by calling System.getProperties().
package javaapplication14;

import java.util.Enumeration;
import java.util.Properties;

public class JavaApplication14 {

    public static void main(String[] args) {

        System.out.println("\nSystem Properties\n");
        System.out.println("=================\n");
        
        Properties properties = System.getProperties();
        System.out.println(properties.toString());
        System.out.println("\n");

        Enumeration<String> prop
                = (Enumeration<String>) properties.propertyNames();

        while (prop.hasMoreElements()) {
            String propName = prop.nextElement();
            System.out.println(
                    propName + " : "
                    + System.getProperty(propName));
        }
    }

}


How it run on Windows 10/Intel i5 vs Raspbian/Raspberry Pi 2.




Updated@2015-10-04: Appliable on Raspbian Jessie.

Saturday, April 11, 2015

Java 8 example: call between Java and Javascript

This example show how to call Javascript function from Java, and call Java method from Javascript.


JavaTryJavaScript.java
package javatryjavascript;

import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JavaTryJavaScript {
    
    final static String myJavascript = "/home/pi/testJS/newjavascript.js";

    public static void main(String[] args) 
            throws FileNotFoundException, ScriptException, NoSuchMethodException {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine nashornEngine = scriptEngineManager.getEngineByName("nashorn");
        
        FileReader fileReader = new FileReader(myJavascript);
        nashornEngine.eval(fileReader);
        
        Invocable invocable = (Invocable)nashornEngine;
        invocable.invokeFunction("testJavaScript1", "Hello from Java");
        
    }
    
    public static void JavaCalledFromJS(String s){
        System.out.println("Java method called from JavaScript: " + s);
    }
    
}

The Javascript in a separated file, /home/pi/testJS/newjavascript.js
var testJavaScript1 = function(a){
    print('testJavaScript1, called from Java: ' + a);
    testJavaScript2();
    return;
}

var testJavaScript2 = function(){
    print('testJavaScript2');
    var javaClass = Java.type("javatryjavascript.JavaTryJavaScript");
    javaClass.JavaCalledFromJS("message from JavaScript");
    return;
}

Monday, March 23, 2015

Set JAVA_HOME for Raspbian pre-loaded java 1.8.0

Current Raspbian (version 2015-02-16) preload with java 1.8.0. By default, both java and javac are in PATH (that's why we can run java and javac directly), but no JAVA_HOME set.

This video show how to verify java location, set JAVA_HOME in .bashrc, and verify using jEdit.


- By default, java should be in /usr

- Edit hidden file .bashrc with command:
$ nano .bashrc

- Add the line:
export JAVA_HOME=/usr

- Save and reboot.


Thursday, November 27, 2014

Code Java on the Raspberry Pi, BlueJ on the Raspberry Pi

BlueJ, start from version 3.14, fully supports the Raspberry Pi. BlueJ is a Java development environment that allows development as well as program execution on the Pi.

BlueJ on the Raspberry Pi provides full access to hardware attached to the Raspberry Pi via the open source Pi4J library, from the the familiar Java SE language, including the new Java 8.

link:
http://bluej.org/raspberrypi/index.html



Current issue of Java Magazine, november/december 2014, have a charter of "Code Java on Raspberry Pi" introduce how BlueJ brings Java SE 8 development directly to the Raspberry Pi.



Tuesday, September 16, 2014

Sunday, August 3, 2014

Run JavaFX application on Raspberry Pi remotely from Netbeans 8

With Remote Platform set up on Netbeans 8, we can also develop JavaFX application on host computer running Netbeans, run the application remotely on Raspberry Pi. In case of Java FX application running on Raspberry Pi, it will display on Raspberry Pi monitor, instead of console.

Set up Java SE 8 Remote Platform on Netbeans 8 for Raspberry Pi

With Netbeans 8 installed on host computer and Java SE 8 on Raspberry Pi, we can set up remote platform on Netbeans 8. Such that we can develop Java application using Netbeans on host and deploy on Raspberry Pi remotely, all within Netbeans IDE.

It's assumed Java SE 8 have been installed on your Raspberry Pi as in last post "Install JDK 8 on Raspberry Pi and set JAVA_HOME and PATH".

In host PC (Ubuntu in my case) start Netbeans 8.

Click Tools -> Java Platforms

Click Add Platform...


Select Remote Java Standard Edition and click Next >


Name your new Remote Platform, and enter other information:
- 192.168.1.104 is the IP address of your remote Raspberry Pi.
- pi, the user in Raspberry Pi, and also enter password.
- /opt/jdk1.8.0_06 in Remote JRE Path is the JDK 8 installed location on Raspberry Pi, in last post.

(Updated@2014-09-17) for New Raspbian (2014-09-09) come with Java 8, set Remote JRE Path to default location /usr. ~ Setup Netbeans Remote Platform for default Java 8 in new Raspbian

Then finish.



This video show the steps:


Deploy Java application on Raspberry Pi remotely from Netbeans 8

Create Java Application in Netbeans as normal case. To Run it using the Remote Platform (created in previous steps):

Right click on Project -> Propertiers.

Select Run tab. Select the new created Remote Platform in Runtime Platform, and name the new Configuration.


Then you can run the application on remote Raspberry Pi by running it in this new configuration.



Once Remote Platform setup in your Netbeans IDE, you can also
development JavaFX application on Nerbeans 8, and run it remotely on Raspberry Pi
- remote debug Java program on Raspberry Pi
Cross compile Java/Swing program for Raspberry Pi



Friday, August 1, 2014

Java 8 and Embedded JavaFX

Presentation on using the new embedded features in Java 8, including JavaFX on devices like the Raspberry Pi and Lego Mindstorms EV3. Also covers JavaFX on tablets and smartphones.