title: How to run a .class file that is part of a package from cmd?
body: <p>I keep getting errors when I make my <code>.class</code> part of a <code>package</code> and try to run it from cmd.</p>
  
  <p>Here's the code that works after using <code>javac</code> and then java:</p>
  
  <pre><code>class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello World!");
      }
  }
  </code></pre>
  
  <p>and then the code that does not work:</p>
  
  <pre><code>package com;
  
  class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello World!");
      }
  }
  </code></pre>
  
  <p>giving me this error after trying to run the program via the command: <code>java HelloWorld</code>:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
  e: com/HelloWorld)
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(Unknown Source)
      at java.security.SecureClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.access$100(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
  </code></pre>
  
  <p>Here's what I've tried doing so far:</p>
  
  <pre><code>java -cp . HelloWorld
  java -cp . com.HelloWorld
  java -cp . com/HelloWorld
  java HelloWorld
  java com.HelloWorld
  java com/HelloWorld
  </code></pre>
  
  <p>Keep in mind that <code>javac</code> returns with no errors and that simply removing <code>package com;</code> solves the problem. Sometimes in other scenarios I get an error that says the main class file cannot be found or something along those lines.</p>
  
  <p>What am I doing wrong?</p>