Reputation: 792
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.
Upvotes: 90
Views: 360314
Reputation: 1240
Right click the folder containing your source (src)
Select Mark Directory as → Source Root
Make sure your Main method is declared like this
public class Main { public static void main(String[] args) {} }
Upvotes: 0
Reputation: 1
File-->Settings-->Build, Execution, Deployment -->Compiler--> Java Compiler and change "target bytecode version". in my case, this is 11
Upvotes: 0
Reputation: 1
Go to file- Project Structure - Project and in sdk select your Java jdk file
Upvotes: 0
Reputation:
-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:
and it should work
Upvotes: 1
Reputation: 1149
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
Upvotes: 1
Reputation: 3547
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) {
}
Upvotes: 14
Reputation: 1796
I had the similar issue and solved it by doing the below step.
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.
Upvotes: 10
Reputation: 1
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.
Upvotes: 0
Reputation: 21
If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:
Upvotes: 1
Reputation: 3138
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.
Upvotes: 1
Reputation: 21
Something else that worked for me:
Upvotes: 1
Reputation: 665
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,
Upvotes: 37
Reputation: 35
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.
Upvotes: 0
Reputation: 1196
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:
Some of the classes in my folder don't have a main()
method, but I still see a Run option for those.
Upvotes: 89
Reputation: 106389
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).
Upvotes: 90