2
$cat JarTest.java 

package edu.testjava.simple;

public class JarTest
{
    public static void main(String args[])
    {
        System.out.println("Hello World\n");
    }
}


$javac JarTest.java

$java JarTest

I get error:

Exception in thread "main" java.lang.NoClassDefFoundError: JarTest (wrong name: edu/testjava/simple/JarTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: JarTest.  Program will exit.

Why so?

Anything wrong with package etc?

Thanks!

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
xyz
  • 8,607
  • 16
  • 66
  • 90
  • 1
    What you did is totally intuitive, and it's very annoying that this doesn't just work. To expand on Matt's answer below, you need to specify that the classpath includes the current directory. I think this happens because java is looking in the directory ./edu/testjava/simple for your file. – Cory Kendall Sep 29 '11 at 06:46

2 Answers2

4

You need to do:

$ java -cp . edu.testjava.simple.JarTest

You need to specify the name of the class with the full package name (in this case edu.testjava.simple). You don't need .class or .java on the end.

You can also see the answers to How to execute a java .class from the command line.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
3

Yes, it's because of the package. Java enforces the project structure to be the same as the package structure. That means, that the class in the edu.testjava.simple package must be located in the edu/testjava/simple/ directory.

You may either:

  • Remove package declaration (this, however, is only acceptable in such "hello world" cases)
  • Compile the file using javac -d . JarTest.java (this will put the .class file in the appropriate directory) and launch it via java edu.testjava.simple.JarTest
Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
  • Yes, that's why i said "either". If you'll choose a first option, the source file can be compiled and executed in the way author proposes. – Alex Abdugafarov Sep 29 '11 at 07:00
  • after compiling it using -d, which will create appropriate matching directory, can I launch it by going manually in that directory and then just saying java JarTest ? – xyz Oct 05 '11 at 12:43
  • @p2pnode No, you can't, you'll need to be at a top level; but you can use `java -cp . edu.testjava.simple.JarTest` as Matthew Farwell suggested. – Alex Abdugafarov Oct 05 '11 at 19:50