This question already has answers here:
"Error: Main method not found in class MyClass, please define the main method as..."
(10 answers)
Closed 9 years ago.
when i try to compile this:
public class Risk
{
}
class territory
{
public static void main (String[]arg)
{
System.out.println ("hi") ;
}
}
I get this error message:
Exception in thread "main" java.lang.NoSuchMethodError: main
whats going wrong here?
The class containing the main() function must be public, and you may only define one public class per file. You'll want to have two separate files Risk.java and Territory.java.
Risk.java:
public class Risk {
}
Territory.java:
public class Territory
{
public static void main (String[]arg)
{
System.out.println ("hi") ;
}
}
EDIT: It turns out this isn't true - I was able to run your initial code with the following command line:
java territory
But my earlier comments point to the best practice for a real app, such as a Risk game.
What class are you trying to run? If you're using the class territory, that will work. Risk has no main method, though.
Can you figure out why this example causes the same issue?
public class Simple {
public void main(String args[]) {
System.out.println("Inside function");
}
}
Answer: because main() should be public static void!
Could it just be a spacing issue? Your original post shows no space between the ']' and 'arg'.
Try this:
public static void main (String[] arg)
or, if that still doesn't work:
public static void main (String arg[])
What the answer wound up being was that the class i run must contain main or else it won't work. i'm posting this because, though other answers give roughly the same information, they don't make it explicit.
Related
Bad english alert
Whenever I try to use System.out.println on another class besides main, every single IDE installed in my PC returns the error on the title.
I'm writing a really simple code.
On IntelliJ, I had already tried to use "Invalidated caches" but didn't work as well.
Works here:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
System.out.println("Hi"); /* <-- This works on main, but doesn't
work in any other class opened in
another tab*/
}
But not in this other tab:
package javaapplication3;
public class NewClass {
System.out.println("Hi");
}
UPDATE
Here some images to specify the problem: Work here, but not here.
All code has to be in methods.
The command System.out.println("") will work only in a method.
Placing it under a class but not a method will result in the compiler throwing an error.
Eg:
public class test { // class
public static void main(String args[]) {
// inside main method
System.out.println("Hello, World!"); // correct
}
}
will work perfectly fine.
But, if you place the command under just a class, it's going to result in an error.[Needs to be in a certain method]
Eg:
public class test{
// inside a class, but no method
System.out.println("Hello, World!"); //incorrect
}
Also, you need to make sure your class and function are not reserved keywords.
And from what you've specified above, main in not a class but it's the main method of the class.
This question already has answers here:
Error: class X is public should be declared in a file named X.java
(19 answers)
Closed 4 years ago.
New to coding. Searched following query to no avail.
I'm using Dcoder IDE and trying the Hello World tutorial.
The following error message occurs when I try to output my code.
source_file.java:1: error: class HelloWorld1 is public,
should be declared in a file named HelloWorld1.java
public class HelloWorld1
^
1 error
My code is as follows
public class HelloWorld1
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
The file is saved as HelloWorld1.java
How do I resolve this?
Thanks
Your file is saved as source_file.java. In order for your code to compile, it must be named HelloWorld1.java.
To improve the readability of your code it is often a good practice to indent, like this:
public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Are you sure that the file is saved as HelloWorld1.java? Because in the output you posted it reads it as "source_file.java".
I was writing a code in which I print a statement "Hello World" but an error occur named cannot find symbol. I tried hard to remove this error but failed.
public class Input{
System.out.println("Hello World");}
This is the statement and the error
Please anyone can help me in resolving this error and tell me why this error occur so I will not repeat this mistake in future.
Use this :
public class Input {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
You can't print in the scope of a class. You need to inform more about the difference between a function and a class.
A "class" is sort of like a noun. It's the person, place or thing.
public class myThing {
//This is a comment
//put stuff that describes the thing in the curly braces
}
In order to make the thing do something, you must put it in a method
public void myMethod(){
//code in here gets run when myMethod runs
}
Methods must be in a class though. A method can usually be though of as "something a thing can do"
public class myThing {
public void myThingCanDoThis(){
//does this stuff only when called from somewhere else
}
}
The main method is a special type of method that always looks like this:
public static void main(String[] args) {
//This special method gets run automatically when your program runs
}
Your hello world should define a thing. It should have the method, or "verb", of "main".
public class myThing {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
For now just assume that all your java code must go inside of the main method. Learn the basics and then when you learn more start to pay attention to things like "public", "static", "class" , "void" etc. It will all make sense in time
This question already has answers here:
Why main() method is needed in java main class
(6 answers)
Closed 5 years ago.
I have been trying to make the Hello World Java application. But when I try to run the program it says that my selection has no main type. Here is my source code.
public class HelloWorldClass {
System.out.println("Hello world!");
}
Because it has no main() method where the code would take the starting point. Use this
public class HelloWorldClass {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Please note there are some of the things you should always take a note of while programming. Which is also known as the syntax of the language.
Java requires you to have the class written as the FileName, HelloWorldClass is the name of the file of yours.
Then, any data type of it. In my case it is void which means it won't return anything to you in the end.
Also, you should write String[] args which is the Parameter to the method. I was last night trying to understand why I should write these? There is a method, which runs without these, but Java recommends you to add the parameters.
When running Java you need a main method so the compiler knows what to run:
public class HelloWorldClass {
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
This question already has answers here:
How do I call one constructor from another in Java?
(22 answers)
Closed 6 years ago.
This made me mad (Eclipse Kepler)
public class FastReader
{
public static void main (String[] args)
{
FastReader a = new FastReader("hi");
}
public FastReader(int a)
{
}
public FastReader(String b)
{
FastReader(10);
}
}
And I get this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method FastReader(int) is undefined for the type FastReader
at FastReader.<init>(FastReader.java:14)
at FastReader.main(FastReader.java:6)
It almost made me mad! help me get rid of this!
Thank you!
Use
public FastReader(String b) {
this(10);
}
public FastReader(String b)
{
this(10);
}
This is the correct way to call the same class constructor.
If you want to call a same class constructor use the keyword 'this' if you want call the parent class constructor use the keyword 'super'.