why is main(args: Array) allowed to be defined in more than one fileWhat is the difference between main and regular function?Error: Execution failed for task ':app:clean'. Unable to delete fileConfiguring Gradle with KotlinHow can I get my JUnit tests to compile and run in my Kotlin+Gradle project?Android Kotlin : Error Unresolved reference: DaggerAppComponentWhy does Kotlin allow having two classes with same name and package in different folders (i.e.: main and androidTest)?Unresolved reference when referencing Java code from Kotlin tests in Spring Boot project using GradleWhat is the difference between main and regular function?Why aren't “internal” types in the main source tree visible in my unit tests?Failsafe withFallback(): why kotlin compiler fails to infer lambda type?Can Gradle produce multiple Kotlin Native binaries (for one OS)?

What is the highest possible temporary AC at level 1, without any help from others?

Can the poison from Kingsmen be concocted?

Character descriptions

Arriving at the same result with the opposite hypotheses

How to hide an urban landmark?

Universal hash functions with homomorphic XOR property

How can this tool find out registered domains from an IP?

Recommended tools for graphs and charts

How is water heavier than petrol, even though its molecular weight is less than petrol?

1980s live-action movie where individually-coloured nations on clouds fight

C++ Arduino IDE receiving garbled `char` from function

Confusion around using "des" in sentences

Is the term 'open source' a trademark?

Should I give professor gift at the beginning of my PhD?

Find the limit of a multiplying term function when n tends to infinity.

How to deal with apathetic co-worker?

Thread Pool C++ Implementation

How Often Do Health Insurance Providers Drop Coverage?

What makes an item an artifact?

Why did the Herschel Space Telescope need helium coolant?

Should an arbiter claim draw at a K+R vs K+R endgame?

Frame failure sudden death?

Soft question: Examples where lack of mathematical rigour cause security breaches?

Can you see exclusive car models from other platforms when playing cross-platform?



why is main(args: Array) allowed to be defined in more than one file


What is the difference between main and regular function?Error: Execution failed for task ':app:clean'. Unable to delete fileConfiguring Gradle with KotlinHow can I get my JUnit tests to compile and run in my Kotlin+Gradle project?Android Kotlin : Error Unresolved reference: DaggerAppComponentWhy does Kotlin allow having two classes with same name and package in different folders (i.e.: main and androidTest)?Unresolved reference when referencing Java code from Kotlin tests in Spring Boot project using GradleWhat is the difference between main and regular function?Why aren't “internal” types in the main source tree visible in my unit tests?Failsafe withFallback(): why kotlin compiler fails to infer lambda type?Can Gradle produce multiple Kotlin Native binaries (for one OS)?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have defined main(args: Array) in two Kotlin files in the same package. The compiler does not complain. Why not?
I am using IntelliJ IDEA with Gradle



#file1.kt
package test
fun main(args: Array<String>)

#file2.kt
package test
fun main(args: Array<String>)









share|improve this question
























  • Why should it complain? These are two separate entry points... if you want to run the program you need to know exactly which one you want to use.

    – Moira
    Mar 24 at 17:23












  • @Moira Suppose you want to call one of them from a class in another package, how would you import it and make sure you don't import the other one? Other functions than main do cause an ambiguity error.

    – JB Nizet
    Mar 24 at 17:36











  • @Moira Can we define main() in the 2 files? Answer is not.

    – user1443721
    Mar 24 at 17:39












  • Fair enough, good point. I wonder what happens if you try to invoke main? (edit: you get an error)

    – Moira
    Mar 24 at 17:46







  • 1





    It's not supposed to complain when you have it in two different files. You can do the same with Java, because the main function always points to a Java class, not a package. The code compiles to file[num]Kt.java, which means two separate classes. It's by design, and is also valid in Java

    – Zoe
    Mar 24 at 18:59


















1















I have defined main(args: Array) in two Kotlin files in the same package. The compiler does not complain. Why not?
I am using IntelliJ IDEA with Gradle



#file1.kt
package test
fun main(args: Array<String>)

#file2.kt
package test
fun main(args: Array<String>)









share|improve this question
























  • Why should it complain? These are two separate entry points... if you want to run the program you need to know exactly which one you want to use.

    – Moira
    Mar 24 at 17:23












  • @Moira Suppose you want to call one of them from a class in another package, how would you import it and make sure you don't import the other one? Other functions than main do cause an ambiguity error.

    – JB Nizet
    Mar 24 at 17:36











  • @Moira Can we define main() in the 2 files? Answer is not.

    – user1443721
    Mar 24 at 17:39












  • Fair enough, good point. I wonder what happens if you try to invoke main? (edit: you get an error)

    – Moira
    Mar 24 at 17:46







  • 1





    It's not supposed to complain when you have it in two different files. You can do the same with Java, because the main function always points to a Java class, not a package. The code compiles to file[num]Kt.java, which means two separate classes. It's by design, and is also valid in Java

    – Zoe
    Mar 24 at 18:59














