JTree Java code, Having error of cannot load main class, even it is having main class

146 views Asked by At

Question - Write a program that create hierarchical tree data structure

Science -> BSc IT -> NS -> ST -> ASP.NET -> ADV JAVA -> LINUX

Answer-   |
       \ /
        -
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.tree.*;

    class JTree extends JFrame
    {
        JTree t;
        DefaultMutableTreeNode sci, bscit, s1,s2,s3,s4,s5;
        public JTree()
        {
            Container c=getContentPane();
            c.setLayout(new FlowLayout());

            sci=new DefaultMutableTreeNode("Science");
            bscit=new DefaultMutableTreeNode("BSc IT");
            s1=new DefaultMutableTreeNode("NS");
            s2=new DefaultMutableTreeNode("ST");
            s3=new DefaultMutableTreeNode("ASP.NET");
            s4=new DefaultMutableTreeNode("ADV JAVA");
            s5=new DefaultMutableTreeNode("LINUX");

            sci.add(bscit);
            bscit.add(s1);
            bscit.add(s2);
            bscit.add(s3);
            bscit.add(s4);
            bscit.add(s5);

            c.add(t);

            pack();
            setVisible(true);
        }

        public static void main(String args[]) //Error at here of Can not load main class
        {
            JTree obj=new JTree();
        }
    }

JTree(run)

run:
java.lang.NoClassDefFoundError: jtree/JTree (wrong name: JTree)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Please help me to get rid out of it.

1

There are 1 answers

0
Naruto Biju Mode On

Rename your class name (eg Main) and create JTree field inside your class then create an instance of it inside the constructor of the main class:

import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;

class Main extends JFrame {
    JTree t;
    DefaultMutableTreeNode sci, bscit, s1, s2, s3, s4, s5;

    public Main() {
        Container c = getContentPane();
        c.setLayout(new FlowLayout());

        sci = new DefaultMutableTreeNode("Science");
        bscit = new DefaultMutableTreeNode("BSc IT");
        s1 = new DefaultMutableTreeNode("NS");
        s2 = new DefaultMutableTreeNode("ST");
        s3 = new DefaultMutableTreeNode("ASP.NET");
        s4 = new DefaultMutableTreeNode("ADV JAVA");
        s5 = new DefaultMutableTreeNode("LINUX");

        sci.add(bscit);
        bscit.add(s1);
        bscit.add(s2);
        bscit.add(s3);
        bscit.add(s4);
        bscit.add(s5);

        t = new JTree(sci);
        c.add(t);

        pack();
        setVisible(true);
    }

    public static void main(String args[])
    {
        Main obj = new Main();
    }
}