DevelopNow
Posts: 6
Joined: Fri Aug 17, 2012 8:07 am

Java GPIO access

Fri Aug 17, 2012 8:24 am

I'm trying to access the RPi GPIOs within Java but I'm not able to get it running.
I did take the example GPIO Driving Example (Java) from
http://elinux.org/Rpi_Low-level_peripherals
I did download
raspberry-gpio-0.0.1-SNAPSHOT.jar.zip
from
http://code.google.com/p/rpi-gpio-java/downloads/list
unzip the file and added the file location to the CLASSPATH variable with the command

Code: Select all

export CLASSPATH="/home/pi/"
Then I was able to compile it without error with

Code: Select all

javac VerySimpleServer.java
But when I try to start it with

Code: Select all

java VerySimpleServer
I got this error message:

Code: Select all

Exception in thread "main" java.lang.NoClassDefFoundError: be/doubleyouit/raspberry/gpio/GpioGateway
Caused by: java.lang.ClassNotFoundException: be.doubleyouit.raspberry.gpio.GpioGateway
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: VerySimpleServer. Program will exit.
Could anyone provide me with an running example to access RPi GPIOs incl. all steps that I have to do to compile and start it?

Thanks a lot in advance.

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

Re: Java GPIO access

Sat Aug 18, 2012 5:06 am

The error seems to suggest that the "raspberry-gpio-0.0.1-SNAPSHOT.jar" can't be found in the classpath. Your post text actually says raspberry-gpio-0.0.1-SNAPSHOT.jar.zip. Is the jar really packaged in the zip on your files system?

I was able to GPIO working with this Java library last night.
-Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Java GPIO access

Sat Aug 18, 2012 9:36 am

@DevelopNow: You have to put the Jar into the CLASSPATH, not just the directory name that contains the *.jar file:

Code: Select all

export CLASSPATH="/home/pi/raspberry-gpio-0.0.1-SNAPSHOT.jar"

Code: Select all

while not self.asleep():
    sheep += 1

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: Java GPIO access

Sat Aug 18, 2012 12:08 pm

You need to download and extract the library so you have a jar file in the same directory as your .java file.

This should then work fine but if not compile with javac -cp . VerySimpleServer.java and run with java -cp . VerySimpleServer

You can put the jar file in another directory such as libs and amend the classpath to be something like -cp .:libs note you need the . when running otherwise you will get an error that your own classes can't be found.

DevelopNow
Posts: 6
Joined: Fri Aug 17, 2012 8:07 am

Re: Java GPIO access

Sat Aug 18, 2012 3:29 pm

Thanks a lot for your replays!
I did unzip the file, but the problem was the missing "." Due to that missing dot, as fruitloaf wrote, my own class could not be found.
After fixing that, I got another error, that GPIO was not available, so I also had to download and add that in my CLASSPATH. And CLASSPATH-setting are not carried over when you are not in sudo-mode but you start the program with sudo.
For all beginners, here a step by step list what must be done to use GPIOs with Java on the Raspberry Pi:

Code: Select all

  a. sudo apt-get clean
  b. sudo apt-get update
  c. sudo apt-get install openjdk-6-jre
  d. sudo apt-get install openjdk-6-jdk
  e. Download and unzip the latest package from http://code.google.com/p/rpi-gpio-java/downloads/list so you got an .jar file in your working directory (e.g. raspberry-gpio-0.0.1-SNAPSHOT.jar)
  f. Download and unzip the latest package from http://commons.apache.org/io/download_io.cgi so you got an .jar file in your working directory (e.g. commons-io-2.4.jar)
  g. Activate sudo-mode by typing 'sudo su'
  h. Set the CLASSPATH environment variable by typing e.g. (depending on your working directory and package versions) 'export CLASSPATH="/home/pi/NetIo/raspberry-gpio-0.0.1-SNAPSHOT.jar:/home/pi/NetIo/commons-io-2.4.jar:."' Please be aware not to forget the ":." at the end as described before!
  i. Compile the Jave file 'yourclass.java' by typing 'javac yourclass.java'
  j. Start the Java file by typing 'java yourclass'

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: Java GPIO access

