20

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?

Mkr
  • 428
  • 2
  • 5
  • 16
  • 1
    Just create the class with the main method, right click and "Run 'your class'" i use intellij idea and should be the same in android studio. – Ringo Nov 25 '14 at 21:55
  • Ok it works - but as I see only when my project is well build - because this method invokes some gradle tasks. (I even tried it earlier - but on project with some errors and then it didn't work). Anyway - do I really need to run gradle to print "Hello world" on terminal :) ? – Mkr Nov 25 '14 at 22:09

7 Answers7

20

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

If there is the standard method to start Java application

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.

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

halfer
  • 19,824
  • 17
  • 99
  • 186
erluxman
  • 18,155
  • 20
  • 92
  • 126
11

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

Ross Hambrick
  • 5,880
  • 2
  • 43
  • 34
  • Thanks for answer. I didn't think about that because I just wanted to fast test something small inside my files, but you partially solved my problem the same as luisdurazoa and you get +25. – Mkr Nov 25 '14 at 22:31
11

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

enter image description here

kevinweber
  • 606
  • 9
  • 11
8
  1. Create the class with main() method:

    public class Test1 {
    
      public static void main(String[] args) {
        System.out.println("hello1");
      }
    }
    
  2. Hit ctrlshiftF10 (or ctrlshiftR for Mac)

It will compile, assemble and print hello1

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Andrew
  • 36,676
  • 11
  • 141
  • 113
3

Solution:

  1. Execute run * * main() with coverage;

  2. Modify gradle.xml under. Idea

< GradleProjectSettings> Add a row under the label node

< option name=”delegatedBuild” value=”false” />

https://programmerah.com/tag/android/page/2/

3

Solution:

  • 1.Create a java class in any package of your project with Android Studio.
  • 2.Provide a main() method:
    package com.example.demo;

    public class test {
        public static void main(String[] args) {
            System.out.println("hello,java");
        }
    }
  • 3.Avoid use targetSdk 31 in build.gradle:
    defaultConfig {
        applicationId "com.example.demo"
        minSdk 28
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
  • 4.Configure /${yourproject}/.idea/gradle.xml:
<GradleProjectSettings>
        <option name="delegatedBuild" value="false" />
        ....
</GradleProjectSettings>
  • 5.Sync your project with Gradle Files;
  • 6.Last,you can Run single java.
Ta0Ta0Ta0
  • 31
  • 3
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 06:29
1

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!
Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53