0

Is there someone who can help me with this issue? When I run the Java Application directly on Netbeans, it works 100% fine. I can control the LED. But when I build the project and run the built jar file, the application appears but it seems like it cannot control the LED whenever I click the button.

Source Code:

package pi4j_led;

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;

public class mainWindow extends javax.swing.JFrame {

    //create GPIO Controller
    final GpioController gpio = GpioFactory.getInstance();

    //provision gpio pin #01 as an output pin and turn on
    final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "My LED", PinState.HIGH);



    public mainWindow() {
        initComponents();
        pin.setShutdownOptions(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        bOn = new javax.swing.JButton();
        lbDisplay = new javax.swing.JLabel();
        bBlinking = new javax.swing.JButton();
        bClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Controlling GPIO Pins Using Pi4J Library");

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Turn LED OFF and ON");

        bOn.setText("ON");
        bOn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bOnActionPerformed(evt);
            }
        });

        lbDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbDisplay.setText("The LED is turned ON");

        bBlinking.setText("BLINKING MODE: OFF");
        bBlinking.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBlinkingActionPerformed(evt);
            }
        });

        bClose.setText("Close");
        bClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bClose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(bOn, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bBlinking, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bOn, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                    .addComponent(bBlinking, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(lbDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bClose, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void bOnActionPerformed(java.awt.event.ActionEvent evt) {                                    
        if (bOn.getText().equals("ON")) {
            pin.low();
            bOn.setText("OFF");
            lbDisplay.setText("The LED is turned OFF");
        } else {
            pin.high();
            bOn.setText("ON");
            lbDisplay.setText("The LED is turned ON");
        }
    }                                   

    private void bBlinkingActionPerformed(java.awt.event.ActionEvent evt) {                                          
        if (bBlinking.getText().contains("OFF")) {
            bBlinking.setText("BLINKING MODE: ON");
            bOn.setEnabled(false);
            new Thread(() -> {
                for (;;) {
                    pin.low();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(mainWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    pin.high();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(mainWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if(bBlinking.getText().contains("OFF")){
                        break;
                    }
                }
            }).start();
        }else{
            bBlinking.setText("BLINKING MODE: OFF");
            bOn.setText("ON");
            bOn.setEnabled(true);
            lbDisplay.setText("The LED is turned ON");
        }
    }                                         

    private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {                                       
        gpio.shutdown();
        this.dispose();
    }                                      

    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(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mainWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton bBlinking;
    private javax.swing.JButton bClose;
    private javax.swing.JButton bOn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lbDisplay;
    // End of variables declaration                   
}
  • You should write an application that will test all outputs and then notice which one turns on the led. I am guessing you may have your outputs wrong. You should also post the code and maybe raspberry pi model. – Sam Orozco Feb 19 '20 at 05:52
  • I didn't catch the part where you said you can control the LED from netbeans. So the outputs are probably not the problem. Are you seeing any error from the console when you run the jar? – Sam Orozco Feb 19 '20 at 05:54
  • There are no problems with the code and it's output because it is 100% running fine when the project is runned in Netbeans. But when I "clean and build" the application and open the "dist" folder then run the Jar file, the application starts up then the LED turns ON because I set it up to HIGH when the application starts up, but when I click the buttons, there are no respond (but running on netbeans it is totally working). – Paulo Pacardo Rellon Feb 19 '20 at 06:02
  • And also the pin shutdown function of Pi4J is working fine when I click the close button which has codes for the pin shutdown. I think the function "low" and "high" is not working after building the project – Paulo Pacardo Rellon Feb 19 '20 at 06:05
  • The jar files needed for the pi4j is already included inside the "dist" folder. But it is still not working. – Paulo Pacardo Rellon Feb 19 '20 at 06:09
  • Please post a minimal example - just a main method which turns the LED on/off. List all the classpath items, exception messages and the command line you use to run the program outside of NetBeans. – jurez Feb 19 '20 at 08:35
  • Thanks for the help. I just edited my post and added the source code for the JFrame. The main class is only for setting the visibility of the JFrame to true. – Paulo Pacardo Rellon Feb 19 '20 at 13:18

0 Answers0