manojsamal
Posts: 2
Joined: Tue Sep 08, 2015 7:04 am

Netbeans and pi4j importing

Tue Sep 08, 2015 7:21 am

pi4j import in netbeans isn't working.
Can anyone please provide a full process to run a simple code mentioned at http://pi4j.com/example/control.html for led blinking on NETBEANS remote platform .

Thanks in advance for the help.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Netbeans and pi4j importing

Tue Sep 08, 2015 2:20 pm

Prerequisite:
1.) Download and extract the Pi4J JAR files somewhere on your local system (http://pi4j.com/download.html)
2.) Download and install latest NetBeans IDE for Java Developers.

In NetBeans:
1.) File > New Project > Java > Java Application
2.) Enter a project name such as "Pi4JSample"
3.) Make sure the "Create Main Class" option is checked and use "ControlGpioExample" for the class name.
4.) Click "Finish" to create project template.
5.) In the project's tree, right-click the "Libraries" folder and select "Add Library"
6.) "Create" a new library using Library Name: "Pi4J-Core"
7.) Make sure the "Classpath" option is selected and then "Add JAR/Folder" - select the "pi4j-core.jar" file from your local file system (assuming you already have downloaded Pi4J on your computer)
8.) Select the "Sources" option and add the "pi4j-core-sources.jar".
9.) Select the "JavaDoc" option and add the "pi4j-core-javadoc.jar".
10.) Select the "OK" button to finish adding the Pi4J Library to NetBeans.
11.) You should now be back on the "Add Library" dialog. Make sure that "Pi4J-Core" is selected and click "Add Library" to add it to your project.
12.) Next, copy all the ControlGpioExample source code from https://raw.githubusercontent.com/Pi4J/ ... ample.java and paste it into your local ControlGpioExample.java source file replacing the existing source.
13.) Select the "Run" menu item and then "Build Project" sub menu item. This should compile the program and make sure there are no error. Look at the bottom of the IDE for the "Output" window and you should see a "BUILD SUCCESSFUL" message in the output text.

Running on the Pi from NetBeans (ref: https://blog.idrsolutions.com/2014/08/u ... pberry-pi/)
1.) In NetBeans, select the "Run" menu item and then "Set Project Configuration" sub menu item and click "Customize".
2.) Click "New" button to create a new configuration. Provide a name such as "Raspberry Pi" for the configuration.
3.) Next, click "Manage Platforms", then "Add Platform".
4.) Select "Remote Java Standard Edition" and click "Next" button.
5.) Provide a platform name such as "RPI-Platform"
6.) Provide the host name/address, username "pi" and password "raspberry".
7.) For the "Remote JRE Path"field, use:

Code: Select all

/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre
8.) Click "Finish" button.
9.) Now you should be at the "Java Platform Manager" dialog.
10.) Look for the "Exec Prefix" field under "Platform Properties". Edit this field and enter "sudo" (We need sudo to launch GPIO programs.)
11.) Click "Close" to close the "Java Platform Manager" dialog.
12.) Now back at the "Project Properties" dialog, select from the dropdown menu, Runtime Platform = "RPI-Platform". Click "OK" to close.
13.) From the NetBeans menu, select the "Run" menu item and then "Run Project" sub menu item.

You program will compile and then transfer the compiled program and libraries to your Raspberry Pi and then run the program on the Pi.

You should see some output similar to this:

Code: Select all

ant -f /Users/roberts/NetBeansProjects/Pi4J-Test -Dnb.internal.action.name=run -Dremote.platform.rp.target=linuxarmvfphflt-15 -Dremote.platform.password=***** -Dremote.platform.rp.filename=linuxarmvfphflt -Dremote.platform.java.spec.ver=18 run-remote
init:
Deleting: /Users/roberts/NetBeansProjects/Pi4J-Test/build/built-jar.properties
deps-jar:
Updating property file: /Users/roberts/NetBeansProjects/Pi4J-Test/build/built-jar.properties
compile:
Copying 1 file to /Users/roberts/NetBeansProjects/Pi4J-Test/build
Copy libraries to /Users/roberts/NetBeansProjects/Pi4J-Test/dist/lib.
To run this application from the command line without Ant, try:
java -jar "/Users/roberts/NetBeansProjects/Pi4J-Test/dist/Pi4J-Test.jar"
jar:
Connecting to 10.1.1.190:22
cmd : mkdir -p '/home/pi/NetBeansProjects//Pi4J-Test/dist'
Connecting to 10.1.1.190:22
done.
profile-rp-calibrate-passwd:
Connecting to 10.1.1.190:22
cmd : cd '/home/pi/NetBeansProjects//Pi4J-Test'; 'sudo' '/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java'  -Dfile.encoding=UTF-8   -jar /home/pi/NetBeansProjects//Pi4J-Test/dist/Pi4J-Test.jar 
<--Pi4J--> GPIO Control Example ... started.
--> GPIO state should be: ON
--> GPIO state should be: OFF
--> GPIO state should be: ON
--> GPIO state should be: OFF
--> GPIO state should be: ON for only 1 second
run-remote:
BUILD SUCCESSFUL (total time: 39 seconds)
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

manojsamal
Posts: 2
Joined: Tue Sep 08, 2015 7:04 am

Re: Netbeans and pi4j importing

Wed Sep 09, 2015 9:45 am

Thanks :D
Worked perfectly. was a silly mistake by me indeed.

gmseed
Posts: 35
Joined: Sat Aug 29, 2015 7:16 pm

Re: Netbeans and pi4j importing

Sat Sep 12, 2015 4:04 pm

Hi

Your instructions assume that NetBeans is running on a Linux machine.

I'm using NetBeans on a Win7 laptop and get the following exception when running:

Code: Select all

run:
<--Pi4J--> GPIO Control Example ... started.
Sep 12, 2015 4:54:32 PM com.pi4j.util.NativeLibraryLoader load
SEVERE: Unable to load [libpi4j.so] using path: [/lib/libpi4j.so]
java.lang.IllegalArgumentException: The path has to be absolute, but found: \lib\libpi4j.so
	at com.pi4j.util.NativeLibraryLoader.loadLibraryFromClasspath(NativeLibraryLoader.java:120)
...
To get this working on Windows would I require the dll equivalent of the .so library packed into the pi4j-core.jar?

Thanks
Graham

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Netbeans and pi4j importing

Sat Sep 12, 2015 4:24 pm

Hi Graham,

Are you attempting to run/execute your Java program on the host computer? (Windows)
If so .. well, then that won't work. Pi4J will only resolve it's runtime native library dependency when running directly on the Pi.
It really does not matter if the host computer is Windows, OSX, or Linux it will ultimately fail because the native library is only compiled for ARM devices and has specific RPi implementation code embedded.

You can run your Java program directly on the Pi hardware using Netbeans. Please check your remote system configuration and make sure that the RPi platform that you configured is selected for your project's run configuration. In the past I have noticed that Netbeans sometimes doesn't save my remote platform selection and I have to edit it again to get it working.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

gmseed
Posts: 35
Joined: Sat Aug 29, 2015 7:16 pm

Re: Netbeans and pi4j importing

Mon Sep 14, 2015 12:32 pm

The posted error was at your step 13, which I understand that at this step it is attempting to run on the host Windows machine but with Linux built jars specifically for the ARM processor.

From what you say though not sure it would have worked on any host if your steps are followed from 1 onwards. Unless I'm missing something or should be looking forward to the 2nd numbered block?

But ah...reading your step 13 instructions again I see it states "Run", when it really doesn't mean run but rather build. OK.

I'll give it another go, but I'm thinking about adding a Ubuntu dual boot to my laptop, which may make life easier!!

Graham

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Netbeans and pi4j importing

Mon Sep 14, 2015 12:43 pm

I guess I should mentioned that I am running Netbeans from OSX, so those instructions were based on my environment/installation :-)
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

gmseed
Posts: 35
Joined: Sat Aug 29, 2015 7:16 pm

Re: Netbeans and pi4j importing

Mon Sep 14, 2015 1:41 pm

Thanks for your reply.

Wow...works a treat!

Now, that's a lot easier than using BlueJ on the RPi unit itself.

Thanks again for your help.

Graham

gmseed
Posts: 35
Joined: Sat Aug 29, 2015 7:16 pm

Re: Netbeans and pi4j importing

Mon Sep 14, 2015 8:50 pm

Hi

I don't know if you've tried this, but I generated a Swing app with a JFrame main window.

Using the default host project configuration it runs fine.

