How can I solve java lang NoClassDefFoundError

0 votes

I've tried both the examples in Oracle's Java Tutorials. They both compile fine, but at run time, both come up with this error:

Exception in thread "main" java.lang.NoClassDefFoundError: graphics/shapes/Square
    at Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: graphics.shapes.Square
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

I think I might have the Main.java file in the wrong folder.

Here is the directory hierarchy:

graphics
├ Main.java
├ shapes
|   ├ Square.java
|   ├ Triangle.java
├ linepoint
|   ├ Line.java
|   ├ Point.java
├ spaceobjects
|   ├ Cube.java
|   ├ RectPrism.java

And here is Main.java:

import graphics.shapes.*;
import graphics.linepoint.*
import graphics.spaceobjects.*;

public class Main {
    public static void main(String args[]) {
        Square s = new Square(2, 3, 15);
        Line l = new Line(1, 5, 2, 3);
        Cube c = new Cube(13, 32, 22);
    }
}

Why am I doing this wrong?

UPDATE

The Main class was added to the graphics package (I added package graphics; to it), the classpath was set to the "_test" folder (which contains graphics), it was compiled, and Java Graphics was used to run it.

It functioned in main (from the command line).

Extremely tardy UPDATE #2

Since I wasn't using Eclipse and only had Notepad++ and the JDK, the aforementioned update fixed my issue. Though they share similar concepts, it appears that many of these responses are for Eclipse and IntelliJ IDEA.

Sep 20, 2022 in Java-Script by Tejashwini
• 780 points
913 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java-Script

0 votes
0 answers

How can I solve Exception in thread "main" java.lang.NullPointerException error

I am having a problem at a ...READ MORE

Sep 20, 2022 in Java-Script by Tejashwini
• 780 points
583 views
0 votes
1 answer

How can I send a message to a particular client with socket.io?

Hii, You can try the code below: io.to(socket.id).emit("event", data); whenever ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
27,829 views
0 votes
1 answer

How can I update NodeJS and NPM to the next versions?

Hello @kartik, First check your NPM version npm -v 1).Update ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
1,035 views
0 votes
2 answers

How can I set a session var using javascript and get it via php code?

Yes it is possible. You can either ...READ MORE

answered Aug 9, 2020 in Java-Script by Okugbe
• 280 points
28,778 views
0 votes
1 answer

How can I allow django admin to set a field to NULL?

Hello @kartik, Try to overwrite the save() method ...READ MORE

answered Jun 12, 2020 in Java-Script by Niroj
• 82,840 points
4,809 views
0 votes
1 answer

How can I Abort Ajax requests using jQuery?

Hello @kartik, It is always best practice to ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,840 points
2,902 views
0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,140 points
1,217 views
0 votes
2 answers

How can I solve java.lang.NoClassDefFoundError in Java?

NoClassDefFoundError means that the class is present ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,920 points
34,097 views
0 votes
2 answers

Explain the functionality of Math.random() method.

You can use java.util.Random class. int myNumber = ...READ MORE

answered Aug 3, 2018 in Java by Sushmita
• 6,920 points
1,350 views
0 votes
1 answer

What is the difference between package private, public, protected?

Modifier Class Package Subclass World Public Y Y Y Y Protected Y Y Y N No modifier Y Y N N private Y N N N   Public: Any package, any class accessibility is ...READ MORE

answered May 24, 2018 in Java by Parth
• 4,640 points
1,577 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP