[英]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:一旦我将包含我的类文件的文件夹标记为源文件夹,就启用了该选项:
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.我遇到了类似的问题并通过执行以下步骤解决了它。
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:其他对我有用的东西:
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-:如果您无法运行正确的程序并尝试所有其他答案。单击“编辑配置”并执行以下步骤:
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:然后:
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.