Error: could not find or load the main class hello.world.HelloWorld

I am trying to run this project called "hello user". I am new to Java, so I wrote a simple program that takes your name and displays "Hello". and when I start, I get the following error:

run: Error: Could not find or load main class hello.world.HelloWorld Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) 

But when I run the HelloWorld.java file, it does it perfectly

I do it on Netbeans IDE 7.2

+4
source share
6 answers

Instead of an encoding error, this may be due to the IDE. Since "Run File" works fine, but "Run Project" does not work, I think you need to configure something in the IDE itself. Right-click the project and select "Set as Home", now start the project. just giving him an assumption may not help you, but it's worth it. If this does not help, please insert your code too.

+2
source

You need to run the .class file containing the public static void main(String[] args) method.

Here, your HelloWorld.java file may contain a class method with main() . So you can run it.

This is because the execution of any Java program begins with a call to main() .. The JVM needs an entry point to your code .. What is main() .. If it does not find it .. It will not work.

So, make sure that any class file you use must have a main() method.

UPDATE : - And for the starting point, perhaps you can skip using packages .. Just go with a simple Java class without packages.

+1
source

Your class needs the function public static void main(String[] args) . And what's more, I suspect the error may be in the package.

If you want your class to be in <main_package>.<sub_package> , the directory structure

 - main_package - sub_package -HelloWorld.java 

And do not forget to write your class as follows.

 package main_package.sub_package; public class HelloWorld { public static void main(String[] args){ System.out.println("Hello " + args[o]); } } 

This is all due to the naming convention in Java

+1
source

This post may also appear in Eclipse (Juno 4.2.2 in my case), and I found two potential reasons for this. In my cases: 1. DTD was erroneous. I deleted the file and solved the problem *. 2. after cleaning the project, the external Jar that I built from the outside was deleted, as can be seen from the properties β†’ Java Build Path β†’ Libraries. *

* Solving one of the above problems, you need to restart Eclipse

0
source

if you use intellij idea, then simply rebuilding (cleaning and assembling) the project can solve your problem. because intellij may still be trying to load old classes that don't exist or have changed

0
source

Make sure you call as shown below:

  public class HelloWorld { public static void main(String[] args) { System.out.println("hello user"); } } 

To run a Java class offline, public static void main(String[] args) is the writing method that should be.

-1
source

Source: https://habr.com/ru/post/1438205/


All Articles