Unable to run Java code with Intellij IDEA

后端 未结 12 2391
说谎
说谎 2020-12-14 05:18

I have just downloaded the IDE, 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 op

相关标签:
12条回答
  • 2020-12-14 05:48

    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
    0 讨论(0)
  • 2020-12-14 05:49

    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.

    0 讨论(0)
  • 2020-12-14 05:49

    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 讨论(0)
  • 2020-12-14 05:51

    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).

    0 讨论(0)
  • 2020-12-14 05:52

    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) {
    
    }
    
    0 讨论(0)
  • 2020-12-14 05:58

    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.

    0 讨论(0)
提交回复
热议问题