Printing a message to the console without using the main () method

I was asked this question in an interview.

How to print a message to the console without using the main() method?

+42
java
Dec 22 2018-11-12T15
source share
10 answers
 public class Foo { static { System.out.println("Message"); System.exit(0); } } 

System.exit(0) terminates the program before jvm starts looking for main()

Perfect link

(Note: even if it is compiled using the JDK 7 javac , it cannot be started with its java , because it expects the main(String[]) method.)

+62
Dec 22
source share
 public final class Main { static { System.out.println("Hello World"); System.exit(0); } } 

The static block is first executed as soon as the class is loaded before the main(); method is called main(); , and therefore, before calling main() System.exit(0) initiates the shutdown of the VM.

The System.exit method stops the execution of the current thread, and all others are dead in their tracks. When System.exit is called, the virtual machine performs two cleanup tasks before closing.

First, it executes all final locks registered in Runtime.addShutdownHook . This is useful for allocating resources external to the virtual machine. Use disconnect hooks for behavior that must occur before the VM exits.

The second cleaning task performed by the virtual machine when invoking System.exit concerns finalizers. If either System.runFinalizersOnExit or its evil twin Runtime.runFinalizersOnExit , VM runs finalizers on all objects that have not yet been finalized. These methods were obsolete a while ago and not without reason. Never call System.runFinalizersOnExit or Runtime.runFinalizersOnExit for any reason: they are some of the most dangerous methods in Java libraries. Calling these methods can cause finalizers to run on live objects, while other threads simultaneously manipulate them, resulting in unstable behavior or deadlocks.

Thus, System.exit immediately stops all program flows; This does not finally force the blocks to execute, but it starts the final hooks before the VM stops. Use interrupts to complete external resources when the virtual machine shuts down. You can pause a virtual machine without intercepting it when closing System.halt , but this method is rarely used.

+12
Dec 24 '11 at 2:06 p.m.
source share

In a file named A.java

 class Con { String hi = "\n\nHello World\n\n"; } 

You just need to compile the program on Windows. Do not run it. :-P

+10
Dec 22 2018-11-11T00:
source share
 class MainMethodNot { static { System.out.println("Hello World"); System.exit(0); } } 

Since the static initializer block is executed the first time the class is loaded, we can print "Hello World" without writing the main method. Execution is terminated using the "System.exit ()" command. Thus, we prevent the error "main method not found". This is a pretty tricky question.

+6
Dec 22 2018-11-12T00:
source share

You can define a custom class loader that prints your message:

 public class MyClassLoader extends ClassLoader { public MyClassLoader(ClassLoader other) { super(other); System.out.println("Hi there"); System.exit(0); } } 

Then run the java command:

java -Djava.system.class.loader=MyClassLoader

(no need to add a class as a parameter)

+6
Aug 29 '13 at 9:20
source share

In java 6, you could use System.out.println (); without main (). Starting with java 7, this cannot be done with a static block. It will still request the main method in the main class.

+2
Mar 01 '15 at 12:31 on
source share

If you also do not want to use a static block, this can be done as follows.

 public class NoMain { private static final int STATUS = getStatus(); private static int getStatus() { System.out.println("Hello World!!"); System.exit(0); return 0; } } 

However, note that this is for Java version 6. It does not work in Java 7 , which is said to be supported in Java 8. I tried to use JDK 1.8.0_77-b03, which still does not work

+2
Apr 24 '16 at 17:23
source share

Yes, one way is a static block, but in the previous version of the JDK, not in JDK 1.7.

 class Withoutmain{ static{ System.out.println("Message: Your message can be print on console without main() method"); System.exit(0); } } 

Output: Message: your message can be printed on the console without the main () method (if not JDK7)

Output: error: main method not found in class A3, define the main method as follows: public static void main (String [] args)

Link

+1
Nov 03 '14 at 6:28
source share

Actually, this does not work in the latest update of java 8. You can call it error correction according to them, but as far as I am confident in my current knowledge, this cannot be called error correction, since it also leads to several conceptual changes in Java programming.

0
Sep 30 '16 at 18:17
source share

Yes, you can print the message to the console without using main ().

Create a test using JUnit and run it:

 @Test public printTest() { System.out.println("myprint"); } 
0
Aug 22 '17 at 20:52
source share



All Articles