1












1








1








I have defined main(args: Array) in two Kotlin files in the same package. The compiler does not complain. Why not?
I am using IntelliJ IDEA with Gradle



#file1.kt
package test
fun main(args: Array<String>)

#file2.kt
package test
fun main(args: Array<String>)









share|improve this question
















I have defined main(args: Array) in two Kotlin files in the same package. The compiler does not complain. Why not?
I am using IntelliJ IDEA with Gradle



#file1.kt
package test
fun main(args: Array<String>)

#file2.kt
package test
fun main(args: Array<String>)






kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 18:59







user1443721

















asked Mar 24 at 17:18









user1443721user1443721

2141518




2141518












  • Why should it complain? These are two separate entry points... if you want to run the program you need to know exactly which one you want to use.

    – Moira
    Mar 24 at 17:23












  • @Moira Suppose you want to call one of them from a class in another package, how would you import it and make sure you don't import the other one? Other functions than main do cause an ambiguity error.

    – JB Nizet
    Mar 24 at 17:36











  • @Moira Can we define main() in the 2 files? Answer is not.

    – user1443721
    Mar 24 at 17:39












  • Fair enough, good point. I wonder what happens if you try to invoke main? (edit: you get an error)

    – Moira
    Mar 24 at 17:46







  • 1





    It's not supposed to complain when you have it in two different files. You can do the same with Java, because the main function always points to a Java class, not a package. The code compiles to file[num]Kt.java, which means two separate classes. It's by design, and is also valid in Java

    – Zoe
    Mar 24 at 18:59


















  • Why should it complain? These are two separate entry points... if you want to run the program you need to know exactly which one you want to use.

    – Moira
    Mar 24 at 17:23












  • @Moira Suppose you want to call one of them from a class in another package, how would you import it and make sure you don't import the other one? Other functions than main do cause an ambiguity error.

    – JB Nizet
    Mar 24 at 17:36











  • @Moira Can we define main() in the 2 files? Answer is not.

    – user1443721
    Mar 24 at 17:39












  • Fair enough, good point. I wonder what happens if you try to invoke main? (edit: you get an error)

    – Moira
    Mar 24 at 17:46







  • 1





    It's not supposed to complain when you have it in two different files. You can do the same with Java, because the main function always points to a Java class, not a package. The code compiles to file[num]Kt.java, which means two separate classes. It's by design, and is also valid in Java

    – Zoe
    Mar 24 at 18:59

















Why should it complain? These are two separate entry points... if you want to run the program you need to know exactly which one you want to use.

– Moira
Mar 24 at 17:23






Why should it complain? These are two separate entry points... if you want to run the program you need to know exactly which one you want to use.

– Moira
Mar 24 at 17:23














@Moira Suppose you want to call one of them from a class in another package, how would you import it and make sure you don't import the other one? Other functions than main do cause an ambiguity error.

– JB Nizet
Mar 24 at 17:36





@Moira Suppose you want to call one of them from a class in another package, how would you import it and make sure you don't import the other one? Other functions than main do cause an ambiguity error.

– JB Nizet
Mar 24 at 17:36













@Moira Can we define main() in the 2 files? Answer is not.

– user1443721
Mar 24 at 17:39






@Moira Can we define main() in the 2 files? Answer is not.

– user1443721
Mar 24 at 17:39














Fair enough, good point. I wonder what happens if you try to invoke main? (edit: you get an error)

– Moira
Mar 24 at 17:46






Fair enough, good point. I wonder what happens if you try to invoke main? (edit: you get an error)

– Moira
Mar 24 at 17:46





1




1





It's not supposed to complain when you have it in two different files. You can do the same with Java, because the main function always points to a Java class, not a package. The code compiles to file[num]Kt.java, which means two separate classes. It's by design, and is also valid in Java

– Zoe
Mar 24 at 18:59






It's not supposed to complain when you have it in two different files. You can do the same with Java, because the main function always points to a Java class, not a package. The code compiles to file[num]Kt.java, which means two separate classes. It's by design, and is also valid in Java

– Zoe
Mar 24 at 18:59













1 Answer
1






active

oldest

votes


















0














