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?");
   } 
}
8
  • 1
    Add the code to your question. Without it, it's hard to know if you have a package declaration, and if you have a proper main. Commented Sep 26, 2015 at 23:50
  • From which location you tried to run your command and what is structure of your classes? Commented Sep 26, 2015 at 23:52
  • OH!! I think I've got it. When I declare package Hello; java looks for a folder named Hello under the current folder. Commented Sep 26, 2015 at 23:54
  • package Hello; public class HelloWorld { public static void main(String[] args) { System.out.println("Hi there. How you doin?"); } } Commented Sep 26, 2015 at 23:54
  • 1
    Have you read this? stackoverflow.com/a/18093929/139985 Commented Sep 26, 2015 at 23:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You must run the code outside the Hello folder

java Hello/HelloWorld

You should have HelloWorld.class inside Hello directory.

1 Comment

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

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.