I am a java beginner, the first java IDE I downloaded was Visual Studio Code, it was very easy to use and everything is auto configured. But it kind overheats my laptop all the time, so I want to try IDEA, so far it's a very good experience, except when I open a java file and tried to run it in IDEA, it always pops out this run configuration window and I don't understand how to configure it. In visual Studio Code I can open any java file any time and run without any issues, but now I have to go through creating projects every time. Is there any solution for this?
CodePudding user response:
From how the file icon looks:
your file is not recognized as the part of the sources of your project. Check the project settings to ensure that source directories are correctly set.
I'd also recommend you to look up and follow the conventions for the directory structure of java projects.
Once you've fixed the problem with sources, you'll see "run" icon next to your class, main
method, or when you're right clicking the file.
CodePudding user response:
The IDE needs to know what's called the entry point of the program, i.e. where to start running your code. That's what the "Edit Configuration" window is wanting you to do. If your file "Lab3.java" is in a package, make sure to fully specify that in the field you have in red. Otherwise without knowing how your project is structured (as the other answer alludes to), it's difficult to pinpoint what we're missing here.
CodePudding user response:
When you create your IntelliJ project, add a directory /src
right at the root of your project. Right click on that folder and tell IntelliJ that you wish to mark it as a source root. The directory should turn blue in color.
Put your packages under /src
. IntelliJ will know that those are Java files.
When you want to run a class with a main method, choose Run->Edit Configurations. Tell IntelliJ that you want to add an Application. It should prompt you with the classes that have main
methods in them. You'll have no trouble running them.
CodePudding user response:
JShell
If you just want to run a few lines of Java, try
Down in the Run pane, you should see the results, the Hello World!
text.
At this point you can add your single file to the org.example
package seen in the Project pane.
By the way, you can change that package name by context-clicking and choosing Refactor > Rename….
Later, learn to use the Run/debug configurations feature of IntelliJ.
Know that you need not create a new project for each time you want to do a little experiment. Create one project for such experiments. Keep adding new .java
class files for each experiment. Delete old class files you no longer need.
Eventually, I suggest updating the versions of various items in your POM. The QuickStart archetype is not configured for the latest versions (for reasons I cannot fathom).
And when you learn about unit testing, replace JUnit 4 in the POM with JUnit Jupiter (Aggregator) to use JUnit 5. One of the benefits of using Maven is that you can easily switch out dependencies such as going from JUnit 4 to JUnit 5.