-5

I have a seemingly simple program in Java, but when I run it, I get the error:

Error: Could not find or load main class

Here is my code.

public class HelloPrinter
{
    public static void main(String[] args)
    {
        System.out.println("Hello, World!");
    }
}

I'm entirely new to the Java language, and I don't know what else to do. My program file is named "hello_world.java", and when attempting to run the program, I type in "java hello_world.java". Am I doing something fundamentally wrong? I've also attempted "java -cp hello_world", but that gave me the same error.

What am I doing wrong?

Jacob Marshall
  • 182
  • 2
  • 11
  • What you're doing wrong? Impossible to say, since you didn't tell us how you compile and run your code. – Tom Mar 30 '16 at 19:42
  • This error is very, very common. Please google it – Johnny Willer Mar 30 '16 at 19:42
  • How are you executing your app? – Mateus Viccari Mar 30 '16 at 19:42
  • I used a text editor to write the program, saved it as .java file, and ran it using the terminal application on a mac. What else am I supposed to do? – Jacob Marshall Mar 30 '16 at 19:46
  • Your certainly executing another .class file than `HelloPrinter.class` – Yassin Hajaj Mar 30 '16 at 19:46
  • Dup: [How to execute a java .class from the command line](http://stackoverflow.com/q/1279542) – Tom Mar 30 '16 at 19:48
  • I'm entirely new to the Java language, and I don't know what else to do. My program file is named "hello_world.java", and when attempting to run the program, I type in "java hello_world.java". Am I doing something fundamentally wrong? I've also attempted "java -cp hello_world", but that gave me the same error. – Jacob Marshall Mar 30 '16 at 19:48
  • The file must be named HelloPrinter.java if the class is HelloPrinter. See linked question. –  Mar 30 '16 at 19:49
  • I renamed the file to HelloPrinter.java as you recommended, but I'm still getting the same error. **Error: Could not find or load main class HelloPrinter.java** – Jacob Marshall Mar 30 '16 at 19:52
  • Just read the duplicate question. – Tom Mar 30 '16 at 19:52
  • I have read it, and I still do not understand what needs to be done. I ran "javac HelloPrinter.java" and received a new file called "HelloPrinter.class". Even after reading the duplicate question, I still do no understand what needs to be done. What obvious mistake am I making? – Jacob Marshall Mar 30 '16 at 19:56

1 Answers1

-1

From what little information you've given, I'd say you're executing it the wrong way.

Make sure you run the following two commands:

javac HelloPrinter.java
java HelloPrinter

The first command compiles your source code into a .class file. The second command executes that class file.

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • This works. That was the fundamental, obvious mistake I was making. I'm new to this, and I really appreciate your help. – Jacob Marshall Mar 30 '16 at 19:58
  • @Jacob Don't forget to [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) this answer if it helped you. – Tom Mar 30 '16 at 20:02