Simple Java n00b help (hopefully seriously simple)

Soldato
Joined
4 Nov 2003
Posts
5,738
Location
Edinburgh
Were instructed to use eclipse for our java work, now on the uni lab comps they use 3.2 and 3.3 (but default is 3.2) i can't get 3.2, so i'm using 3.3.

Problem is I create a new project and load to simple ass java files into it, but i cannot for the life of my run it, when i KNOW it runs fine on the uni labs (3.2). I tried it on 3.3 on the lab comps today and it spat out the exact same error, about not being able to find the main class...:
"java.lang.NoClassDefFoundError: BounceMain
Exception in thread "main" "

I'll upload the two java files now, but i know they're Ok. What should i be looking out for?
Really getting to me this, becuase i need to start some real coding but if i can't rely on it to actually interpret or compile whats the point?
 
Right beansprouts uploader doesn't like java or txt files... So unfortunately you get them posted here!

BounceMain
Code:
import javax.swing.*;

/*************************************************
 * This is the main application class.
 * All it does is initialize the application,
 * and open a single window.
 * BouncePanel does all the work.
 *************************************************/

public class BounceMain {
	JFrame      mainWindow;
	BouncePanel helloPanel;
	
	// Create a new window, and put helloPanel inside it.
	public BounceMain() {
		mainWindow = new JFrame("Bouncing Ball");
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		helloPanel = new BouncePanel();
		mainWindow.getContentPane().add(helloPanel);
        
		mainWindow.pack();
		mainWindow.setVisible(true);
	}
    
	private static void createAndShowGUI() {
		// Make sure we have nice window decorations.
		JFrame.setDefaultLookAndFeelDecorated(false);
		// Create an actual instance of HelloBounceApp
		new BounceMain();		
	}

	public static void main(String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}


BouncePanel
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class BouncePanel extends JComponent 
                         implements ComponentListener, Runnable {
	
	final static long serialVersionUID = 0;  // kill warning
	
    // constants
    final static BasicStroke stroke = new BasicStroke(2.0f);
      
    // fields
    float xsize,ysize;  // size of window
    float xpos,ypos;    // position of ball 
    float xlen,ylen;    // size of ball
    float speed;        // distance the ball moves in each frame
    float dx,dy;        // current speed + direction of ball 
    
    int    delay;       // delay between frames in miliseconds
    Thread animThread;  // animation thread                       
                
    /*************************************************
     * Draws the ball on the screen.
     *************************************************/                                
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        
        // get the current window size     
        Dimension dim = getSize();
        xsize = dim.width;
        ysize = dim.height; 
        
        // clear background to white
        g2.setPaint(Color.white);
        g2.fill(new Rectangle2D.Double(0,0,xsize,ysize));       
                
        // draw ball                 
        g2.setPaint(Color.red);
        g2.fill(new Ellipse2D.Double(xpos, ysize-ypos-ylen, xlen, ylen));
        g2.setColor(Color.black);
        g2.draw(new Ellipse2D.Double(xpos, ysize-ypos-ylen, xlen, ylen));
 
        g2.dispose();
    }
    
    // empty methods that are required by the GUI event loop
    public void componentHidden (ComponentEvent e)  { }
    public void componentMoved  (ComponentEvent e)  { }
    public void componentResized(ComponentEvent e)  { }
    public void componentShown  (ComponentEvent e)  { }
    
    /****************************************************
     * Checks to see if the ball has hit any walls.
     * Called from within run(). 
     ****************************************************/
    public void checkWalls() {
       if (xpos + xlen >= xsize) { 
           xpos = xsize - xlen;
           dx   = -dx;
       } 
       if (xpos < 0) {
           xpos = 0;
           dx   = -dx; 
       } 
       if (ypos + ylen >= ysize) {
           ypos = ysize - ylen;
           dy   = -dy;
       }             
       if (ypos < 0) {
           ypos = 0;
           dy   = -dy;
       }
    }
    
    /***********************************************************
     * This is the animation thread.  
     * The code here is what actually causes the ball to bounce.
     ***********************************************************/ 
    public void run() {
      while (true) {  // loop forever          
        // update position
        xpos += dx;
        ypos += dy;
                
        // check to see if the ball has hit any walls
        checkWalls();
            
        // sleep a bit until the next frame
        try { Thread.sleep(delay); } 
        catch (InterruptedException e) { break; }
        
        // refresh the display
        repaint();  
      }
    }
        
    /****************************************************
     * This is a constructor for the BouncePanel class.
     * It initializes all the values that the class needs
     * in order to work properly.
     ****************************************************/
    
    public BouncePanel() {
        // set values for all the variables
        xsize = 480;
        ysize = 360;
        xpos  = 240;
        ypos  = 180;
        xlen  = 60;
        ylen  = 60;
        speed = 5;
        dx    = speed;
        dy    = speed;
        delay = 10;
        
        // set up window properties
        setBackground(Color.white);
        setOpaque(true);
        setPreferredSize(new Dimension((int) xsize, (int) ysize));
        setFocusable(true);
        addComponentListener(this);
        
        // start the animation thread
        animThread = new Thread(this);
        animThread.start();
    }
}
 
I'm not entirely sure what you mean?

Here is a screen of how i expected (and how it seems to work on 3.2?)

eclipse screen.PNG
 
Hmmmm, ok so thats not working either, what should i be looking at that isn't eclipse then? Thats a standard eclipse download not chnaged anything, it is running on vista though does this make a difference?
 
Right just got back in and round to doing this, installed the JDK 6 Update 4 just to make sure, i think i may have had it anyway. No change, its still spitting out main class errors. Arg...
 
Right i've totally cleaned my installations out, and installed the JDK again, it still wasn't compiling within the command prompt, but after setting the path it is compiling and running now. Although eclipse *still* isn't working... I'm beginning to lose faith here...
 
Right done that, can compile/run in the command prompt now, so i have something to show!

Eclipse is still arsing around. Could it be i've had Visual Studio installed on here, and/or GHCi and WinHugs? Or the fact i'm running vista?

Can't thank you enough for the help you're giving me, can't describe how lost i am and how this is driving me up the wall! This is meant to be a platform independent language and IDE! How can it be this hard! :D

Here's what i get from the eclipse console:

java.lang.NoClassDefFoundError: main
Caused by: java.lang.ClassNotFoundException: main
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main"
 
Last edited:
HelloWorld Compiles and runs from the command prompt fine, i've googled this all day, surely if this was a vista nasty it would be all over the internet by now? I'm just not sure if i'm searching for the right thing...

Anyways
I have no idea what this means, but this is what it says under import declarations (surely these where just taken from the code?) :p
BounceMain
javax.swing.*
BouncePannel
java.awt.*
java.awt.event.*
java.awt.geom.*
javax.swing.*

I may just end up using something else, although my course is taught under the EclipseIDE annoyingly so if they give any tutorials/walkthrough they're allways the Eclipse screenshots, and the lab computers only have Eclipse (although i could throw netbeans on them, it would be a pain...)
 
I have some work in for a thursday tutorial in uni, i missed the last one cause the b*****d wouldn't work, hope i can get something together for this tutorial...! Netbeans it may be.
 
It compiles and runs perfectly from the command prompt, and yeah i have the latest Eclipse, and i've downloaded it quite a few times *just to make sure* :p :D Bah, i'll try netbeans and hope to christ that works!

I can't understand if the command prompt can compile and run it, what does eclipse do inbetween using the same commands?
 
Well like yours it says its depriciated and non modifiable, but its still pointing in the right place. I've also got UAV turned off but i didnt run as admin, i'll give that a quick go now cant see it working....

//Edit: No joy, i'm still in the middle of other things currently, so i'll have a play with netbeans and just hope that does the job, you;ve been very helpful but i dont want to waste your time anymore! Cheers anyway though!
 
Last edited:
Well i'd love to "understand it" but i'm just starting off developing in java, lets get to small grips with the language first! I feel like i'm delving deep into the depths of eclipse just to get it working in any way? Doesn't seem right something is way off...
 
Yeah thats where this java is coming from now, its an OO programming course :D

Cheers guys, you've been very helpful i hope to get started on my task tonight after i've got my backup schedule sorted first, then i may be formatting this computer anyway (which may even sort out other problems with eclipse, but i shouldn't have to go to these measures!).
 
Back
Top Bottom