Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you run a Java program without main method? [duplicate]

Tags:

java

Possible Duplicate:
Printing message on Console without using main() method

Can someone suggest how can a JAVA program run without writing a main method..

For eg:

System.out.println("Main not required to print this");

How can the above line be printed on console without using the public static void main(String arg[]) in the class.

like image 636
user1515129 Avatar asked Jul 10 '12 20:07

user1515129


People also ask

How can we run Java program without main method?

Yes, we can execute a java program without a main method by using a static block. Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block.

Will Java compile without main method?

Yes, We can Compile and Execute a java program without Main function. Instead of that we can use static block to compile and Execute the program.


2 Answers

Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:

public class Foo {
    static {
         System.out.println("Message");
         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

In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:

The program compiled successfully, but main class was not found. Main class should contain method: public static void main (String[] args).

Here an alternative is to write your own launcher, this way you can define entry points as you want.

In the article JVM Launcher you will find the necessary information to get started:

This article explains how can we create a Java Virtual Machine Launcher (like java.exe or javaw.exe). It explores how the Java Virtual Machine launches a Java application. It gives you more ideas on the JDK or JRE you are using. This launcher is very useful in Cygwin (Linux emulator) with Java Native Interface. This article assumes a basic understanding of JNI.

like image 181
Konrad Reiche Avatar answered Sep 28 '22 10:09

Konrad Reiche


Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.

public class Hello {
  static {
    System.out.println("Hello, World!");
  }
}

Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:

Exception in thread "main" java.lang.NoSuchMethodError: main

[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.

As of JDK6 onward, you no longer see the message from the static initializer block; details here.

like image 45
maerics Avatar answered Sep 28 '22 09:09

maerics