1) Create a public static void main(String args[]) {……} in your class and run this java class to run the code inside the main block
2) The second way is to create a static block inside a class static {……} and run the java file. The code inside the static block will get executed.
public class RunJavaMain { public static void main(String args[]) { System.out.println("Main method is called"); } } public class RunJavaStatic { static { System.out.println("This file is executed using main method"); System.exit(0); } }The System.exit(0) lets the program exit before the JVM is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
As of Java 7, it stop working. It compiles fine but while execution it gives the error message
No comments:
Post a Comment