Sat Aug 18, 2012 4:43 pm

Personally I always stick the classpath in the commandline for java and javac and also set the following at the bottom of ~/.bashrc

Code: Select all

export JAVA_HOME=/opt/java/current
export PATH=$PATH:/opt/java/current/bin
Works much more reliably as it isn't effected by sudo

DevelopNow
Posts: 6
Joined: Fri Aug 17, 2012 8:07 am

Re: Java GPIO access

Sun Aug 19, 2012 7:57 am

Thanks for this tip.

Did you manage to enable pullup resistors with the Java GPIO package?
In Python I do it in that way and it works:

Code: Select all

# For Switch input we use pin 17 according BCM pin count
SWITCH1 = 17
# set up GPIO input with pull-up control
#   (pull_up_down be PUD_OFF, PUD_UP or PUD_DOWN, default PUD_OFF)
GPIO.setup(SWITCH1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
but I did not find any infos how to enable pullups with the Java GPIO package.

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

Re: Java GPIO access

Wed Aug 22, 2012 6:44 am

DevelopNow wrote:Thanks for this tip.

Did you manage to enable pullup resistors with the Java GPIO package?
In Python I do it in that way and it works: ...
but I did not find any infos how to enable pullups with the Java GPIO package.
Here is the source file where the setup() method is implemented.
http://code.google.com/p/rpi-gpio-java/ ... teway.java
I don't see any current support for the pull-up options.
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
xranby
Posts: 539
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Java GPIO access

Sun Sep 23, 2012 10:02 am

Hear Ye all fellow Java programmers ... Pi4J - A Java library to interface with the Raspberry Pi!
http://www.raspberrypi.org/phpBB3/viewt ... 46#p178646
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
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Java GPIO access

Sun Sep 23, 2012 11:28 pm

xranby wrote:Hear Ye all fellow Java programmers ... Pi4J - A Java library to interface with the Raspberry Pi!
http://www.raspberrypi.org/phpBB3/viewt ... 46#p178646
@xranby, Thanks for reminding me of this post.
DevelopNow wrote: Did you manage to enable pullup resistors with the Java GPIO package?
In Python I do it in that way and it works:
@DevelopNow, the Pi4J project does include programmatic control of the pull up/down resistance.
http://www.pi4j.com
http://www.savagehomeautomation.com/pro ... ry-pi.html
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

michaelvogt
Posts: 1
Joined: Sun Dec 09, 2012 9:51 am

Re: Java GPIO access

Sun Dec 09, 2012 10:06 am

The mentioned google code page for rpi-gpio-java gives me a 404 error. Does this project exist now at a different place, or is the Pi4J now the lib to use?


Thanks,
Michael

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

Re: Java GPIO access

Sun Dec 09, 2012 6:03 pm

michaelvogt wrote:The mentioned google code page for rpi-gpio-java gives me a 404 error. Does this project exist now at a different place, or is the Pi4J now the lib to use?
Thanks,
Michael
That google page (rpi-gpio-java) has been down for a couple of months now. I guess the author/developer decided to take down the project. The elinux page appears to have been updated with a new alternative library from jkranse: http://elinux.org/RPi_Low-level_peripherals.

My opinion would be to use Pi4J instead :mrgreen:. But of course, I have a very biased opinion since I started the Pi4J project. Pi4J was started to address some of the limitations of rpi-gpio-java and also to provide a more Java-like API for consuming GPIO on the Raspberry Pi. The Pi4J project also supports GPIO pin state change eventing and offers I2C and RS232 communications APIs in addition to GPIO. Pi4J has gained quite a few adopters and additional contributors and we have plans to take it much further.

Truthfully speaking you should probably try each of the available libraries out for yourself and see which one best suits your needs. I'm sure that each project has its own set of advantages and features.

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

Return to “Other programming languages”