Page 1 of 1

First Java program for pi on rasperry pi with netbeans

Posted: Fri May 29, 2015 1:26 am
by armf1993
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) {
}
}

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

Posted: Fri May 29, 2015 6:14 am
by baantonia
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.

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

Posted: Fri May 29, 2015 6:33 am
by DougieLawson
You'll need to run it with sudo java -cp classpathstuffhere -jar jarfilenamehere