0

I built a package called "com.hello" in eclipse and I wrote an easy HelloWorld program. Eclipse automatically added "package com.hello;" on top of my program. And HelloWorld.java was put in

F:\workspace\helloWorld\src\com\hello;

HelloWorld.class was put in

F:\workspace\helloWorld\bin\com\hello.

It worked very well in Eclipse. But when I entered the directory "F:\workspace\helloWorld\bin\com\hello" and used command line with "java HelloWorld," I got NoClassDefFoundError. I know it may have something to do with the classpath. But I'm not quite sure.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Tao Huang
  • 1,181
  • 1
  • 9
  • 14

2 Answers2

4

Your class is in a package com.hello. To run it, you must make sure the base directory of the package, which is F:\workspace\helloWorld\bin in your case, is in the classpath.

Try running it like this:

java -cp F:\workspace\helloWorld\bin com.hello.HelloWorld

You can also go to the directory F:\workspace\helloWorld\bin and then run it with

java com.hello.HelloWorld

This will work because Java will use the current directory as the default (if you do not have the CLASSPATH environment variable set).

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • That really works! Thank you very much! I've been learning RMI with command line. And this problem is puzzling me! Thx again~ – Tao Huang Oct 16 '12 at 08:54
0

Go to F:\workspace\helloWorld\bin\ and run it this way:

java -cp .; com.hello.HelloWorld
Dan D.
  • 32,246
  • 5
  • 63
  • 79