Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running java helloworld

Tags:

java

I tried to google this, went to oracle.com and read all the questions on this forum related to this. I wrote a simple "Helloworld" program

package helloworld;

public class Helloworld {
    public static void main(String[] args) {

        System.out.println("Hello World!");
    }
}

NetBeans compiles the source code into a .class file. I move that file to C:\MyJava

I try to run it by C:\MyJava> java -cp . Helloworld and all possible variations of such. I keep getting the NoClassDefFoundError: Helloworld (wrong name: helloworld/Helloworld).

To make sure. There's a question almost exactly like this (difference in "wrong name"). The solution to that question does not work in my case.

like image 203
PBD10017 Avatar asked Feb 13 '26 05:02

PBD10017


2 Answers

You get the "wrong name" error because your class is in the package helloworld. Java expects you to provide the fully-qualified class name on the command line:

C:\MyJava> java -cp . helloworld.Helloworld

The directory structure must match the package structure. This means that you should have a directory C:\MyJava\helloworld that contains the class file Helloworld.class.

like image 143
Jesper Avatar answered Feb 14 '26 17:02

Jesper


You need to tell it the package name (which is helloworld):

C:\MyJava> java -cp . helloworld.Helloworld 
like image 25
Jon Lin Avatar answered Feb 14 '26 17:02

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!