Exception in thread “main” java.lang.ClassNotFoundException:

Question:

Confused about creating packages and classes. Trying to populate an array and display it.

package com.javarush.test;

/**
 * Created by User on 21.07.2015.
 */
public class Solution {
    public static void main (String[] args) {
        int [] table = new int [10];
        for (int i=0; i<table.length; i++) {
            table[i] = i;
            System.out.println(table[i] +" ");
        }
        System.out.println(table);
    }
}

Here's what it gives:

Exception in thread "main" java.lang.ClassNotFoundException: com.javarush.test.Solution
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

Process finished with exit code 1

How to solve this problem?

Answer:

In this screenshot, I saw that the test package is empty (no triangle next to test ).

  1. Place the file with the code in the directory corresponding to the package (i.e. <project_path>/com/javarush/test ).
  2. Check if the file name is the same as the class name (i.e. the file should be named Solution.java ).

PS So you will not display the array on the screen. With System.out.println(table) the Object.toString() method will be Object.toString() for table , which will Object.toString() something like [I@... for an array (see the documentation for Object.toString() ). Use Arrays.toString() from java.util.Arrays .

PPS And yes, as @Vladimir said, at first you shouldn't bother so much with nested packages (unless, of course, this is required in the task). Of course, working in the default package is not recommended, so create 1 package (in your case, test ) and work 🙂

Scroll to Top