2

i'm new to java. i've created a file called HelloWorld.java;

package tp;

 /**
 *
 * @author Utilisateur
 */
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("HelloWorld works!");
    }
}

compiled it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in; executed the code by doing: java -cp . HelloWorld in the same folder as HelloWorld.java is in.

but i get this error message

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: javaTp/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)

can anyone help?

3 Answers 3

2

The package name is your issue.

Java uses directories in compiled class folders to denote packages. Hence as your HelloWorld class defines itself as being in package 'tp', you need to do one of the following:

You can either run this (with your existing class):

> java -cp . tp.HelloWorld

Or you can remove the package declaration from the top of your class, recompile and run:

> java -cp . HelloWorld
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answer. choosed the second solution. it works now!
1

Because you declared the package tp, Java expects your HelloWorld.class file to live in a directory ./tp.

Typically a Java project will have a nested directory structure compiled to a mirrored one, such as:

src/
  tp/
    HelloWorld.java
classes/
  tp/
    HelloWorld.class

Comments

0

You need to use the full name: java tp.HelloWorld

2 Comments

Try again. His package name is "tp". It's also likely that he does not have the correct folder structure in place.
Space in between. Edited.

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.