Please help me out in out to get rid of this error. Whereas i tried the same code in Command line it was working perfectly without any exceptions.
public class String {
public static void main(String[] args) {
StringBuilder nb = new StringBuilder("");
nb.append("<string1>");
nb.append("<string2>");
System.out.println(nb.toString());
}
}
Error: Main method not found in class String, please define the main method as:
public static void main(String[] args) or a JavaFX application class
must extend javafx.application.Application
Don't name your class String as String is also a "build-in" type of Java (java.lang.String).
Related
I have recently started learning java and its my first OOP language.I read that static methods do not require the class to instantiated they run when you feed the class to the JVM.My question is what would happen if the static method is inside a private nested class. will it still run?
EDIT- I tried it does not work, I want to know what is happening in the background.
public class tester{
private class estupid{
public static void main(String[] args){
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
To the people down voting, A suggestion,A more productive activity would be telling what's wrong with the snippet, Thank You.
There are many mistakes, which you can simply get by compiling the code. I suggest you to use command line javac compilation
If you compile your code as it is
C:\src>javac tester.java
tester.java:3: error: Illegal static declaration in inner class tester.estupid
public static void main(String[] args) {
^
modifier 'static' is only allowed in constant variable declarations
1 error
As per above error, make your nested class as static nested class. Now the code will compile successfully but you will get error while running it:
C:\src>javac tester.java
C:\src>java tester
Error: Main method not found in class tester, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
With above error you can understand that you are running tester class, but it does not contain any main method which is searched by the JVM. So add a main method in tester class and yes you can call the static method in static inner class. Changed code will be like this which will run properly:
public class tester {
private static class estupid {
public static void main(String[] args) {
System.out.println("Hello Im a static method of a private class and main too");
}
}
public static void main(String[] args) {
estupid.main(args);
}
}
After compiling and running the above code
C:\Himanshu\GitHub\hsingh-learning\src>javac tester.java
C:\Himanshu\GitHub\hsingh-learning\src>java tester
Hello Im a static method of a private class and main too
This is just to correct your code and making it compilable and runnable, BUT writing main method in nested class is not suggested.
Other thing is that you are making private nested class, so you are making it unaccessible from outside of the holding class (tester class in your case). tester class is public and accessible to JVM but the nested class is marked private so that cannot be accessed.
That does not mean you cannot invoke main static method of nested class from JVM. Make your nested class public.
public class tester {
public static class estupid {
public static void main(String[] args) {
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
Compile it, which will produce 2 class files.
1. tester.class
2. tester$estupid.class
Run the 2nd tester$estupid which contains the main method (which is required by JVM)
C:\Himanshu\GitHub\hsingh-learning\src>java tester$estupid
Hello Im a static method of a private class and main too
The main method must be a member of a public class. A static method is a method that is a child of the class itself rather than an object, or an "instance" of that class.
Okay so I'm learning java and everything was working fine yesterday, and all of the sudden today all of the stuff i wrote (just some super basic stuff) no longer work, for example
package sectionOne;
public class AddingNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 10;
int y = 20;
int result = x+y;
System.out.println(result);
}
}
gives me error:
Error: Main method not found in class sectionOne.AddingNumbers, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Unless I change this:
public static void main(String[] args) {
to this:
public static void main(java.lang.String[] args) {
the program won't work. Also when I create a string like
String x = "test";
it won't work unless I do
java.lang.String x = "test";
What's going on?
You've got your own class named String that is causing conflicts with the compiler -- rename it to anything else (that doesn't conflict with core class names that is).
You've got a class that has the same name as the java.lang.String class in your namespace so:
1->try to search in every import and, if you find some String class imported, remove it.
2->search in your package if there is a class named String and rename it
if you did this and it still doesn't work, then your compiler has some problems :/
I typed this code into Eclipse
public class Hello
{
public static void main()
{
System.out.println("Doesn't execute");
}
// .....
}
When I press run it says that it does not contain a main type.
I don't know what I'm doing wrong , and I am new to java.
It should be:
public static void main(String[] args)
This is what your code should look like:
public class Hello {
public static void main(String[] args) {
System.out.println("Doesn't execute");
}
}
Notice the closing parenthesis, also I have properly changed your main method.
Here's another hint:
When you create a new Java class in Eclipse, there is an option to auto-generate the main method stub for you (this option would have fixed your error without you even knowing).
It is the first checked checkbox in the following screenshot.
main method without the string array arguments is not the method that JVM looks for to start the execution of a class.
After completion of the initialization for a class (during which other consequential loading, linking, and initializing may have occurred), the method main of class is invoked.
The method main must be declared public, static, and void. It must specify a formal parameter whose declared type is array of String. Therefore, either of the following declarations is acceptable:
public static void main(String[] args)
public static void main(String... args)
Read more about JVM startup, loading, linking and intilization of class here:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.4
I have written a simple java program mentioned in below. Unfortunately a compile error occurs.
class String {
public static void main(String[] args) {
System.out.println("stre");
}
}
The following comes out during compilation at command prompt:
c:\Java>java String
Error: Main method not found in class String, please define the main method as:
public static void main(String[] args)
It wasn't working for any of my programs, not even this simple one! Why is this?
EDIT:
Now I have:
import java.lang.*;
import java.lang.String.*;
class incogn {
public static void main(String[] args) {
System.out.println("stre");
}
}
And its not working. Why isn't that working?
It says the exact thing as before.
What you said with the Java.lang.String[] works, but why won't this? And why haven't I needed to put any of this on before?
It's probably because you are using the name String for your class, colliding with java.lang.String; in other words, you have written that your main method takes instances of your class as input, rather than the one that the main method needs to take, namely java.lang.String. Try either
Renaming your class
Changing your signature to public static void main(java.lang.String[] args) {
You should not call your class String, it will conflict with the java class String.
Or, you can change your code to:
class String {
public static void main(java.lang.String[] args) {
System.out.println("stre");
}
}
Don't create a java file named "String" as it is the java library class in java.lang.String
Make the class as public
So, write your source code as below:
public class StringDemo {
public static void main(String[] args) {
System.out.println("stre");
}
}
Compile it now. I believe it would work well.
1) The class should be public
2) Avoid to call you class String, as it has the duplicate name with java.lang.String (You still can, but not good). Otherwise, your main method is having wrong type of arguments.
3) You don't have to explicitly import from java.lang package.
public class String {
public static void main(java.lang.String[] args) {
System.out.println("stre");
}
}
OR
public class YourClass {
public static void main(String[] args) {
System.out.println("stre");
}
}
Try to use the second form.
Well, giving your class name String and then call main(String[] args) is ambiguous, if it means java.lang.String or your newly defined class, but with java scope rules, it will use your, unless otherwise declared.
Update after edit
There's no java.lang.String package, but it is a class. Therefore, you can't say
import java.lang.String.*;
By adding the ".*" the compiler understands that this refers to some package String.
One exception, is the static import, but this is not what you need here.
I was wondering what the effect of creating extra main methods would do to your code.
For example,
public class TestClass {
public static void main (String[] args){
TestClass foo = new TestClass();
}
}
After the program initially starts up, foo will be created and it would have another public main method inside it. Will that cause any errors?
It will cause no errors. Just because you initialize an object, doesn't mean the main method gets executed. Java will only initially call the main method of the class passed to it, like
>java TestClass
However, doing something like:
public class TestClass
{
public static void main (String[] args)
{
TestClass foo = new TestClass();
foo.main(args);
}
}
Or
public class TestClass
{
public TestClass()
{
//This gets executed when you create an instance of TestClass
main(null);
}
public static void main (String[] args)
{
TestClass foo = new TestClass();
}
}
That would cause a StackOverflowError, because you are explicitly calling TestClass's main method, which will then call the main method again, and again, and again, and....
When in doubt, just test it out :-)
The main method is static, which means it belongs to the class rather than the object. So the object won't have another main method inside it at all.
You could call the main method on instances of the object, but if you do that it's literally just another way of calling TestClass.main() (and it's frowned upon by many, including me, to call a static method on an instance of an object anyway.)
If you're referring to multiple main methods in the same program, then this isn't a problem either. The main class is simply specified and its main method is executed to start the program (in the case of a jar file this is the main-class attribute in the manifest file.)
It won't have an additional main-method, as main is static. So it's once per class.
If you have multiple main-methods in your project, you will specify which one to launch when starting your application.
This is perfectly fine. Having multiple main methods doesn't cause any problems. When you first start a Java program, execution begins in some function called main in a class specified by the user or by the .jar file. Once the program has started running, all the other functions called main are essentially ignored or treated like other functions.
After searching for a Java Class with multiple main() methods or in plain words, overloaded main() methods, I came up with an example of my own. Please have a look
public class MultipleMain{
public static void main(String args[]){
main(1);
main('c');
main("MyString");
}
public static void main(int i){
System.out.println("Inside Overloaded main()");
}
public static void main(char i){
System.out.println("Inside Overloaded main()");
}
public static void main(String str){
System.out.println("Inside Overloaded main()");
}
}
I tested this Java Code on JDK 1.7 and works like a charm !
You need "public static void main(String args[])" to start with and then you can call overloaded main methods inside this main and it should work for sure.
Any comments and suggestion are highly appreciated. I am just a novice Java Developer willing to develop my Java skills.
Thanks,
PK
No, you can have any number of main-methods in a project. Since you specify which one you want to use when you launch the program it doesn't cause any conflicts.
You can have only one main method in one class, But you can call one main method to the another explicitly
class Expmain
{
public static void main(String[] ar)
{
System.out.println("main 1");
}
}
class Expmain1
{
public static void main(String[] ar)
{
System.out.println("main 2");
Expmain.main(ar);
}
}
when you run your Java class it will always look for the signature public static void main(String args[]) in the class. So suppose if you invoking by command line argument, it will look for the method Signature in the class and will not invoke other until if u explicitly inoke it by its class name.
class MainMethod1{
public static void main(String[] ags){
System.out.println("Hi main 1");
testig2 y = new testig2();
//in this case MainMethod1 is invoked/.......
// String[] a = new String[10];
// testig2.main(a);
}
}
class MainMethod2{
public static void main(String[] ags){
System.out.println("Hi main 2");
}
}
But when you try the same from eclipse it will ask for which class to compile. Means MainMethod1 or Mainmethod2. So if te class has the exact signature they can be used as individual entry point to start the application.
Coming to your question, If you remove the signature as u did above by changing the argument if main method. It will act as a normal method.
It is all about the execution engine of JVM. Remember, you write >java com.abc.MainClass on cmd prompt.
It explains everything. If main method is not found here it throws a run time Error:Main Method Not Found in class MainClass.
Now if main method is found here, it acts as the first point when Program Counters have to map and start executing the instructions. Referred classes are loaded then, referred methods may be called using the instances created inside. So, main is class specific though one class can have only one main method.
Please note, main method's signature never changes. You can have two overloaded main methods in same class, like
public static void main(String[] args) {}
public static void main() {} //overloaded in same class.
During Static binding, the original main is resolved and identified by execution engine.
Another interesting point to consider is a case where you have two different classes in of java file.
For example, you have Java file with two classes:
public class FirstClassMultiply {
public static void main (String args[]){
System.out.println("Using FirstClassMultiply");
FirstClassMultiply mult = new FirstClassMultiply();
System.out.println("Multiple is :" + mult.multiply(2, 4));
}
public static void main (int i){
System.out.println("Using FirstClassMultiply with integer argument");
FirstClassMultiply mult = new FirstClassMultiply();
System.out.println("Multiply is :" + mult.multiply(2, 5));
}
int multiply(int a, int b) {
return (a * b);
}
}
class SecondClass {
public static void main(String args[]) {
System.out.println("Using SecondClass");
FirstClassMultiply mult = new FirstClassMultiply();
System.out.println("Multiply is :" + mult.multiply(2, 3));
FirstClassMultiply.main(null);
FirstClassMultiply.main(1);
}
}
Compiling it with javac FirstClassMultiply.java will generate two .class files, first one is FirstClassMultiply.class and second one is SecondClass.class
And in order to run it you will need to do it for the generated .class files: java FirstClassMultiply and java SecondClass, not the original filename file.
Please note a couple of additional points:
You will be able to run SecondClass.class although it's class wasn't public in the original file!
FirstClassMultiply overloading of the main method
of is totally fine, but, the only entry point to your prog
will be the main method with String args[] argument.