Error: cant find main class

Go To StackoverFlow.com

2

I am a newbie java dev using netbeans IDE 7.1.1 and im watching this tutorial and right off the bat i get an error in my program even after 5 retypes to make sure its exactly the same as in the video so anyways this is the error

Error: Could not find or load main class javagame.JavaGame Java Result: 1

and this is the code i have written

package JavaGame;

import javax.swing.JFrame;

public class JavaGame extends JFrame {

    public JavaGame(){
        setTitle("java game");
        setSize(500, 500);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation();

    }

    public static void main(String[] args){

    }
}

You can find in this image how my project structure looks.

2012-04-04 23:08
by Vurb
How are you trying to run it - Shahzeb 2012-04-04 23:17
im just hitting the big green arrow up on the toolba - Vurb 2012-04-04 23:33
Maybe you should learn a little about java basics instead of trying to let your IDE do everything for you - jahroy 2012-04-04 23:42


2

If that is an exactly copy-paste from your code, then your problem is with package name. Java is case sensitive and your package name is JavaGame so the qualified name of your class is JavaGame.JavaGame whilst the exception you get says that it can not find the class javagame.JavaGame.

Rename your package using Netbeans from JavaGame to javagame. That should fix the problem.

2012-04-04 23:13
by Alonso Dominguez
That's not the problem - Shahzeb 2012-04-04 23:18
when i do this it makes the "public class JavaGame extends JFrame" to have and erro - Vurb 2012-04-04 23:20
@Vurb, can't figure out the error if you don't tell what it is. The error mentioned in the question is due to case sensitiveness, so, either rename your package or change the reference to your qualified class name when invoking it - Alonso Dominguez 2012-04-04 23:26
@Vurb, by the way, to rename a package using Netbeans you should use the "Refactor" menu option, don't do it by hand - Alonso Dominguez 2012-04-04 23:29
i changed the package name but still same thin - Vurb 2012-04-04 23:36
did you tried to left-click on the package symbol that contains your class and use Refactor > Rename ? it's sounds like a really weird error ... if you could add a small screenshot to your post showing that package structure it would be grea - Alonso Dominguez 2012-04-04 23:42
If you don't use the refactor option, you probably need to change the location of your class file as well. Your IDE sets everything up for you when you create a new class (including the location of your class files). You just changed the package name, which affects where the compiler will look for compiled classes - jahroy 2012-04-04 23:44
ya here is the image link http://imgur.com/o7ev - Vurb 2012-04-04 23:46
what I can see in that screenshot is that you are renaming the class, not the package. the package is the node that contains the class file (and it has a symbol of a package, by the way). In the other hand, the error that Netbeans is maybe showing you is because you are not following java naming conventions, which state that package names should be in lowercase whilst class names in camel case; in other words, you package should be javagame and your class JavaGame - Alonso Dominguez 2012-04-04 23:50
ok so now i named the package javagame and the class JavaGame but it still shows the same erro - Vurb 2012-04-04 23:56
after all, I just can suggest to delete the project and re-start from scratch (and when Netbeans is asking you if you want to remove the sources from disk mark the checkbox). I'm telling this 'cos I feel that I can not help you more without being sited beside you. Listen, your code is simple, it doesn't has any error at first sight and it must work at first try. Surely the problem comes from messing up the names of classes and packages. Restart but keep the package and class names as I've suggested you, and never try to change them in the code, you MUST always use the "Refactor" option - Alonso Dominguez 2012-04-05 00:07
ok well i restarted and this time i just named it game and what do you know it works. Sorry for all the trouble, thanks for your help - Vurb 2012-04-05 00:11
you should accept the answer (there is tick mark beside each answer that when clicked it means that you accepted it), at least do it as a sign of gratitude for the effort of helping you ; - Alonso Dominguez 2012-04-05 00:14
instructions to accept an answe - Alonso Dominguez 2012-04-05 00:46


1

The problem is with your package name.. change the code at line1

package JavaGame;    
package javagame;

NetBeans usually likes to use lower text for packages.

Hope this solves the problem. As the code runs on my machine.

Phil

2012-04-05 00:03
by Phil
ya i did this but still same thin - Vurb 2012-04-05 00:04
In the folders on the left what is your package called and what is the package code at the top of your program - Phil 2012-04-05 00:08
the package is called javagame and the package code is package javagame - Vurb 2012-04-05 00:10
This should work as it works on my NetBeans. My package is called javagame and i copyed your code in and changed JavaGame to javagame. Make sure your imports are below the package. Try reloading The IDE. Can you run other programs fine? Phi - Phil 2012-04-05 00:14
ya other programs run fine but i just made a new project called game and it worke - Vurb 2012-04-05 01:33


0

Go to environmental variables and add path of JDK.
I got this error in past, and solved by setting classpath.

2012-04-09 14:19
by unknown
Ads