Java Newbie Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnolan
    New Member
    • May 2007
    • 2

    #1

    Java Newbie Problem

    Hello there,

    I'm brand new to Java and have. I'm taking my 2nd Java class at school and I'm pretty lost at this point.

    The main problem I'm having right now is I cannot get my code to execute. My code will compile, but when I try to execute it, either just using the "java" command or executing from TextPad, I get the following error:

    Exception in thread "main" java.lang.NoCla ssDefFoundError : MortgagePayment 1
    Press any key to continue . . .



    In my last class, my compiler was working fine and I could easily execute all of my applications. However, I stupidly ran Windows Updates and it installed Java 6 on my computer and nothing has worked right since.

    When I execute "java -version" from a command line, this is what I get:

    java version "1.6.0_01"
    Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
    Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)


    Here's the code I've compiled and am trying to execute:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.bor der.*;
    import java.text.Decim alFormat;

    public class MortgagePayment 1 extends JFrame implements ActionListener
    {

    private JTextField mortgageAmount;
    private JTextField mortgageTerm;
    private JTextField interestRate;
    private JTextField mortgagePayment Amount;
    private JButton calculateBtn;
    private JButton quitBtn;


    public MortgagePayment 1() {

    JPanel contentPane = (JPanel)getCont entPane();
    contentPane.set Layout(new BorderLayout(0, 4));
    JPanel northPanel = new JPanel();
    Border b = BorderFactory.c reateEtchedBord er();

    northPanel.setB order(BorderFac tory.createTitl edBorder(b, "Calculate Mortgage Payment", TitledBorder.TO P, TitledBorder.CE NTER));

    northPanel.setL ayout(new GridLayout(4, 2, 10, 10));


    JLabel mortgageAmountL abel = new JLabel("Input Initial Mortgage Amount (dollars)", JLabel.RIGHT);
    JLabel mortgageTermLab el = new JLabel("Input Mortgage Term (years)", JLabel.RIGHT);
    JLabel interestRateLab el = new JLabel("Input Interest Rate", JLabel.RIGHT);
    JLabel mortgagePayment AmountLabel = new JLabel("Mortgag e Payment Amount", JLabel.RIGHT);

    mortgageAmount = new JTextField(10);
    mortgageTerm = new JTextField(10);
    interestRate = new JTextField(10);
    mortgagePayment Amount = new JTextField(10);

    northPanel.add( mortgageAmount) ;
    northPanel.add( mortgageTerm);
    northPanel.add( interestRate);
    northPanel.add( mortgagePayment Amount);
    northPanel.add( mortgageAmountL abel);
    northPanel.add( mortgageTermLab el);
    northPanel.add( interestRateLab el);
    northPanel.add( mortgagePayment AmountLabel);
    // add the rest of labels and textfields

    JPanel southPanel = new JPanel();

    calculateBtn = new JButton("Calcul ate");
    southPanel.add( calculateBtn);

    calculateBtn.ad dActionListener (this);

    quitBtn = new JButton("Quit") ;
    southPanel.add( quitBtn);

    quitBtn.addActi onListener(this );

    contentPane.add (northPanel, BorderLayout.NO RTH);
    contentPane.add (southPanel, BorderLayout.SO UTH);

    }

    public void actionPerformed (ActionEvent evt)
    {

    JButton sourceBtn = (JButton)evt.ge tSource();

    if (sourceBtn == calculateBtn)
    {
    //calculate monthly payment

    //.... set mortgagePayment Amount textfield to monthly payment.
    }

    else if (sourceBtn == quitBtn)
    {
    System.exit(0);
    }

    }//close actionPerformed method


    public static void main(String[] args)
    {

    MortgagePayment 1 mp1 = new MortgagePayment 1();

    mp1.setTitle("M ortgage Payment: Assignment 1 developed by ...");
    mp1.pack();
    mp1.addWindowLi stener(new WindowAdapter()
    {

    public void windowClosing(W indowEvent e)
    {

    Window w = e.getWindow();

    w.setVisible(fa lse);
    w.dispose();

    System.exit(0);

    }//close windowClosing method

    });//close addWindowListen er method


    mp1.setVisible( true);

    }//close Main method

    }//close MortgagePayment 1 class


    Any help would be greatly appreciated.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by pnolan
    Exception in thread "main" java.lang.NoCla ssDefFoundError : MortgagePayment 1
    Press any key to continue . . .
    Your Java virtual machine simply can't find the file MortgagePayment 1.class.
    You need to tell it where it is. This is how you do it:

    1) go to the directory where the class is stored.
    2) type:
    Code:
    java -classpath . MortgagePayement1
    note the space-dot-space following the "-classpath" flag. This tells Java to look
    for classes in the current working directory.

    kind regards,

    Jos

    Comment

    • pnolan
      New Member
      • May 2007
      • 2

      #3
      Hi there,

      Thanks for the response. I did what you said, but I got the following error:

      C:\Java407>java -classpath . MortgagePayment 1
      Exception in thread "main" java.lang.Unsup portedClassVers ionError: Bad version n
      umber in .class file
      at java.lang.Class Loader.defineCl ass1(Native Method)
      at java.lang.Class Loader.defineCl ass(Unknown Source)
      at java.security.S ecureClassLoade r.defineClass(U nknown Source)
      at java.net.URLCla ssLoader.define Class(Unknown Source)
      at java.net.URLCla ssLoader.access $100(Unknown Source)
      at java.net.URLCla ssLoader$1.run( Unknown Source)
      at java.security.A ccessController .doPrivileged(N ative Method)
      at java.net.URLCla ssLoader.findCl ass(Unknown Source)
      at java.lang.Class Loader.loadClas s(Unknown Source)
      at sun.misc.Launch er$AppClassLoad er.loadClass(Un known Source)
      at java.lang.Class Loader.loadClas s(Unknown Source)
      at java.lang.Class Loader.loadClas sInternal(Unkno wn Source)


      You should know that since my original posting, I went in and uninstalled "ALL" my Java components and went back to Java 5 update 11. When I run java -version now I get:

      java version "1.5.0_11"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
      Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Recompile your MortgagePayment 1.java source file and everything should be fine.
        You currently have a version problem, that's why your JVM is throwing an error.

        kind regards,

        Jos

        Comment

        Working...