Can't seem to figure out why this simple program has stopped working on my computer. My programs that I compiled up until a Windows update are fine but when I try to compile a new program I get the following. I've attached a picture of the program and the command prompt error message. I've checked the CLASSPATH and it looks fine.
Program - HelloWorldApp.java
class HelloWorldApp{
public static void main(String[] args){
System.out.println("Hello World!"); //Display the string
}
}
execute javac HelloWorldApp.java - all is fine
execute java HelloWorldApp -
Error: Main method not found in class HelloWorldApp, please define
the main method as: public static void main(String[] args) or a JavaFX
application class must extend javafx.application.Application
Please excuse me if this has been answered. I searched but couldn't find this problem
Have you declared a String class among your list of classes? If so, try using java.lang.String[] args as argument to your main method.
Related
package concurrencyTest;
public class concurrencyTest implements Runnable
{
#Override
public void run()
{ System.out.println("Hello from a thread!"); }
public static void main(String[] args)
{
concurrencyTest c = new concurrencyTest();
Thread t = new Thread(c);
t.start();
}
}
Hi, I'm just trying to get my java concurrency test to run. But I'm getting this error :
Error: Main method not found in class concurrencytest.ConcurrencyTest, please
define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
I'm guessing that somewhere, in the myriad of project directories and subfiles that a java program needs to run, the name of the project or class has been wrongly referenced in lowercase letters. I've manually been thru all the files I can find that span from the root directory and renamed any instance of lowercase 'concurrencytest'. But still it seems the compiler finds a reference to lowercase concurrencytest and so refuses to compile. Any idea where this reference may be?
My root directory, source directory, and java code file are all called 'concurrencyTest'
edit
amended the original code for this question to include 'static' in the main method definition. Doing this was necessary but has not fixed the problem.
You wrote the main definition wrong. You should change it as below:
public static void main(String[] args) {
// ...
}
I found a reference to 'concurrencytest' in the meta file ...\concurrencyTest\nbproject\project.properties
So I changed it to 'concurrencyTest'
Also when NetBeans can't find the expected main method in the class it expects, it opens a 'RunProject' window where it lists main methods from classes it has found. For me it found a main in 'concurrencyTest' so I selected it.
One of these actions solved the problem, but not sure which one.
Take this code for instance
public class Hello
{
static void main(String[] args)
{
System.out.println("Hello World");
}
}
Here, I didn't write public for the main method and compiled the class. When I run the program why does the error read as "Could not find or load main class Hello.java".
My question is, if main(String[] args) is a 'method' then why say 'main class'?The point is not that public is there or not. The point is that I changed access modifier which caused main(string[] args) to be invisible to JVM. So why does JVM say main class and not main() method?
P.S. If this a stupid question then I really regret asking it.
Edit:-
Here is the error message
Error: Could not find or load main class Hello.java
There are two problems here.
main() must be declared as public static void.
However the real problem was your command line. Clearly it was
java Hello.java
It should have been
java Hello
There is no such class here as Hello.java. The name of the class is Hello.
filename: mainoverloading.java
error: could not find or load main class mainoverloading
class simple{
public static void main(int a)
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("Hi");
main(10);
}
}
Your class is named simple (not mainoverloading). Rename the class (or move the file "mainoverloading.java" to "simple.java").
When You compile the above given class using
javac mainoverloading.java
It is successfully compiled and a class file named simple.class is generated in your folder.
You can then run it by typing
java simple.
But this is actually not a good practice, as Elliott Frisch said rename your class into simple.java.
have a look For Windows- HelloWorld
For Linux - HelloWorld
Java allow us to use any name for file name, only when class is not public.
Its running nicely, because for eclipse main class it simple, its smart to identify that simple.class will be created.
If you are running from command line ,
class file created for your code is simple.class so JVM will be unable to find mainoverlading.class
I am new to Java and its my first program in Java, I am trying to run "Hello World" App program but its giving an error. It has compiled but has generated this error:
Exception in thread "main" java.lang.NoClassDefFoundError:
HelloWorldApp/class Caused by: java.lang.ClassNotFoundException:
HelloWorldApp.class at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
I wrote the given code in Note Pad and saved that in C:\Program Files x86)\Java\jdk1.6.0_14\bin
code:
public class HelloWorldApp{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Remeber that you must use specific filename in java so:
If your class is HelloWorldApp, you must put it into a file named HelloWorldApp.java
Then you must compile with javac HelloWorldApp.java.
Now, you can execute the result .class file with java HelloWorldApp
use only class, not public class.
public class can only be used when your java code file name is same as your public class name in which your main method is present.
use
class HelloWorldApp{
public static void main(String[] args){
System.out.println("Hello World");
}
The runtime's not finding the actual class file, rather than any issue with the code itself. For a starter though, you should be working in your own folder tree and be treating anything under "Program Files" as Read-only and not anywhere to be creating files.
Create your own folder elsewhere, compile then run, possibly specify the class path when running.
I want to create a simple java class, with a main method, but when I compile my code, I get this error message :
Error: Main method not found in class errors.TestErrors, please define
the main method as: public static void main(String[] args)
This is the source code :
package errors;
public class TestErrors {
public static void main(String[] args){
System.out.println("hello");
}
}
Why I'm seeing this error, as you can notice I've alreader declared the main method !
As said in my comments, looks like you've declared a String class among your own classes. To prove this, I've created a basic example:
class String {
}
public class CarelessMain {
public static void main(String[] args) {
System.out.println("won't get printed");
}
public static void main(java.lang.String[] args) {
System.out.println("worked");
}
}
If you execute this code, it will print "worked" in the console. If you comment the second main method, the application will throw an error with this message (similar for your environment):
Error: Main method not found in class edu.home.poc.component.CarelessMain, please define the main method as:
public static void main(String[] args)
This usually happens if ur complete project isnotconfigured correctly
or one of your class in project has still some errors
in such cases IDE will prompt stating the same that project contains some error
and you still proceed (ie run your class)
as project has some bugs new classes will not be created and IDE will run
the class which was available previously
to make sure this is ur case u can add new class in your project and try to run it and if ur getting no such class exist then there its is a perfect evidence
Just check your java file, it has not been saved. Please save all java files before compiling.
If the answers above are not working for you: make sure "main" is not capitalized in your method definition.
OK:
public static void main(String[] args)
ERROR:
public static void Main(String[] args)
Though the error message makes the required syntax for the main method clear, incorrect caps can be hard to spot. It took me ~30 minutes of troubleshooting to find this typo today. This was clearly not an issue for the OP, but is another easy/likely way to produce the same error message, and this answer may help other users.
I had this issue just now. This error came up because the JRE file that i switched out didn't had the full library. After I corrected/added the right JRE system library the problem went away.
Right click on the class name (which you are trying to run)->Run As->Run Configurations->Under Main Class Tab
Write your main class name and hit on Run.
Try commenting the first line.
//package errors;