Would the compiler complain if it was Java? No.
Since main method is an entry point to your application, the problem can arise only when there is ambiguity in the application that you are trying to compose. In your question, you haven't provided any specifics about your application. Those 2 files can be completely unaware of each other for what it's worth.



It can be a problem at startup, but even that would rarely happen these days. Modern frameworks, like Spring Boot, have their own way to identify where to start your application, that is where the entry point is.
They make use of their specific MANIFEST files and context/class loaders and project structures.






share|improve this answer























  • It is different in Java. The main method in Java is defined inside the class. Here in Kotlin, the main function is top level. Also I do mention the 2 files are in the same package. Even though developers may not be aware it, the compiler will. Try define main() in both files to see what happens.

    – user1443721
    Mar 25 at 4:24












  • I did try to add a file beside my main application file and it still compiles and works without problem. You should provide more context probably. Everything compiles down to Java classes, the only difference with Kotlin is that the classes will have "Kt" in the end.

    – yuranos87
    Mar 25 at 8:25











  • did you try main() in the 2 files? I mean main(), not main(args: Array<String>)

    – user1443721
    Mar 26 at 3:20











  • Ah, got your point. But still, the problems only happen when I try to build a Spring Boot executable by calling bootRun or bootJar. If I only run gradle compileKotlin everything if fine. So we are back to the questions "Why should Kotlin compiler bother?"

    – yuranos87
    Mar 26 at 6:26











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326422%2fwhy-is-mainargs-arraystring-allowed-to-be-defined-in-more-than-one-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Would the compiler complain if it was Java? No.
Since main method is an entry point to your application, the problem can arise only when there is ambiguity in the application that you are trying to compose. In your question, you haven't provided any specifics about your application. Those 2 files can be completely unaware of each other for what it's worth.



It can be a problem at startup, but even that would rarely happen these days. Modern frameworks, like Spring Boot, have their own way to identify where to start your application, that is where the entry point is.
They make use of their specific MANIFEST files and context/class loaders and project structures.






share|improve this answer























  • It is different in Java. The main method in Java is defined inside the class. Here in Kotlin, the main function is top level. Also I do mention the 2 files are in the same package. Even though developers may not be aware it, the compiler will. Try define main() in both files to see what happens.

    – user1443721
    Mar 25 at 4:24












  • I did try to add a file beside my main application file and it still compiles and works without problem. You should provide more context probably. Everything compiles down to Java classes, the only difference with Kotlin is that the classes will have "Kt" in the end.

    – yuranos87
    Mar 25 at 8:25











  • did you try main() in the 2 files? I mean main(), not main(args: Array<String>)

    – user1443721
    Mar 26 at 3:20











  • Ah, got your point. But still, the problems only happen when I try to build a Spring Boot executable by calling bootRun or bootJar. If I only run gradle compileKotlin everything if fine. So we are back to the questions "Why should Kotlin compiler bother?"

    – yuranos87
    Mar 26 at 6:26















0














Would the compiler complain if it was Java? No.
Since main method is an entry point to your application, the problem can arise only when there is ambiguity in the application that you are trying to compose. In your question, you haven't provided any specifics about your application. Those 2 files can be completely unaware of each other for what it's worth.



It can be a problem at startup, but even that would rarely happen these days. Modern frameworks, like Spring Boot, have their own way to identify where to start your application, that is where the entry point is.
They make use of their specific MANIFEST files and context/class loaders and project structures.






share|improve this answer























  • It is different in Java. The main method in Java is defined inside the class. Here in Kotlin, the main function is top level. Also I do mention the 2 files are in the same package. Even though developers may not be aware it, the compiler will. Try define main() in both files to see what happens.

    – user1443721
    Mar 25 at 4:24












  • I did try to add a file beside my main application file and it still compiles and works without problem. You should provide more context probably. Everything compiles down to Java classes, the only difference with Kotlin is that the classes will have "Kt" in the end.

    – yuranos87
    Mar 25 at 8:25











  • did you try main() in the 2 files? I mean main(), not main(args: Array<String>)

    – user1443721
    Mar 26 at 3:20











  • Ah, got your point. But still, the problems only happen when I try to build a Spring Boot executable by calling bootRun or bootJar. If I only run gradle compileKotlin everything if fine. So we are back to the questions "Why should Kotlin compiler bother?"

    – yuranos87
    Mar 26 at 6:26













0












0








0







Would the compiler complain if it was Java? No.
Since main method is an entry point to your application, the problem can arise only when there is ambiguity in the application that you are trying to compose. In your question, you haven't provided any specifics about your application. Those 2 files can be completely unaware of each other for what it's worth.



