Run single java file with standard main(String [] args) method - Android Studio

后端 未结 5 1007
青春惊慌失措
青春惊慌失措 2020-12-17 16:16

To start with - Yes, I know that this is crazy/strange. But I need it :).

I want to find simpliest way to run single java file (and prefer non-termi

5条回答
  •  甜味超标
    2020-12-17 16:54

    This may be too late but this might help other developers,

    If you're using JAVA, you need to write it as told by @Andrey.

    If you're using Kotlin, you can simply write a function without making a Class.

    fun main(){
         println("you")
    }
    

    This will work.

    But, remember, function name should only be main and don't use anything that is part of Android JDK inside this main function.

    Ex:- if you write

    fun main(){
       Log.e("you","you")
    }
    

    Now as Log is part of Android, you'll get runtime exception saying

    Exception in thread "main" java.lang.RuntimeException: Stub!
    

提交回复
热议问题