Run application . jar made in swing

Asked

Viewed 119 times

0

I created a simple calculator, with just the add-on function. Running the IDE works normally. So I built and tried to run the application outside the IDE, but I was not successful. You could tell me how to run it?

I already went to the directory on the terminal and tried java -jar calculator.jar, but nothing happens, not even an error return.

inserir a descrição da imagem aqui

package my.numberaddition;

public class NumberAdditionUI extends javax.swing.JFrame {


public NumberAdditionUI() {
initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adição de Número", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12), new java.awt.Color(51, 51, 255))); // NOI18N

jLabel1.setText("Valor 1");

jLabel2.setText("Valor 2");

jLabel3.setText("Resultado");

jButton1.setText("Limpar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Calcular");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField3)
.addComponent(jTextField2)
.addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(10, 10, 10)
    .addComponent(jButton2)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
    .addComponent(jButton1)
    .addGap(27, 27, 27))
.addComponent(jTextField1))
.addGap(29, 29, 29))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1))
.addContainerGap(32, Short.MAX_VALUE))
);

jButton3.setText("Sair");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
    .addGap(0, 0, Short.MAX_VALUE)
    .addComponent(jButton3)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addGap(6, 6, 6))
);

pack();
}                     

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
System.exit(0);
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

float num1, num2, resultado;

num1 = Float.parseFloat (jTextField1.getText());
num2 = Float.parseFloat (jTextField2.getText());

resultado = num1+num2;

jTextField3.setText (String.valueOf (resultado));
}                                        
public static void main(String args[]) {
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(NumberAdditionUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NumberAdditionUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NumberAdditionUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NumberAdditionUI.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 NumberAdditionUI().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
}

The problem was that I had not specified the main class of the project. Using Netbeans, I right-clicked on the project/properties/run and there I specified my main class, causing the jar to run normally.

  • Alias, if there is no error, please enter the application code?

  • This one https://github.com/nelsoncbf/calculator/blob/master/src/my/numberaddition/NumberAdditionUI.java

  • 1

    Nelson is not how stackoverflow works, you must edit the question by adding a code here on the site and that is a [mcve] so that we can generate the jar too.

  • The code is over 9,000 characters long, breaking it here, no libraries or other classes would be hard to execute.

  • 1

    Did you access the link? You must provide an executable code, that is, without dependencies. How do you expect us to help you if we can’t even test and generate a jar? Go to and read the link to learn.

  • I accessed yes, I managed to post, editing the post. The limit 600 characters is only in comment.

  • Nelson, here executed without problems, follow my directions in the answer of the linked question as duplicate, in the yellow box, if you do as there, will work.

  • I am using Netbeans, unfortunately even generating the . jar, it does not run. I created an application with Javafx + Swing displaying Hello World in window and it worked normally when opening .jar. With the calculator the icon is not even generated, it looks like a compressed file in the icon, with extension . jar.

  • I added another one explaining in netbeans.

  • Yes, I saw it, thank you. The problem was never creating the . jar, the problem is that it is not running after creating the .jar. When I created the project he created the . jar smoothly, but only runs within Netbeans, does not run in production environment, outside the IDE. I will review everything and start from scratch to see if I identify the problem, even so, thanks a lot for helping Articuno.

Show 5 more comments
No answers

Browser other questions tagged

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