title: java.lang.NoClassDefFoundError when using package xyz; on top. Works otherwise
body: <p>I am running a simple program <em>Lec01.java</em> in directory ...src/one. So I add <strong>package one;</strong> on top of the file as seen below.</p>
  <p>Here is Lec01.java:</p>
  <pre><code>package one;
  
  public class Lec01 {
      public static void main(String[] args) {
          System.out.println(&quot;Not public!&quot;);
      }
  }
  </code></pre>
  <p>Now when I compile using <strong>javac Lec01.java</strong>, it compiles fine and creates a <em>Lab01.class</em> file for me in the same folder. But when I run it using <strong>java Lab01</strong>, it says:</p>
  <p><em>Caused by: java.lang.NoClassDefFoundError: one/Lec01 (wrong name: Lec01)</em></p>
  <p>But when I remove <strong>package one;</strong> from the start, it runs fine.</p>
  <p>Here is the new Lec01.java, which runs fine:</p>
  <pre><code>public class Lec01 {
      public static void main(String[] args) {
          System.out.println(&quot;Not public!&quot;);
      }
  }
  </code></pre>
  <p>What I could learn from other similar questions is that <strong>in the first case, it's not finding the generated .class file when it wants to run</strong>. It's also related to classpath but I cannot exactly figure out how. And in the second case, it finds it as it searches in the source directory where it created the class.</p>
  <p>But what confuses me is that the .class file gets created at the same location in both the cases, but in one case it can find it, in the second case, cannot.</p>
  <p>Could someone please tell me what's going on?</p>