Next, I follow your instructions and set up a remote project configuration. Using the "Test Configuration" it passes successfully.

However, when I run it I get the runtime exception:

Code: Select all

cmd : cd '/home/pi/NetBeansProjects//TriEV0'; 'sudo' '/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java'  -Dfile.encoding=UTF-8   -jar /home/pi/NetBeansProjects//TriEV0/dist/TriEV0.jar 
Exception in thread "AWT-EventQueue-0" 
java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
	at java.awt.Window.<init>(Window.java:536)
	at java.awt.Frame.<init>(Frame.java:420)
	at java.awt.Frame.<init>(Frame.java:385)
	at javax.swing.JFrame.<init>(JFrame.java:181)
	at gui.MainWindow.<init>(MainWindow.java:20)
...
so it seems to work for non-gui programs but has a problem with gui programs.

Have you seen this? I don't know if it's because I'm using the NetBeans .form files and I'll try an example without these but I don't think this should cause any issues since they write out Java code inside the UI classes.

I see in the "Java Platform Manager" dialog the variable "java.awt.graphicsenv" that is set to "sun.awt.X11GraphicsEnvironment".

I must add that I also find the runtime configurations a bit buggy as it keeps deleting my remote configuration. Even when I waste time resetting it up and then come to run it, it doesn't appear in the list. The only thing I can think of is that I have several configurations, some local and some remote. I've just been trying it now and it's almost unusable!

Graham

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Netbeans and pi4j importing

Mon Sep 14, 2015 8:58 pm

~ misinformation deleted ~ :oops:
Last edited by savageautomate on Tue Sep 15, 2015 12:27 pm, edited 1 time in total.
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

gmseed
Posts: 35
Joined: Sat Aug 29, 2015 7:16 pm

Re: Netbeans and pi4j importing

Tue Sep 15, 2015 9:18 am

I know I can develop a Swing app using Eclipse with WindowBuilderPro on my laptop, then move the files over to the RPi and build using BlueJ and the Swing app runs fine on the RPi. So, I'm confident this is possible.

Fidelius
Posts: 408
Joined: Wed Jan 01, 2014 8:40 pm
Location: Germania

Re: Netbeans and pi4j importing

Tue Sep 15, 2015 9:36 am

savageautomate wrote:~ community please correct me if I am wrong ~

I don't think the Oracle JVM includes any support for Swing on the Pi. JavaFX however does work.
Oh yes, Oracle's Java 8 included with Raspbian (and downloadable from Oracle), is a full J2SE JDK edition meaning it includes every Swing bit and uses X11 to display it. So we can run any Swing program on the Pi, even the fat Netbeans runs fine. (Unfortunately Oracle cut FX since some versions, but the original Raspbian Java 8 still has FX support.)

However, when using remote tunnels like Netbeans via SSH to remote-run programs on the Pi, this X11 sometimes makes problems and you've to set the correct X11 (or Netbeans?) environment variables. But I don't know yet what's missing with Gmseed's problem.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Netbeans and pi4j importing

Tue Sep 15, 2015 12:30 pm

I guess my comment was related to an earlier version of the JDK where only headless was supported. I removed my earlier post to avoid misinformation to future readers.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

gmseed
Posts: 35
Joined: Sat Aug 29, 2015 7:16 pm

Re: Netbeans and pi4j importing

Wed Sep 16, 2015 9:30 am

Just thought I'd add that if I switch to my RPi and run the remotely built jar by NetBeans in the .../dist directory then it runs fine:

[email protected] ~/NetBeansProjects/TriEV0/dist $ sudo /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java -jar TriEV0.jar

So, the app is ok, and runs on the RPi ok, and the problem lies in running the Swing app remotely.

Image

gmseed
Posts: 35
Joined: Sat Aug 29, 2015 7:16 pm

Re: Netbeans and pi4j importing

Thu Sep 17, 2015 9:57 am

One thing I tried was from the article:

https://wiki.utdallas.edu/wiki/display/ ... +and+PuTTY

and installed Xming.

I then started the Xming server before doing anything else.

I then started a Putty connection with my RPi's IP address and port 22 for ssh and in the Connection|SSH|X11 settings of Putty ensured "Enable X11 forwarding" was checked and set "X display location" to "127.0.0.1:0"

From the Putty session I can run X11 graphical programs on the Windows side; eg:

