0

I created a package in Eclipse and ran my HelloWorld just fine from Eclipse.

When I went to a command prompt and navigated to that folder and ran javac HelloWorld.java, it compilied without issue.

When I ran java HelloWorld, I got Error: Could not find or load main class HelloWorld

I also tried java Hello.HelloWorld thinking that it might be because it had a package declaration

package Hello; 
public class HelloWorld { 
    public static void main(String[] args) { 
       System.out.println("Hi there. How you doin?");
   } 
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Jeff Ciaccio
  • 83
  • 1
  • 7

2 Answers2

2

When you run javac, use the switch -d to specify that you would like to create the folder structure for package. javac reference.

E.g. javac -d . HelloWorld.java

When you say -d ., compiler creates the classes with package directory structure in the current path.

Once you have the compiled classes, use java Hello.HelloWorld to run the program.

Suggests you to start the package name with lower case.

James Jithin
  • 10,183
  • 5
  • 36
  • 51
0

You must run the code outside the Hello folder

java Hello/HelloWorld

You should have HelloWorld.class inside Hello directory.

rvillablanca
  • 1,606
  • 3
  • 21
  • 34
  • Thanks all!!! I realized I had to first create the classes folder in Windows before running the -d switch. I also was running java from within the folder where the class was. Going up a folder and giving it the relative path did the trick! Thanks again! @rvillablanca – Jeff Ciaccio Sep 28 '15 at 02:13