简体   繁体   中英

How to execute a .java file if it has more than one class in it?

Consider I am having Sample.java file with class A and class B with two main methods respectively. how to execute this file

A java file can not have two classes, unless one is inside the other.

In that case, the full name of the inner class will be something like somepackage.MainClass.InnerClass

You'll have to give a main class to the java program, so you'll end up choosing the main or the inner class.

That said, having a main in an inner class would be strange, to say the least.

A valid Java file is only allowed to have one public class and it must have the same name as the filename. Therefore open your cmd and run:

javac myclass.java           //Compile
java myclass arg1 arg2...    //Execute

A .java file can have other classes, but they aren't allowed to be public, so the execute command is not ambigious.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM