1

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                   
}
  • what command are you using to run it? – rob Mar 10 '15 at 13:07
2

Use java -cp myjar.jar javatest.Javatest

first argument after '-cp' is the jar file, 
second is the classpath to class containing main(String[]) method

If you dont have a MANIFEST in your jar file, you need to specify which class contains the main(String[] args) method. If you want to avoid it in the future, and be able to run a jar via doubleclick (non console) read How to create a MANIFEST file

  • it still showing the same error – user27849 Mar 11 '15 at 11:56
  • Make sure thet Javtest.java file is in /javatest sirectory in your jarfile. If ths doesn't work, just run the classfile instead of jar. Please post screenshot of your jarfile file hierarchy opened in archive viewer. – thekiwi5000 Mar 11 '15 at 14:03
1

You didn't mention what parameters you were passing to Java but I suspect that you are not including PI4J on your classpath.

You need to include the following:

.:/opt/pi4j/lib/'*'

for example

java -cp .:/opt/pi4j/lib/'*' javatest/Javatest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.