It can be a problem at startup, but even that would rarely happen these days. Modern frameworks, like Spring Boot, have their own way to identify where to start your application, that is where the entry point is.
They make use of their specific MANIFEST files and context/class loaders and project structures.






share|improve this answer













Would the compiler complain if it was Java? No.
Since main method is an entry point to your application, the problem can arise only when there is ambiguity in the application that you are trying to compose. In your question, you haven't provided any specifics about your application. Those 2 files can be completely unaware of each other for what it's worth.



It can be a problem at startup, but even that would rarely happen these days. Modern frameworks, like Spring Boot, have their own way to identify where to start your application, that is where the entry point is.
They make use of their specific MANIFEST files and context/class loaders and project structures.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 19:32









yuranos87yuranos87

1,89822330




1,89822330












  • It is different in Java. The main method in Java is defined inside the class. Here in Kotlin, the main function is top level. Also I do mention the 2 files are in the same package. Even though developers may not be aware it, the compiler will. Try define main() in both files to see what happens.

    – user1443721
    Mar 25 at 4:24












  • I did try to add a file beside my main application file and it still compiles and works without problem. You should provide more context probably. Everything compiles down to Java classes, the only difference with Kotlin is that the classes will have "Kt" in the end.

    – yuranos87
    Mar 25 at 8:25











  • did you try main() in the 2 files? I mean main(), not main(args: Array<String>)

    – user1443721
    Mar 26 at 3:20











  • Ah, got your point. But still, the problems only happen when I try to build a Spring Boot executable by calling bootRun or bootJar. If I only run gradle compileKotlin everything if fine. So we are back to the questions "Why should Kotlin compiler bother?"

    – yuranos87
    Mar 26 at 6:26

















  • It is different in Java. The main method in Java is defined inside the class. Here in Kotlin, the main function is top level. Also I do mention the 2 files are in the same package. Even though developers may not be aware it, the compiler will. Try define main() in both files to see what happens.

    – user1443721
    Mar 25 at 4:24












  • I did try to add a file beside my main application file and it still compiles and works without problem. You should provide more context probably. Everything compiles down to Java classes, the only difference with Kotlin is that the classes will have "Kt" in the end.

    – yuranos87
    Mar 25 at 8:25











  • did you try main() in the 2 files? I mean main(), not main(args: Array<String>)

    – user1443721
    Mar 26 at 3:20











  • Ah, got your point. But still, the problems only happen when I try to build a Spring Boot executable by calling bootRun or bootJar. If I only run gradle compileKotlin everything if fine. So we are back to the questions "Why should Kotlin compiler bother?"

    – yuranos87
    Mar 26 at 6:26
















It is different in Java. The main method in Java is defined inside the class. Here in Kotlin, the main function is top level. Also I do mention the 2 files are in the same package. Even though developers may not be aware it, the compiler will. Try define main() in both files to see what happens.

– user1443721
Mar 25 at 4:24






It is different in Java. The main method in Java is defined inside the class. Here in Kotlin, the main function is top level. Also I do mention the 2 files are in the same package. Even though developers may not be aware it, the compiler will. Try define main() in both files to see what happens.

– user1443721
Mar 25 at 4:24














I did try to add a file beside my main application file and it still compiles and works without problem. You should provide more context probably. Everything compiles down to Java classes, the only difference with Kotlin is that the classes will have "Kt" in the end.

– yuranos87
Mar 25 at 8:25





I did try to add a file beside my main application file and it still compiles and works without problem. You should provide more context probably. Everything compiles down to Java classes, the only difference with Kotlin is that the classes will have "Kt" in the end.

– yuranos87
Mar 25 at 8:25













did you try main() in the 2 files? I mean main(), not main(args: Array<String>)

– user1443721
Mar 26 at 3:20





did you try main() in the 2 files? I mean main(), not main(args: Array<String>)

– user1443721
Mar 26 at 3:20













Ah, got your point. But still, the problems only happen when I try to build a Spring Boot executable by calling bootRun or bootJar. If I only run gradle compileKotlin everything if fine. So we are back to the questions "Why should Kotlin compiler bother?"

– yuranos87
Mar 26 at 6:26





Ah, got your point. But still, the problems only happen when I try to build a Spring Boot executable by calling bootRun or bootJar. If I only run gradle compileKotlin everything if fine. So we are back to the questions "Why should Kotlin compiler bother?"

– yuranos87
Mar 26 at 6:26



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326422%2fwhy-is-mainargs-arraystring-allowed-to-be-defined-in-more-than-one-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript