---
title: "How can I execute any program in java without main method?"  
description: "How can I execute any program in java without main method?"  
author: "Anonymous User"  
published: 2011-07-23  
updated: 2011-07-23  
canonical: https://www.mindstick.com/forum/276/how-can-i-execute-any-program-in-java-without-main-method  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# How can I execute any program in java without main method?

Hi,

I have a small [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) [while creating](https://www.mindstick.com/forum/105382/which-constraints-we-can-use-while-creating-a-database-in-sql) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program). This might be a silly [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) as I am fresher in [JAVA](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming). I want to ask that\
is it possible to run any program in java which have not main method. Like we can run some program in c and [c++](https://www.mindstick.com/blog/745/array-in-c-plus-plus) which\
doesent have main method. Is it possible in java.

Thanks.

## Replies

### Reply by James Smith

Hi awadhendra,

You can use static block to perform this task and at the end of statements called System.exit(0) to avoid to generate an exception.

```
class MainMethodNot{    static    {        System.out.println("This java program have run without the run method");        System.exit(0);            }}
```

Thanks.


---

Original Source: https://www.mindstick.com/forum/276/how-can-i-execute-any-program-in-java-without-main-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
