I want to make a GUI application in java which can control the gpio pins. I used pi4j lib for control the pins. I wrote a pin toggle code inside the button click event as shown below. while running JAR file in Pi it showing the error as "can't find the main class". I would like to know whether it is possible to write the gpio control code in click event...pls help..
package javatest;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*
* @author R
*/
public class Javatest extends javax.swing.JFrame {
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
imageCross = new javax.swing.ImageIcon(cross);
imageCircle = new javax.swing.ImageIcon(circle);
if (a==0){
jButton4.setIcon(imageCross);
a=1;
}
else{
jButton4.setIcon(imageCircle);
a=0;
}
try {
System.out.println("PC1<--Pi4J--> GPIO Control Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
Thread.sleep(5000);
System.out.println("PC2<--Pi4J--> GPIO Control Example ... started.");
// provision gpio pin #01 as an output pin and turn on
final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);
System.out.println("--> GPIO state should be: ON");
Thread.sleep(5000);
// turn off gpio pin #01
pin.low();
System.out.println("--> GPIO state should be: OFF");
Thread.sleep(5000);
// toggle the current state of gpio pin #01 (should turn on)
pin.toggle();
System.out.println("--> GPIO state should be: ON");
Thread.sleep(5000);
// toggle the current state of gpio pin #01 (should turn off)
pin.toggle();
System.out.println("--> GPIO state should be: OFF");
Thread.sleep(5000);
// turn on gpio pin #01 for 1 second and then off
System.out.println("--> GPIO state should be: ON for only 1 second");
pin.pulse(1000, true); // set second argument to 'true' use a blocking call
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
} catch (InterruptedException ex) {
Logger.getLogger(Javatest.class.getName()).log(Level.SEVERE, null, ex);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Javatest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Javatest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Javatest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Javatest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Javatest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
// End of variables declaration
}