87

I have just downloaded the IntelliJ IDEA, and I want to edit my first Java file with it, I'm not interested in creating a whole project, just editing the single file.

So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.

I write some code and the main run and debug buttons are greyed out! I can't run my code!

I have already installed Java 8 update 45 64-bit (I have a 64 bit OS) as well as the Java Development Kit (J8U45). I have set my global IDE SDK as my JDK installation, and when it prompts me I also set this as my project SDK, but still the run and debug buttons are unable to be used!

Edit 1:
I am also unable to run my file regardless of if its in a project or not.

Edit 2:
Screenshot of my project setup. Larger Project Setup Run Icons Project Setup

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Adam Griffiths
  • 680
  • 6
  • 26
  • 60
  • 4
    Create a project, then create a Java module. Put your class there and execute it. If you only want/need the IDE for editing the Java file, edit the file accordingly, then compile it using `javac` and execute the class using `java WhateverTheNameOfYourClass`. – Luiggi Mendoza Jun 18 '15 at 19:19
  • I also tried that, but when I did I was unable to create a Java file, the closest is a Java class file (its a small project so I will only need one file and not multiple classes) and it said unable to create class-file. – Adam Griffiths Jun 18 '15 at 19:19
  • Try adding a source root in your project. Put your java file under that – vikingsteve Jun 18 '15 at 19:23
  • What is javac? It seems weird that I have to compile my program every time I want to run it, with other programming languages I've been able to run the code in the IDE on the fly without compiling it. I have also put a source root in and it still won't let me run the file. – Adam Griffiths Jun 18 '15 at 19:25
  • Are you sure you have a Java **Module** and that the proper JDK is configured in this **Project**? – Luiggi Mendoza Jun 18 '15 at 19:25
  • [javac](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html) – Luiggi Mendoza Jun 18 '15 at 19:25
  • Do I really need to compile my code every time I want to run it using javac? I thought the point was to be able to test code on the fly? – Adam Griffiths Jun 18 '15 at 19:30

13 Answers13

87

Move your code inside of the src folder. Once it's there, it'll be compiled on-the-fly every time it's saved.

IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.

Also - while I can't see all of your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a main method (specifically public static void main(String[] args)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 5
    Done and I updated the screenshot to reflect the new file movement. However the run command is still unusable, you can't see it as I have not opened the menu so you can see my code instead. – Adam Griffiths Jun 18 '15 at 19:38
  • Be sure that you have the module for the project set to the Java JDK. Otherwise, try putting a break point on the only executable line in the code and enter debug mode. If that works, then you're capable of running the code (and if you do run this you won't get any output since nothing's being printed). – Makoto Jun 18 '15 at 19:51
80

My classes contained a main() method yet I was unable to see the Run option. That option was enabled once I marked a folder containing my class files as a source folder:

  1. Right click the folder containing your source
  2. Select Mark Directory as → Test Source Root

Some of the classes in my folder don't have a main() method, but I still see a Run option for those.

William Price
  • 4,033
  • 1
  • 35
  • 54
MamathaMacherla
  • 1,106
  • 7
  • 8
36

right click on the "SRC folder", select "Mark directory as:, select "Resource Root".

Then Edit the run configuration. select Run, run, edit configuration, with the plus button add an application configuration, give it a name (could be any name), and in the main class write down the full name of the main java class for example, com.example.java.MaxValues.

you might also need to check file, project structure, project settings-project, give it a folder for the compiler output, preferably a separate folder, under the java folder,

Matthew E
  • 625
  • 6
  • 6
  • 11
    I think that "src" folder should be marked as "Source root" and not "Resource root". But other than that, that is the correct answer. – arenaq Nov 16 '18 at 01:23
  • Actually I found that `src/main/java` should be marked as the Sources Root. Then `src/main/resources` is the Resources Root, and `src/test/java` and `src/test/resources` are the Test Sources Root and Test Resources Root, respectively. – Ethan T Jul 15 '19 at 17:57
  • I cloned a project from Github and it didn't set the folder to source, so once I done it manually. Then I was able to configure the compiling options – testing_22 Jan 10 '22 at 22:59
13

Don't forget the String[] args in your main method. Otherwise, there's no option to run your program:

public static void main(String[] args) {

}
meyi
  • 7,574
  • 1
  • 15
  • 28
Ken Ratanachai S.
  • 3,307
  • 34
  • 43
8

I had the similar issue and solved it by doing the below step.

  1. Go to "Run" menu and "Edit configuration"
  2. Click on add(+) icon and select Application from the list.
  3. In configuration name your Main class: name of your main class.
  4. Working Directory : It should point till the src folder of your project. C:\Users\name\Work\ProjectName\src

    This is where I had issue and after correcting this, I could see the run option for that class.

iampranabroy
  • 1,716
  • 1
  • 15
  • 11
1

Something else that worked for me:

  1. Right click the folder in src containing your main
  2. You'll see an option "run 'file.main()'" with the run icon.
  3. Click it, and then the run icon in the top right and bottom left will turn green from then on.
1

Sometimes, patience is key.

I had the same problem with a java project with big node_modules / .m2 directories.
The indexing was very long so I paused it and it prevented me from using Run Configurations.

So I waited for the indexing to finish and only then I was able to run my main class.

Arnaud Claudel
  • 3,000
  • 19
  • 25
1

If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:

  1. Click on add icon and select Application from the list.
  2. In configuration name your Main class: as your main class name.
  3. Set working directory to your project directory.
  4. Others: leave them default and click on apply. Now you can run your program.enter image description here
1

Last resort option when nothing else seems to work: close and reopen IntelliJ.

My issue was with reverting a Git commit, which happened to change the Java SDK configured for the Project to a no longer installed version of the JDK. But fixing that back still didn't allow me to run the program. Restarting IntelliJ fixed this

Kartik Chugh
  • 1,104
  • 14
  • 28
1

-First Move Your Code Files in side the "src" Folder

-Make sure your Main method is declared like the following

   public class Main {
         public static void main(String []args){


       }
    }

then:

  • Go to Project configurations
  • select Java application,
  • check allow parallel run
  • and select your main class

and it should work

user254694
  • 1,461
  • 2
  • 23
  • 46
  • This was a good hint for me but it seems like this may be for a different version than I have but I'm not sure. I clicked an "Add Configuration" button near the top right of the app. From there I selected "Application" then filled out the project name and the specified the `main` class. I also checked the "store as project file" option (to save the settings, perhaps). After clicking Apply I am now able to run the project. Yay! – Nathan Mar 17 '21 at 01:26
0

If you are just opened a new java project then create a new folder src/ in the man project location.

Then cut and paste all your package in that folder.

Then Right click on src directory and select option Mark Directory As > Sources Root.

0

If you use Maven, you must to declare your source and test folder in project directory.

For these, click F4 on Intellij Idea and Open Project Structure. Select your project name on the left panel and Mark As your "Source" and "Test" directory.

0

Go to file- Project Structure - Project and in sdk select your Java jdk file