[email protected] ~ $ leafpad

but not sure how all this helps running a graphical Swing program remotely on the RPi and launched from NetBeans, as I still get the same X11 headless exception when running a Swing app.

Rooock
Posts: 5
Joined: Thu Mar 16, 2017 1:33 pm
Location: Colombia

Re: Netbeans and pi4j importing

Thu Mar 16, 2017 2:20 pm

savageautomate wrote:Prerequisite:
1.) Download and extract the Pi4J JAR files somewhere on your local system (http://pi4j.com/download.html)
2.) Download and install latest NetBeans IDE for Java Developers.

In NetBeans:
1.) File > New Project > Java > Java Application
2.) Enter a project name such as "Pi4JSample"
3.) Make sure the "Create Main Class" option is checked and use "ControlGpioExample" for the class name.
4.) Click "Finish" to create project template.
5.) In the project's tree, right-click the "Libraries" folder and select "Add Library"
6.) "Create" a new library using Library Name: "Pi4J-Core"
7.) Make sure the "Classpath" option is selected and then "Add JAR/Folder" - select the "pi4j-core.jar" file from your local file system (assuming you already have downloaded Pi4J on your computer)
8.) Select the "Sources" option and add the "pi4j-core-sources.jar".
9.) Select the "JavaDoc" option and add the "pi4j-core-javadoc.jar".
10.) Select the "OK" button to finish adding the Pi4J Library to NetBeans.
11.) You should now be back on the "Add Library" dialog. Make sure that "Pi4J-Core" is selected and click "Add Library" to add it to your project.
12.) Next, copy all the ControlGpioExample source code from https://raw.githubusercontent.com/Pi4J/ ... ample.java and paste it into your local ControlGpioExample.java source file replacing the existing source.
13.) Select the "Run" menu item and then "Build Project" sub menu item. This should compile the program and make sure there are no error. Look at the bottom of the IDE for the "Output" window and you should see a "BUILD SUCCESSFUL" message in the output text.

Running on the Pi from NetBeans (ref: https://blog.idrsolutions.com/2014/08/u ... pberry-pi/)
1.) In NetBeans, select the "Run" menu item and then "Set Project Configuration" sub menu item and click "Customize".
2.) Click "New" button to create a new configuration. Provide a name such as "Raspberry Pi" for the configuration.
3.) Next, click "Manage Platforms", then "Add Platform".
4.) Select "Remote Java Standard Edition" and click "Next" button.
5.) Provide a platform name such as "RPI-Platform"
6.) Provide the host name/address, username "pi" and password "raspberry".
7.) For the "Remote JRE Path"field, use:

Code: Select all

/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre
8.) Click "Finish" button.
9.) Now you should be at the "Java Platform Manager" dialog.
10.) Look for the "Exec Prefix" field under "Platform Properties". Edit this field and enter "sudo" (We need sudo to launch GPIO programs.)
11.) Click "Close" to close the "Java Platform Manager" dialog.
12.) Now back at the "Project Properties" dialog, select from the dropdown menu, Runtime Platform = "RPI-Platform". Click "OK" to close.
13.) From the NetBeans menu, select the "Run" menu item and then "Run Project" sub menu item.

You program will compile and then transfer the compiled program and libraries to your Raspberry Pi and then run the program on the Pi.

You should see some output similar to this:

Code: Select all

