Java Swing program does not open window outside of Netbeans, but is open in processes

Asked

Viewed 895 times

2

My problem is that I made a program in Java in Netbeans and it works normally, but the problem is, when I open it. jar outside the IDE, window is not opened.

Here a simple code with the same problem:

package bbb;
import javax.swing.*;

public class NewFrame extends JFrame{
    public NewFrame() {
        initComponents();
    }
    private void initComponents() {
        jButton1 = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setSize(new java.awt.Dimension(700, 400));

        jButton1.setText("btn");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(305, 305, 305)
                .addComponent(jButton1)
                .addContainerGap(319, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
              .addContainerGap(228, Short.MAX_VALUE)
              .addComponent(jButton1)
              .addGap(217, 217, 217))
        );

        ImageIcon icon = new ImageIcon(getClass().getResource("/bbb/btn.png"));
        jButton1.setIcon(icon);

        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(() -> {
            new NewFrame().setVisible(true);
        });
    }

    private JButton jButton1; 
}

Some details:

  • The Newframe class is already the main;
  • I already put a folder with the same package name, in the same directory, with the same images;
  • If I leave the button with no icon, the . jar normally opens;
  • The program normally opens in Netbeans (with or without icon);
  • I’ve also tried opening by cmd (unsuccessfully);
  • The . jar does not open the window (Jframe), but appears in the background in the Task Manager. Ger. de Tar. após tentar abrir o .jar 3x

Please help me. I don’t know what else to do ;-; .

  • In this project of yours in Netbeans, the only class with a method main is that it? If you open your . jar, there will be a file META-INF\MANIFEST.MF, see if the property Main-Class: is pointing to bbb.NewFrame.

  • The problem seems to me to be null reference. Probably this /bbb/btn.png is not being added to the jar, and if you run outside netbeans, java cannot locate.

  • Do the following test: Run the jar with the following at the command prompt with the following command: java -jar <caminho completo da pasta>\<nomedojar>.jar. See if error bursts, if yes, copy the stacktrace and add to the question.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.