• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple main methods in a file  RSS feed

 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know if we add different classes in a file and each class will have a main method. On what basis it will create a class file name and which class we will be able to run from command line?

example:


Like file name is main.java.
When i compile it from command prompt it will create main.class file and i will only be able to run java main not anothermain class but when i use eclipse it will ask which class you want to use by default.

and i when i skip final keyword in main class command prompt will create 2 class files

Can anyone tell what is happening in JVM when i do this kind of experiments :p [Sorry for not following java conventions for class name ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linkon Manwani wrote:Like file name is main.java.
When i compile it from command prompt it will create main.class file and i will only be able to run java main not anothermain class ...

That's not true.

When you run a Java class with the java command in a command prompt window, you specify the class name and not the filename.

You can run anothermain with the command: java -cp . anothermain

Also, if you put those two classes in a file named main.java and you compile it with javac main.java, you will get two class files: main.class and anothermain.class.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Also, if you put those two classes in a file named main.java and you compile it with javac main.java, you will get two class files: main.class and anothermain.class.


It is happening if i will not include final keyword in one of the main method declarations
when i do mention final it will create only main.class file and run only with java main
gives error class not found in case of java anothermain
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I copied and pasted the code you posted and it compiled and ran with no errors. With and without final. Please show us exactly what is happening, including all instructions used for compiling.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i was compiling main.java file last night with javac in command prompt it is creating only main.class whereas file contains two classes main [with final keyword] and anothermain [without final keyword]. It gave error anothermain class not found on command java anothermain .

But now i am compiling it is creating both class file named as main.java as well as anothermain.class with same code in my desktop.


when i compile this code online i.e Ideone.com it displays hello1
Compiled code at Ideone.com


 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using online compilers.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linkon Manwani wrote:But now i am compiling it is creating both class file named as main.java as well as anothermain.class with same code in my desktop.

I think you mean it creates main.class and anothermain.class.
BTW you shouldn't be creating .java files on your desktop, it far better practice to create a folder to hold your .java and .class files. It may be that last night it was creating an anothermain.class file but the desktop icon wasn't showing up where you expected it to (or may not have showed up at all without refreshing the desktop).

Linkon Manwani wrote:
when i compile this code online i.e Ideone.com it displays hello1
Compiled code at Ideone.com



What were you expecting to happen and why?
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the tip .may be that was the problem,,,

Tony Docherty wrote:
What were you expecting to happen and why?


I am expecting The output from both main methods i.e. hello1 and hello both
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't get both main methods called. The instruction is to call one main method; obviously IDEone calls the first found. You will have to compile the code on our own computer and use
javac main.java
java main
java anothermain

By the way: please remind yourself of the naming conventions for Java®.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly keep few things in mind
.class file of every class gets created by the name of class (even for the complex nested ones too)
Either main is in it or not in it.Don't give so much importance to main method its just a static method like any other static method
The only difference is that particular signature of it is identified by JVM
Even you can have multiple main method in a single class thanks to method overloading
Even you can explicitly call it like other static method
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
Even you can have multiple main method in a single class thanks to method overloading
Even you can explicitly call it like other static method

I didn't knew that, thanks a lot for this precious information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!