ant -f /Users/roberts/NetBeansProjects/Pi4J-Test -Dnb.internal.action.name=run -Dremote.platform.rp.target=linuxarmvfphflt-15 -Dremote.platform.password=***** -Dremote.platform.rp.filename=linuxarmvfphflt -Dremote.platform.java.spec.ver=18 run-remote
init:
Deleting: /Users/roberts/NetBeansProjects/Pi4J-Test/build/built-jar.properties
deps-jar:
Updating property file: /Users/roberts/NetBeansProjects/Pi4J-Test/build/built-jar.properties
compile:
Copying 1 file to /Users/roberts/NetBeansProjects/Pi4J-Test/build
Copy libraries to /Users/roberts/NetBeansProjects/Pi4J-Test/dist/lib.
To run this application from the command line without Ant, try:
java -jar "/Users/roberts/NetBeansProjects/Pi4J-Test/dist/Pi4J-Test.jar"
jar:
Connecting to 10.1.1.190:22
cmd : mkdir -p '/home/pi/NetBeansProjects//Pi4J-Test/dist'
Connecting to 10.1.1.190:22
done.
profile-rp-calibrate-passwd:
Connecting to 10.1.1.190:22
cmd : cd '/home/pi/NetBeansProjects//Pi4J-Test'; 'sudo' '/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java'  -Dfile.encoding=UTF-8   -jar /home/pi/NetBeansProjects//Pi4J-Test/dist/Pi4J-Test.jar 
<--Pi4J--> GPIO Control Example ... started.
--> GPIO state should be: ON
--> GPIO state should be: OFF
--> GPIO state should be: ON
--> GPIO state should be: OFF
--> GPIO state should be: ON for only 1 second
run-remote:
BUILD SUCCESSFUL (total time: 39 seconds)
Thanks to savageautomate for the step by step guide. This is mi first comment on raspberry forum and don't know if open a new threat or continue on this . so i will take the risk on this. After doing all the steps described in Robert´s post on step 6. "Running on the Pi from NetBeans"
on address field I used "127.0.0.1"
on Remote JRE Path"field (step 7) I used:

Code: Select all

/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre
all this running netbeans on my raspberry pi 3b. after finishing the process the ControlGpioExample runs normally and I could see the output as described on the post.
But.... nothing happens to the led, I have used gpio 1,2, 3,4,5,6 replacing the pin on the line :

Code: Select all

" Final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);"
Before installing netbeans I have installed a DS1302 RTC with the process described in :
http://www.hobbytronics.co.uk/raspberry ... time-clock

I don't know if the rtc is interferring or what happens with my RP3b, could you help me guys?

Rooock
Posts: 5
Joined: Thu Mar 16, 2017 1:33 pm
Location: Colombia

Re: Netbeans and pi4j importing

Thu Mar 16, 2017 3:18 pm

Thanks to savageautomate for the step by step guide. This is mi first comment on raspberry forum and don't know if open a new threat or continue on this . so i will take the risk on this. After doing all the steps described in Robert´s post on step 6. "Running on the Pi from NetBeans"
on address field I used "127.0.0.1"
on Remote JRE Path"field (step 7) I used:

Code: Select all

/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre
all this running netbeans on my raspberry pi 3b. after finishing the process the ControlGpioExample runs normally and I could see the output as described on the post.
But.... nothing happens to the led, I have used gpio 1,2, 3,4,5,6 replacing the pin on the line :

Code: Select all

" Final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);"
Before installing netbeans I have installed a DS1302 RTC with the process described in :
http://www.hobbytronics.co.uk/raspberry ... time-clock

I don't know if the rtc is interferring or what happens with my RP3b, could you help me guys?
Issue Solved.... checking at PI4J project page I read that PI4j uses numbering scheme same as wiring pi, not broadcom. So gpio4 is not on pin 7 (see raspberry oficial site info) using PI4j gpio4 is on pin 16.

armada86
Posts: 2
Joined: Wed Mar 22, 2017 9:47 am

Re: Netbeans and pi4j importing

Wed Mar 22, 2017 8:47 pm

Im trying to run the example from netbeans installed on the RPi itself,
I've done everything till step 8 , I used host 127.0.0.1 port 22 ,
when I click finish, it gives: Could not connect to java SE Platform.
Please help.

armada86
Posts: 2
Joined: Wed Mar 22, 2017 9:47 am

Re: Netbeans and pi4j importing

Thu Mar 23, 2017 9:59 am

Hi all,
I am using Netbeans installed on the RPi itself. How can I use the GPIO ? i have tried defining a new remote platform. but when i reach step 8 and click finish it gives Could not connect to remote platform

martinel91
Posts: 1
Joined: Thu Jan 18, 2018 6:55 pm

Re: Netbeans and pi4j importing

Thu Jan 18, 2018 7:00 pm

NOT WORK FOR ME (raspberry pi 3)
i did every thing to set up my pi and NetBeans as above and nothing happen with my LED, in IDE an terminal i get the messages "Should be ON" etc but my LED do nothig

i used this to test my pin on gpio and works, but when i complile the .jar nothing happen:
sudo gpio -g mode 21 out
sudo gpio -g write 21 1
sudo gpio -g write 21 0


and yes, i changed the pin number from 1 to 21 in java class

Return to “Java”