简体   繁体   English

无法使用 Intellij IDEA 运行 Java 代码

[英]Unable to run Java code with Intellij IDEA

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.我刚刚下载了 IDE,我想用它编辑我的第一个 Java 文件,我对创建整个项目不感兴趣,只是编辑单个文件。

So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.因此,我使用 Intellij IDEA 从桌面打开了该文件,因为我将其设置为打开 .java 文件的默认程序。

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).我已经安装了 Java 8 更新 45 64 位(我有一个 64 位操作系统)以及 Java 开发工具包 (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!我已经将全局IDE SDK设置为我的JDK安装,当它提示我时我也将其设置为我的项目SDK,但仍然无法使用运行和调试按钮!

Edit: I am also unable to run my file regardless of if its in a project or not.编辑:我也无法运行我的文件,无论它是否在项目中。

Edit 2: Screenshot of my project setup编辑 2:我的项目设置的屏幕截图项目设置

Move your code inside of the src folder.将您的代码移动到src文件夹中。 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. IntelliJ 仅将特定位置的文件识别为项目的一部分——即,蓝色文件夹内的任何内容都被特别视​​为源代码。

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) ).另外 - 虽然我看不到你所有的源代码 - 确保它是正确的 Java 语法,声明的类与文件相同,并且它有一个main方法(特别是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). IntelliJ 不会在没有main方法的情况下运行代码(相反,它不能- 它和 Java 都不知道从哪里开始)。

My classes contained a main() method yet I was unable to see the Run option.我的类包含一个main()方法,但我无法看到Run选项。 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.我的文件夹中的某些类没有main()方法,但我仍然看到这些类的 Run 选项。

right click on the "SRC folder", select "Mark directory as:, select "Resource Root".右键单击“SRC文件夹”,选择“标记目录为:”,选择“资源根目录”。

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.选择运行,运行,编辑配置,用加号按钮添加一个应用程序配置,给它起个名字(可以是任何名字),并在主类中写下主java类的全名,例如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,您可能还需要检查文件、项目结构、项目设置-项目,给它一个编译器输出的文件夹,最好是一个单独的文件夹,在 java 文件夹下,

Don't forget the String[] args in your main method.不要忘记 main 方法中的String[] args Otherwise, there's no option to run your program:否则,没有选项可以运行您的程序:

public static void main(String[] args) {

}

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.工作目录:它应该指向项目的 src 文件夹。 C:\\Users\\name\\Work\\ProjectName\\src 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.这是我遇到问题的地方,更正后,我可以看到该类的运行选项。

Something else that worked for me:其他对我有用的东西:

  1. Right click the folder in src containing your main右键单击 src 中包含主文件的文件夹
  2. You'll see an option "run 'file.main()'" with the run icon.您将看到带有运行图标的选项“运行‘file.main()’”。
  3. Click it, and then the run icon in the top right and bottom left will turn green from then on.单击它,然后右上角和左下角的运行图标将变为绿色。

Sometimes, patience is key.有时,耐心是关键。

I had the same problem with a java project with big node_modules / .m2 directories.我在一个带有大 node_modules / .m2 目录的 java 项目中遇到了同样的问题。
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.所以我等待索引完成,然后我才能运行我的主类。

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在此处输入图片说明

Last resort option when nothing else seems to work: close and reopen IntelliJ.当其他方法似乎都不起作用时,最后的选择是:关闭并重新打开 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.我的问题是恢复 Git 提交,这恰好将为项目配置的 Java SDK 更改为不再安装的 JDK 版本。 But fixing that back still didn't allow me to run the program.但是修复它仍然不允许我运行该程序。 Restarting IntelliJ fixed this重新启动 IntelliJ 解决了这个问题

-First Move Your Code Files in side the "src" Folder -首先将您的代码文件移到“src”文件夹中

-Make sure your Main method is declared like the following - 确保您的 Main 方法声明如下

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


       }
    }

then:然后:

  • Go to Project configurations转到项目配置
  • select Java application,选择 Java 应用程序,
  • check allow parallel run检查允许并行运行
  • and select your main class并选择您的主要课程

and it should work它应该工作

If you are just opened a new java project then create a new folder src/ in the man project location.如果您刚刚打开一个新的 java 项目,则在 man 项目位置创建一个新文件夹 src/

Then cut and paste all your package in that folder.然后将所有包剪切并粘贴到该文件夹​​中。

Then Right click on src directory and select option Mark Directory As > Sources Root .然后右键单击 src 目录并选择选项 Mark Directory As > Sources Root

If you use Maven, you must to declare your source and test folder in project directory.如果使用 Maven,则必须在项目目录中声明源和测试文件夹。

For these, click F4 on Intellij Idea and Open Project Structure.对于这些,单击 Intellij Idea 上的 F4 并打开项目结构。 Select your project name on the left panel and Mark As your "Source" and "Test" directory.在左侧面板上选择您的项目名称并标记为您的“源”和“测试”目录。

when I select "new project" - > java it opens a window with field "project_SDK:" there is "10(java version "10.0.1+10") preselected. Additional Libraries and Frameworks: groovy, kotlin/jvm, scala and in the bottom: use library: [no library selected] - there is no posibility to select after that: "error: library is not specified" 当我选择“新项目”-> java时,它将打开一个带有“ project_SDK”字段的窗口:预先选择了“ 10(java版本“ 10.0.1 + 10”)。其他库和框架:groovy,kotlin / jvm,scala和在底部:使用库:[未选择库]-之后没有选择的可能性:“错误:未指定库”

what can I do to run java code from intellij? 我该怎么办才能从intellij运行Java代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM