Results 1 to 11 of 11
  1. #1
    Leeish is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Angry NoClassDefFoundError

    Been working on this one for a bit to no avail. I followed a tutorial online just to get familiar with different concepts. All of this worked and I had a nice little sprite animation moving on my screen no flickering etc. Perfect.

    Then I tried to move a lot of my code to a Core class that would be extended from my main class. Not working but I have no idea why. I followed the tutorial for this part as well and I am getting a NoClassDefFoundError and as I can tell from reading this comes from members being available at compile time but not run time.

    I have changed no settings, libs, paths, etc. Nothing, only code.

    Here is my main class...

    Java Code:
    package game;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Game extends Core implements KeyListener {
        public static void main(String[] args) {
            new Game().run();
        }
        
        private String mess = "";
        
       //init also call init from superclass
       public void init(){
           super.init();
           Window w = screen.getFullScreenWindow();
           w.setFocusTraversalKeysEnabled(false);
           w.addKeyListener(this);
           mess="press escape to exit";
           
       }
       
       //Key Pressed
       public void keyPressed(KeyEvent e){
           int keyCode = e.getKeyCode();
           if(keyCode == KeyEvent.VK_ESCAPE){
               stop();
           } else {
               mess = "You Pressed : "+ KeyEvent.getKeyText(keyCode);
               e.consume();
           }
       }
       
       //Key Released
       public void keyReleased(KeyEvent e){
           int keyCode = e.getKeyCode();
           mess = "You Released : "+ KeyEvent.getKeyText(keyCode);
           e.consume();
       }
       
       //Last Method from interface
       public void keyTyped(KeyEvent e){
           e.consume();
       }
       
       //draw
       public synchronized void draw(Graphics2D g){
           Window w = screen.getFullScreenWindow();
           g.setColor(w.getBackground());
           g.fillRect(0, 0, screen.getWidth(), screen.getHeight());
           g.setColor(w.getForeground());
           g.drawString(mess, 200, 200);
       }
    }
    Here is the Core

    Java Code:
    package game;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Core {
        
        private static final DisplayMode modes[] = {
            new DisplayMode(800,600,32,0),
            new DisplayMode(800,600,24,0),
            new DisplayMode(800,600,16,0),
            new DisplayMode(640,480,32,0),
            new DisplayMode(640,480,24,0),
            new DisplayMode(640,480,16,0)
        };
        
        private boolean running;
        protected ScreenManager screen;
        
        //Stop Method
        public void stop(){
            running = false;
        }
        
        //Call int and Gameloop
        public void run(){
            try{
                init();
                gameLoop();
            }finally{
                screen.restoreScreen();
            }
        }
        
        //Set to Full Screen
        public void init(){
            screen = new ScreenManager();
            DisplayMode dm = screen.findFirstCompatibleMode(modes);
            screen.setFullScreen(dm);
            
            Window w = screen.getFullScreenWindow();
            w.setFont(new Font("Arial", Font.PLAIN,20));
            w.setBackground(Color.GREEN);
            w.setForeground(Color.WHITE);
            running = true;
        }
        
        //Main Game Loop
        public void gameLoop(){
            long startTime = System.currentTimeMillis();
            long cumTime = startTime;
            
            while(running){
                long timePassed = System.currentTimeMillis() - cumTime;
                cumTime += timePassed;
                
                update(timePassed);
                
                Graphics2D g = screen.getGraphics();
                draw(g);
                g.dispose();
                screen.update();
                
                try{
                    Thread.sleep(20);
                }catch(Exception ex){}
            }
        }
        
        //Update Animation to be Overidden
        public void update(long timePassed){
        }
        
        //Update Draw to be Overidden
        public void draw(Graphics2D g){
        }
    }
    Here is the Screen Manager
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JFrame;
    
    //Buffer Strategy to get rid of flickering
    //Rewrite Screen Class
    public class ScreenManager {
        
        private GraphicsDevice vc;
        
        
        //Give VC access to monitor (Screen)
        public ScreenManager(){
            GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
            vc = e.getDefaultScreenDevice();
        }
        
        //get all compatible DM
        public DisplayMode[] getCompatibleDisplaymodes(){
            return vc.getDisplayModes();
        }
        
        //compares DM passed in to vc DM and see if they match
        public DisplayMode findFirstCompatibleMode(DisplayMode[] modes){
            DisplayMode goodModes[] = vc.getDisplayModes();
            for(int x = 0;x<modes.length;x++){
                for(int y = 0;y<goodModes.length;y++){
                    if(displayModesMatch(modes[x], goodModes[y])){
                        return modes[x];
                    }
                }
            }
            return null;
        }
        
        //Get Current DM
        public DisplayMode getCurrentDisplayMode(){
            return vc.getDisplayMode();
        }
        
        //Compare Display Modes
        public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
            if(m1.getHeight() != m2.getHeight() || m1.getWidth() != m2.getWidth()){
                return false;
            }
            if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate() && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN){
                return false;
            }
            if (m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth() && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI){
                return false;
            }
            return true;
        }
        
        //Make Frame Full Scree
        public void setFullScreen(DisplayMode dm){
            JFrame f = new JFrame();
            f.setUndecorated(true);
            f.setIgnoreRepaint(true);
            f.setResizable(false);
            vc.setFullScreenWindow(f);
            
            if(dm != null && vc.isDisplayChangeSupported()){
                try{
                    vc.setDisplayMode(dm);
                }catch(Exception ex){}
            }
            f.createBufferStrategy(2);
        }
        
        //Set Graphics Object equal to this
        public Graphics2D getGraphics(){
            Window w = vc.getFullScreenWindow();
            if(w != null){
                BufferStrategy s = w.getBufferStrategy();
                return (Graphics2D)s.getDrawGraphics();
            }else {
                return null;
            }
        }
        
        //Updates Display
        public void update(){
            Window w = vc.getFullScreenWindow();
            if (w != null){
                BufferStrategy s = w.getBufferStrategy();
                if(!s.contentsLost()){
                    s.show();
                }
            }
        }
        
        //Return Full Screen Window
        public Window getFullScreenWindow(){
            return vc.getFullScreenWindow();
        }
        
        //Get Width of Window
        public int getWidth(){
            Window w = vc.getFullScreenWindow();
            if (w != null){
                return w.getWidth();
            }else {
                return 0;
            }
        }
        
        //Get Height of Window
        public int getHeight(){
            Window w = vc.getFullScreenWindow();
            if (w != null){
                return w.getHeight();
            }else {
                return 0;
            }
        }
        
        //Exit Full Screen
        public void restoreScreen(){
            Window w = vc.getFullScreenWindow();
            if(w != null){
                w.dispose();
            }
            vc.setFullScreenWindow(null);
        }
        
        //Create image compatible with your monitor
        public BufferedImage createCompatibleImage(int w, int h, int t){
            Window win = vc.getFullScreenWindow();
            if(win != null){
                GraphicsConfiguration gc = win.getGraphicsConfiguration();
                return gc.createCompatibleImage(w, h, t);
            } else {
                return null;
            }
        }
    }
    I don't think I need to include the Animation or sprite as the error is as follows.
    Java Code:
    run:
    Exception in thread "main" java.lang.NoClassDefFoundError: game/ScreenManager
    	at game.Core.run(Core.java:31)
    	at game.Game.main(Game.java:7)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)
    Core line 31 is | screen.restoreScreen();
    main line 7 is | new Game().run();


    Argh... what is wrong. The problem seems to come when I extended Core from Game, but I thought the guy in the video was doing that. Can you not extend your main Class.

    Does Core have to be my main class and then I extend stuff from that?

  2. #2
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: NoClassDefFoundError

    OK so your classes all belong to the game package.
    So your classes should be in: /somewhere/in/your/system/game
    To compile them use javac like you normally would do.
    To run them though you should be one directory higher (/somewhere/in/your/system/) and there invoke the java command with the cp option pointing to your working directory
    and the full name of your class like this game.Game.

    Hope i helped :)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: NoClassDefFoundError

    Can you describe the folders the class files are in and what the current directory and classpath are when you issue the java command?
    Is there a ScreenManager.class file in the games folder? Does the classpath point to the folder containing the game folder?

  4. #4
    Leeish is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: NoClassDefFoundError

    I am using NetBeans IDE to create and run the entire thing. I never had to create/organize folders. However everything is in:


    C:\Documents and Settings\admin\My Documents\NetBeansProjects\Game\src\game
    Listed Files:
    Animation, Core, Game, KeyTest, MainOld, Screen, ScreenManager, Sprite, SpriteOld

    I was digging through and for some reason in the folder

    C:\Documents and Settings\admin\My Documents\NetBeansProjects\Game\build\classes\game
    Listed Files:
    Animation$oneScene
    Animation
    Core
    Game
    Keytest
    SpriteOld

    ScreenManager does not exist. Where the F did it go? Didn't it have to have been there before when the app was running fine? I am guessing the \build\classes\game files are populated upon build time and because the ScreenManager class isn't in there, it's not being built. Am I right, and if so, now what.

    I don't know the dir/classpath when issueing the java command because I just run build/play via NetBeans not from cmd prompt or anything.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: NoClassDefFoundError

    Try going to the classes folder and entering the command:
    java -cp . games.Game

    Otherwise ask someone how to configure your IDE.

  6. #6
    Leeish is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: NoClassDefFoundError

    Ok, so dumb/strange, I went to the properties of the application and under Source Package Folders there is only one listed... the src folder.
    I added the src/game folder and ran the app and got the same compile error.

    I removed that thing that i just added... and it works now... No clue, makes no sense. Sounds like a NetBeans glitch.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: NoClassDefFoundError

    IDEs are wonderfully magical.

  8. #8
    Leeish is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: NoClassDefFoundError

    So is Michael Vick who broke his hand and is costing me my fantasy game. Thanks for the help everyone.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NoClassDefFoundError

    Quote Originally Posted by Leeish View Post
    Ok, so dumb/strange, I went to the properties of the application and under Source Package Folders there is only one listed... the src folder.
    I added the src/game folder and ran the app and got the same compile error.
    That's because game si not a src folder...it's a package.
    The source folder is the folder containing your packages (in your case src).

    But...by doing that I suspect you forced Netbeans to recompile everything hence:

    Quote Originally Posted by Leeish View Post
    I removed that thing that i just added... and it works now... No clue, makes no sense. Sounds like a NetBeans glitch.
    I would bet you didn't force a rebuild when you made your big changes, so Netbeans didn't bother compilig the ScreenManager.
    Generally with IDEs it's a good idea to clean and build if you make some significant structural changes, since they can lose track of what needs recompiling.

  10. #10
    Leeish is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: NoClassDefFoundError

    So when I run my app through the IDE even though it builds it, it doesn't necessarily build everything just what it thinks it needs to. But if I run the clean build it will force everything?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: NoClassDefFoundError

    Pretty much.
    Some IDEs can lose track of what needs building.

Similar Threads

  1. NoClassDefFoundError
    By bmeu in forum JDBC
    Replies: 1
    Last Post: 10-30-2010, 08:08 PM
  2. noClassDefFoundError
    By imorio in forum Eclipse
    Replies: 11
    Last Post: 08-21-2010, 07:45 PM
  3. NoClassDefFoundError
    By Dbirge in forum New To Java
    Replies: 0
    Last Post: 03-16-2010, 05:01 PM
  4. help with NoClassDefFoundError
    By chikoyzki06 in forum New To Java
    Replies: 2
    Last Post: 11-25-2009, 11:38 AM
  5. NoClassDefFoundError
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 12:20 PM

Posting Permissions

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