使用标准 main(String [] args) 方法运行单个 java 文件 - Android Studio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27137479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:18:24  来源:igfitidea点击:

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

javaandroidandroid-studio

提问by Mkr

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-terminal answers :) - if they are possible ) in Android Studio. I mean random .java file from my Android project in which I'll write main(..) method. And there's one more thing: I don't want to create any new modules(I've already seen answers about adding Java library module). I just want to click something and run my (single) file, the same way as I can do it in Eclipse. Is it possible?

我想找到在 Android Studio 中运行单个 Java 文件的最简单方法(并且更喜欢非终端答案:) - 如果可能的话)。我的意思是我的 Android 项目中的随机 .java 文件,我将在其中编写 main(..) 方法。还有一件事:我不想创建任何新模块(我已经看到了有关添加 Java 库模块的答案)。我只想单击某些内容并运行我的(单个)文件,就像在 Eclipse 中一样。是否可以?

采纳答案by Ross Hambrick

If nothing else, you can make a quick JUnit test that calls your class's main method.

如果不出意外,您可以进行一个快速的 JUnit 测试来调用您的类的 main 方法。

回答by erluxman

One thing that might be confusing you, like it was confusing me:

一件事可能会让你感到困惑,就像它让我感到困惑一样:

If there is the standard method to start Java application

如果有启动Java应用程序的标准方法

public static void main (String[] args ) {
// your block here

}

Android Studio will automatically give an option "Run YourClass.mainActivity()", when you right-click anywhere in the editor's editing space.

当您右键单击编辑器编辑空间中的任意位置时,Android Studio 将自动提供一个选项“Run YourClass.mainActivity()”。

Just right click in the Java file and there will be an option to run that particular Java class.

只需右键单击 Java 文件,就会出现运行该特定 Java 类的选项。

回答by kevinweber

The easiest way (for me) is to do a right clickin your editor and select "Run ClassName.main()". See screenshot. Using Android Studio version 1.4.1.

最简单的方法(对我而言)是在编辑器中右键单击并选择“运行 ClassName.main()”。见截图。使用 Android Studio 1.4.1 版。

enter image description here

在此处输入图片说明

回答by Andrey

1) Create the class with main() method:

1) 使用 main() 方法创建类:

public class Test1 {

    public static void main(String[] args) {
        System.out.println("hello1");
    }
}

2) hit ctrl+shift+F10 (or ctrl+shift+R for Mac)

2) 按 ctrl+shift+F10 (或 ctrl+shift+R for Mac)

It will compile, assemble and print

它将编译、汇编和打印

hello1

你好1

回答by Abhishek Kumar

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.

如果您使用的是 JAVA,则需要按照@Andrey 的说明进行编写。

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

如果您使用 Kotlin,则可以简单地编写函数而无需创建类。

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

This will work.

这将起作用。

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

但是,请记住,函数名只能是main并且不要在这个 main 函数中使用任何 Android JDK 的一部分。

Ex:- if you write

例如:- 如果你写

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

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

现在因为 Log 是 Android 的一部分,你会得到运行时异常说

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