Forum Stats

  • 3,853,885 Users
  • 2,264,289 Discussions
  • 7,905,497 Comments

Discussions

Error: Could not find or load main class

2882122
2882122 Member Posts: 2
edited Feb 11, 2015 5:59PM in New To Java

Hello everyone!

I have decided to finally start learning Java and am currently, and slowly, making my way through the tutorials. I am only on chapter 2 in the class section. While reading I came uponJava Tutorials Sample Code and wanted to try and compile it in netbeans. This is the code that it contained:

class BicycleDemo {

    public static void main(String[] args) {

        // Create two different

        // Bicycle objects

        Bicycle bike1 = new Bicycle();

        Bicycle bike2 = new Bicycle();

        // Invoke methods on

        // those objects

        bike1.changeCadence(50);

        bike1.speedUp(10);

        bike1.changeGear(2);

        bike1.printStates();

        bike2.changeCadence(50);

        bike2.speedUp(10);

        bike2.changeGear(2);

        bike2.changeCadence(40);

        bike2.speedUp(10);

        bike2.changeGear(3);

        bike2.printStates();

    }

}

I did the following in order:

1) New project

2) Select Java under Categories and Java Application under Projects

3) I type BicycleDemo in the Project Name Field and click finish

4) I delete all the template code and paste the above code into the field

5) I click run and get the following error:

run:

Error: Could not find or load main class bicycledemo.BicycleDemo

Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)

Any help would be greatly appreciated! Thanks!

Answers

  • PhHein
    PhHein Member, Moderator Posts: 7,245 Silver Trophy
    edited Feb 11, 2015 3:15AM

    Error: Could not find or load main class bicycledemo.BicycleDemo

    either the package statement is missing, or the class BicycleDemo is not in the folder bicycledemo.

  • Unknown
    edited Feb 11, 2015 10:45AM
    I have decided to finally start learning Java and am currently, and slowly, making my way through the tutorials. I am only on chapter 2 in the class section. While reading I came uponJava Tutorials Sample Codeand wanted to try and compile it in netbeans. This is the code that it contained:
    
    class BicycleDemo {
    
    1) New project
    2) Select Java under Categories and Java Application under Projects
    3) I type BicycleDemo in the Project Name Field and click finish
    4) I delete all the template code and paste the above code into the field
    5) I click run and get the following error:
    
    run:
    Error: Could not find or load main class bicycledemo.BicycleDemo
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    

    Thanks for posting ALL of the info - most posters don't do that and it makes it hard to help them.

    Your exception shows it is looking for 'bicycledemo.BicycleDemo' so the 'BicycleDemo' class is expected to be in a package named 'bicycledemo'. The code you posted and the code at the link does NOT use a package statement.

    When you create a new project in NetBeans it will AUTOMATICALLY add a package name to the start of a new class file. So, unknown to you, it created a package named 'bicycledemo' and put the class file in it.

    In step #4 you deleted the template code (which had a 'package bicycledemo' statement in it) and replaced it with the tutorial code.

    Likely you didn't notice the package statement in the template before you deleted the template code.

    package bicycledemo

    .Add that package statement as the first line of the class source code.

    You should generally ALWAYS put your code in a package. For some reasons many of the tutorials do NOT do that.

  • 2882122
    2882122 Member Posts: 2
    edited Feb 11, 2015 5:28PM

    Thanks for the helpful suggestion but it seems I am still having trouble. Unless I misunderstood the new code should look like this:

    package bicycledemo

    class BicycleDemo {

        public static void main(String[] args) {

            // Create two different

            // Bicycle objects

            Bicycle bike1 = new Bicycle();

            Bicycle bike2 = new Bicycle();

            // Invoke methods on

            // those objects

            bike1.changeCadence(50);

            bike1.speedUp(10);

            bike1.changeGear(2);

            bike1.printStates();

            bike2.changeCadence(50);

            bike2.speedUp(10);

            bike2.changeGear(2);

            bike2.changeCadence(40);

            bike2.speedUp(10);

            bike2.changeGear(3);

            bike2.printStates();

        }

    }

    But now I am getting a different error:

    run:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol

      symbol:   class Bicycle

      location: class bicycledemo.BicycleDemo

      at bicycledemo.BicycleDemo.main(BicycleDemo.java:8)

    Java Result: 1

    This stuff is very confusing so far, but hey, we all start somewhere.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Feb 11, 2015 5:55PM
    2882122 wrote:
    
    Thanks for the helpful suggestion but it seems I am still having trouble. Unless I misunderstood the new code should look like this:
    
    But now I am getting a different error:
    [...]
    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
      symbol:   class Bicycle
      location: class bicycledemo.BicycleDemo
      at bicycledemo.BicycleDemo.main(BicycleDemo.java:8)
    Java Result: 1
    
    
    This stuff is very confusing so far, but hey, we all start somewhere.
    
    

    I'm a bit confused: I strongly beleve that any tutorial starts with the concept of a java class and that a class in Java usually has its own *.java file.

    The compiler complains that there is no definition for class Bicycle.

    So the question to you is: where ist the definition of the class Bicycle?

    bye

    TPD

  • Unknown
    edited Feb 11, 2015 5:59PM
           // Bicycle objects
            Bicycle bike1 = new Bicycle();
            Bicycle bike2 = new Bicycle();
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
      symbol:   class Bicycle
      location: class bicycledemo.BicycleDemo
      at bicycledemo.BicycleDemo.main(BicycleDemo.java:8)
    Java Result: 1
    

    Yes - you are referencing class 'Bicycle' but you don't have that class in your package and you have not IMPORTED it from another package.

    So where did you put that class?

    Lesson: Packages (The Java™ Tutorials > Learning the Java Language)

    Put them BOTH in the same package.

    At some point you need to follow the 'package' trail

    Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)

This discussion has been closed.