无法使用Intellij IDEA运行Java代码

时间:2022-04-21 16:41:23

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

我已经安装了64位Java 8更新45(我有64位操作系统)以及Java开发工具包(J8U45)。我已将我的全局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 无法使用Intellij IDEA运行Java代码

编辑2:我的项目设置的屏幕截图

5 个解决方案

#1


45  

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

将代码移到src文件夹中。一旦它存在,它将在每次保存时即时编译。

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)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).

另外 - 虽然我看不到你的所有源代码 - 确保它是正确的Java语法,声明的类与文件相同,并且它有一个main方法(特别是public static void main(String [] args) )。 IntelliJ不会在没有main方法的情况下运行代码(相反,它不能 - 它既不知道也不知道从哪里开始)。

#2


22  

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:

我的类包含main()方法,但我无法看到Run选项。一旦我将包含我的类文件的文件夹标记为源文件夹,该选项就启用了:

  1. Right click the folder containing your source
  2. 右键单击包含源的文件夹

  3. Select Mark Directory as → Test Source Root
  4. 选择标记目录为→测试源根

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

我的文件夹中的某些类没有main()方法,但我仍然看到这些类的Run选项。

#3


8  

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文件夹下,

#4


4  

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

不要忘记main方法中的“String [] args”。否则,没有选项来运行您的程序。

public static void main(String[] args){

}

#5


0  

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。

#1


45  

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

将代码移到src文件夹中。一旦它存在,它将在每次保存时即时编译。

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)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).

另外 - 虽然我看不到你的所有源代码 - 确保它是正确的Java语法,声明的类与文件相同,并且它有一个main方法(特别是public static void main(String [] args) )。 IntelliJ不会在没有main方法的情况下运行代码(相反,它不能 - 它既不知道也不知道从哪里开始)。

#2


22  

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:

我的类包含main()方法,但我无法看到Run选项。一旦我将包含我的类文件的文件夹标记为源文件夹,该选项就启用了:

  1. Right click the folder containing your source
  2. 右键单击包含源的文件夹

  3. Select Mark Directory as → Test Source Root
  4. 选择标记目录为→测试源根

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

我的文件夹中的某些类没有main()方法,但我仍然看到这些类的Run选项。

#3


8  

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文件夹下,

#4


4  

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

不要忘记main方法中的“String [] args”。否则,没有选项来运行您的程序。

public static void main(String[] args){

}

#5


0  

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。