I can't run the program in intelliJ - java

I created a project unchecked (Create project form template)
and added new class named demo.
but when after compiling, I couldn't run the code intellij didin't show me result.
"Edit configuration" only appeared like this:
I don't know what to do :(
I saw someone run the code using Intellij without writing package.
So I don't think that it doesn't work because I didn't write package.
What can I do?

You are missing the String[] args parameter in your main method:
public static void main(String[] args) {
}

Add the args parameter to the main method. Then right click into the editor -> and select "Run demo.main()"

Related

Main class not found in project IntelliJ IDEA: Java Application

IntelliJ does not find a main class in my Java application project. The project was cloned from a git repository so had no run configuration. I go to Edit Configurations, add a new Application template, go to Main class: and it says "No matches found in project".
So, manually searching through the hierarchy I find the .java file that contains the main function but it will not accept it as the main class. I've pasted the file below to prove that it has the correct main function.
public class AdvanceWarsGameHandler implements IGame
{
private Image mImage;
private String mTitle;
public AdvanceWarsGameHandler()
{
mTitle = "Advance Wars Game";
mImage = new Image("/OffBrandCerealOopsAllCarries2-01.png");
}
//Game logic unrelated to graphics goes here
#Override
public void update(Game game, float deltaTime)
{
}
//Update, but for graphics
#Override
public void render(Game game, Renderer renderer)
{
renderer.drawImage(mImage, game.getInput().getMouseX(), game.getInput().getMouseY());
}
public static void main(final String args[])
{
//Creating and starting an instance of AdvanceWarsGameHandler
AdvanceWarsGameHandler advancewars = new AdvanceWarsGameHandler();
Game myGame = new Game(advancewars);
myGame.start();
}
public String getTitle()
{
return mTitle;
}
}
So the question is, why is the IntelliJ project not recognizing the main function in this file, or what is IntelliJ looking for as the "Main class" of an application?
Okay, hopefully this answer will help others who are unfamiliar with IntelliJ IDEA.
The solution came in two parts
Part 1: Missing compilation directory.
Since I did not create the project from new and instead I cloned a Git repository, there was no default compilation directory set up.
To access this in IntelliJ IDEA go to File -> Project Structure -> Project and set the "Project compiler output" so the project can actually compile.
Part 2: Setting up the modules
The original project was created in Eclipse which has packages. In order to get those packages to work in IntelliJ, I had to go to the Modules tab of the Project Structure menu and set my src and res folders as Source and Resource Folders. This allowed IntelliJ to find the main() function in my class and the program ran as expected.
This solved my problem though if any of you IntelliJ users out there can see anything bad about what I did to get it working, please comment.

IntelliJ - Java Main Class is missing when I create a new project

I have just started learning Java using an online course.
When I create a new project with IntelliJ, I don't have the Main class in the src folder. However, the instructor is saying IntelliJ should create the class "Main" by default.
HOW I SHOULD HAVE IT:
-"Name of the project"
-Idea
-src
-"com.myname"
-Main (the Class)
HOW I HAVE IT:
-"Name of the project"
-Idea
-src (which contains nothing)
Therefore, I have to create manually the class "Main" by clicking on "src" and creating a class named "Main" with this code inside:
Public class Main {
public static void main(String[] args) {
}
}
MY QUESTION: how can I set IntelliJ to automatically create the main class every-time a new create a new project?
Many thanks for your help.
Create the project from template:
Command Line App will add a class with the empty main method.

Java Eclipse error method not found in class

The error states Error main method not found in class. Please define the main method as:
public static void main(String[] args).
Eclipse was working for me a few days ago, but now it just gives me that message
an example would be
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World");
}
}
Check if exists any error in your project. Not just the compilation errors. Click on project and check the "Problems" view in Eclipse. You need to remove all of "Errors".
First clean your project Using Project-->Clean and then build it again.
Also make sure that your build path is properly set.
I ran into the same issue. Just clean your project..save your project and then run it.
Check whether you have created a class with the name "String". If you have created a class with the name "String", the compiler will not be able to resolve the conflict between 2 classes i.e. java.lang.String and your String class therefore it won't recognize the main method.
So just remove that String class or rename it.It happened to me also.. I resolved using this only.
I ran into this problem also when I moved the location of the main method in my program to another file in the same program. If this is what happened to you, the fix is to go to the top of Eclipse near the bug and play icons and change the "Run as..." field to "Java application" from whatever its previous location was.
Try to resturt Eclipse in a new workspace.
Maybe you are creating 2 classes in the same package or u have defined one of your defined class name as "String". If you have then change that classname to something else , because at that time compiler cannot differentiate between java.lang.String and your user defined class "String". So jst chage the name of that String class.

Run two main class instances from one project in InteliJ IDEA?

Hello I have a problem that I want to run two distinct instances in one project. One is in Receiver module and second one in Sender. When I try to select Main class for another module in Run/Debug application I get following error. Is it even possible to run like this in idea ? I know this worked in Visual studio when we created new sollution. How about here ?
Do your main method has String[] args?
Remember that a class to be executable must have the following method:
public static void main(String[] args) throws Exception{
}
the throwing part is optional

How can I rename my main class in the NetBeans IDE?

I am using NetBeans IDE and have created a new project. The new project includes a Main.java.
Is it necessary to have it named Main.java, or can I rename the Main.java into another name? If so, how can I do this? When I try to right-click Main.java, there is no rename option. I thought it was possible.
There should be a menu item called
Refactor --> Rename
Link from NetBeans Wiki
Your name can be anything, as long as you have a
public static void main(String[] args)
in it (This is only if you want this class to be the entry point when you run your application).
You can call the file <whatever you like>.java, but the name of the class in that file must match the new name you give to the file.
So if you rename the file to MyCoolClass.java, you also have to change the class from public class Main to public class MyCoolClass.
you can even have the simpler way,click on the file_name.java in the Projects panel,wait for a sec and click one more time (just as you rename any other folder/file).It will allow you to replace the new name and then it will popup a window asking whether you want to refactor or not...
you got to use the refractor, as it is the safest way to do such actions

Categories