Results 1 to 3 of 3
  1. #1
    swilliams236 is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Code compiles but fails to run (works in NetBeans)

    I have a basic GUI program that works fine in NetBeans but when I try and run in outside of NetBeans I get the following error...

    D:\Java II\BlueJTest>"C:\program files\java\jdk1.7.0\jre\bin\java" multioptioncalc.java
    Error: Could not find or load main class multioptioncalc.java

    Here is the source code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.text.*;
    public class MultiOptionCalc extends javax.swing.JFrame {
    
        /** Creates new form MultiOptionCalc */
        public MultiOptionCalc() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jComboBox = new javax.swing.JComboBox();
            jButton1 = new javax.swing.JButton();
            jTextField2 = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Mortgage Calculator", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N
    
            jLabel1.setText("Loan Amount:");
    
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
    
            jLabel2.setText("Terms of Loan:");
    
            jComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 Years at 5.35%", "15 Years at 5.5%", "30 Years at 5.75%" }));
            jComboBox.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jComboBoxActionPerformed(evt);
                }
            });
    
            jButton1.setText("Calculate");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jTextField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField2ActionPerformed(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()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(10, 10, 10)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(55, 55, 55)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .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))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(39, 39, 39)
                    .addComponent(jButton1)
                    .addGap(18, 18, 18)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(26, Short.MAX_VALUE))
            );
    
            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(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))
            );
            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))
            );
    
            pack();
        }// </editor-fold>
    
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // Float Variables
            double balance, monthlyPayment, rateToUse;
            int yearToUse;
            
            //Set declarations
            double monthlyPrincipal = 0;
            double monthlyInterest = 0;
            int[] years = {7, 15, 30};
            double[] rate = {5.35, 5.5, 5.75};
            
            //Array for loan terms
            for(int i=0; i<years.length; ++i)
                jComboBox.addItem(years[i] +  " years at " + rate[i] + "%");
            yearToUse = years[jComboBox.getSelectedIndex()];
            rateToUse = rate[jComboBox.getSelectedIndex()];
            NumberFormat nf = NumberFormat.getCurrencyInstance();
            
            // Parse text
            balance = Double.parseDouble(jTextField1.getText());
            double monthlyRate = ((rateToUse/100)/12);
            
            //Calculate payment
            monthlyPayment = balance*(monthlyRate/(1-Math.pow((1+monthlyRate),-1*
                        (yearToUse*12))));
            jTextField2.setText(nf.format(monthlyPayment));
            
            //Loop for amorization
            for(int counter=1; counter<=yearToUse*12; counter++);{
            monthlyInterest = (balance*monthlyRate);
            balance = (balance - monthlyPrincipal);
            monthlyPrincipal = (monthlyPayment - monthlyInterest);
            }
    }
    
    private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }
    
    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }
    
        /**
         * @param args the command line arguments
         */
        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(MultiOptionCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MultiOptionCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MultiOptionCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MultiOptionCalc.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 MultiOptionCalc().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JComboBox jComboBox;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Code compiles but fails to run (works in NetBeans)

    Quote Originally Posted by swilliams236 View Post
    I have a basic GUI program that works fine in NetBeans but when I try and run in outside of NetBeans I get the following error...

    D:\Java II\BlueJTest>"C:\program files\java\jdk1.7.0\jre\bin\java" multioptioncalc.java
    Error: Could not find or load main class multioptioncalc.java
    You can't run .java source code; you have to run your (compiled) class like this:

    Java Code:
    D:\Java II\BlueJTest>"C:\program files\java\jdk1.7.0\jre\bin\java" multioptioncalc
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    swilliams236 is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Code compiles but fails to run (works in NetBeans)

    Quote Originally Posted by JosAH View Post
    You can't run .java source code; you have to run your (compiled) class like this:

    Java Code:
    D:\Java II\BlueJTest>"C:\program files\java\jdk1.7.0\jre\bin\java" multioptioncalc
    kind regards,

    Jos
    That was it! Thank you Sir

Similar Threads

  1. Works within Netbeans, not in browser though
    By Ambergahill in forum New To Java
    Replies: 0
    Last Post: 11-27-2010, 05:46 PM
  2. Replies: 1
    Last Post: 11-03-2010, 08:49 AM
  3. Replies: 3
    Last Post: 04-15-2010, 02:41 AM
  4. Code compiles in/runs in IDE but not on UNIX system
    By Unclejunebug in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 08:35 AM
  5. Compiles in Netbeans, error in textpad
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •