2

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.

inf3rno
  • 24,976
  • 11
  • 115
  • 197
PBD10017
  • 1,031
  • 2
  • 14
  • 22
  • 1
    `package hello world` is an invalid package declaration, I assume you want `package hello.world;` instead. – Thomas Jul 24 '12 at 08:50
  • @Thomas: Sorry that's my edit error. – nhahtdh Jul 24 '12 at 08:50
  • C:\MyJava>cd helloworld then C:\MyJava\helloworld>java Helloworld .run it like this it will work where helloworld is your packagename.Copy the .class with package(folder). – Dangling Piyush Jul 24 '12 at 08:51

3 Answers3

7

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Jesper, thanks so much. I don't know if this was supposed to be obvious, but it sure wasn't to me. Thank you. – PBD10017 Jul 24 '12 at 09:13
6

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

C:\MyJava> java -cp . helloworld.Helloworld 
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks Jon Lin. Your answer is right as well. Jesper added the package/folder structure that needs to be present to run java. Thanks. – PBD10017 Jul 24 '12 at 09:14
  • you do not need to specify a classpath pointing to ".". This is default behaviour ;) – zip Jul 24 '12 at 09:47
0

Below post is similar to your problem. I hope it guides you;

How do I run .class files on windows from command line?

Community
  • 1
  • 1