armf1993
Posts: 13
Joined: Fri May 29, 2015 1:18 am

First Java program for pi on rasperry pi with netbeans

Fri May 29, 2015 1:26 am

Hi, I am just a begginer at pi and I'm trying to do my first program with netbans for pi, but I am having a trouble, when I run the program, it crashes at "Accessing GPIO..." I'd be greatful if someone helpe me. The code is below

import javax.microedition.midlet.MIDlet;
import jdk.dio.DeviceManager;
import jdk.dio.UnavailableDeviceException;
import jdk.dio.gpio.GPIOPin;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author AugustoRigobertoMore
*/
public class HelloWorld extends MIDlet {

@Override
public void startApp() {
try {
System.out.println("Accessing GPIO...");
GPIOPin pin = (GPIOPin)DeviceManager.open(14);
System.out.println("Blinking...");

System.out.println("HIGH");
pin.setValue(true);
Thread.sleep(3000);
System.out.println("FALSE");
pin.setValue(false);
Thread.sleep(3000);
System.out.println("HIGH");
pin.setValue(true);
System.out.println("FALSE");
Thread.sleep(3000);
pin.setValue(false);

System.out.println("Done...");
pin.close();

} catch (UnavailableDeviceException ex){
Logger.getLogger(HelloWorld.class.getName()).log(
Level.SEVERE, null, ex);
} catch (InterruptedException ex){
Logger.getLogger(HelloWorld.class.getName()).log(
Level.SEVERE, null, ex);
} catch (IOException ex){
Logger.getLogger(HelloWorld.class.getName()).log(
Level.SEVERE, null, ex);
}
}

@Override
public void destroyApp(boolean unconditional) {
}
}

baantonia
Posts: 63
Joined: Fri Feb 06, 2015 2:19 pm

Re: First Java program for pi on rasperry pi with netbeans

Fri May 29, 2015 6:14 am

Probably due to security, you have to allow your application access to the GPIO pins. At the moment I don't have the details to do this. This is certainly the case when using Java ME, you have to allow access outside of the sandpit.

User avatar
DougieLawson
Posts: 40227
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: First Java program for pi on rasperry pi with netbeans

Fri May 29, 2015 6:33 am

You'll need to run it with sudo java -cp classpathstuffhere -jar jarfilenamehere
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Return to “Java”