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!

1
  • 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. Commented Sep 29, 2011 at 6:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

javac -d . edu/testjava/simple/JarTest.java, with the appropriate / or \ for your system
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

3 Comments

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.
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 ?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.