Main class not found in project IntelliJ IDEA: Java Application - java

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.

Related

How should I implement an eclipse plugin to analyze source code and add markers to any errors it finds?

I am trying to make an eclipse plugin that takes in the name of a project and analyzes the code in any .java files in that project and adds markers (red squigglies underneath) to any errors according to a custom set of rules. For right now I'm just trying to figure out how to mark the source code by matching strings, I plan on adding a custom rule set later.
As a simple example if you wanted to mark any occurrence of of the String "Hello" in
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
How would you go about analyzing it and then marking the code inside the editor?

Eclipse Error: Could not find or load main class

I have an Interface class checked out from an svn project, and created an Interface_Tester class to preview the interface. However, when I try to run it in Eclipse I get the following error:
Error: Could not find or load main class Interface_Tester
Here is the code from the interface_tester class -
public class Interface_Tester {
public static void main(String[] args){
Interface test = new Interface();
}
}
I have seen a similar question asked on here but did not understand the answer. I need a step-by-step guide to the solution as I am a beginner. Thank you
UPDATE:
I tried running the Interface class itself and had the same error... All classes are being shown in the project explorer on the left so why is eclipse unable to locate any of them?
On the left side of Eclipse you have a project view. Try right-clicking your Interface_Tester from there and in a dropdown choose run. May be you you are trying to run something else

Android - Constructor not visible BUT *.java copied from old project

at work I got an old Android project that I am using for a new project. From the old project I copy
Sensor.java
in my new project. I do this simply with copy-paste in the Eclipse Package Explorer.
The Sensor.java contains a class with an empty constructor, both public:
public class Sensor { ...
public Sensor() {} ...}
However, when I try to call
Sensor sensor = new Sensor();
the compiler tells me "The constructor Sensor() is not visible", but the exact same thing worked in the old project... I'm clueless and I googled the problem but I can't solve it. Everything is public, so it should be visible, shouldn't it?

How to call a class from another project in Eclipse?

I'm using Eclipse and I have two different projects: A and B.
In project A, I have a class classA where I need to call a method methodB() from a class classB contained in the project B, how can I do that?
I've tried adding the project B to project A build path, but still doesn't work.
Thanks.
You need to add another project in "Project" tab, or add class folder of the project in "Libraries" tab ie you may try to add project B to the Run configuration used by project A. Go to the menu Run -> Run configurations, ther you can add the project B in the tab 'classpath' of your run configuration.
Here's an example that you may find helpful:
Project_1 has the following class:
ClassProjectOne.java which consists of:
public class ClassProjectOne {
private int m_Age;
private final int AGE_INPUT = 15;
public ClassProjectOne() {
setAge(AGE_INPUT);
}
public int getAge() {
return m_Age;
}
private void setAge(int age) {
m_Age = age;
}
}
Project_2 has the following class:
ClassProjectTwo.java which consists of:
public class ClassProjectTwo {
public static void main(String[] args) {
ClassProjectOne t = new ClassProjectOne();
System.out.println(t.getAge());
}
}
In order for this to work, you must right click Project_2 and click on Properties. Then click on Java Build Path -> Add... -> Select Project_1 -> OK. This sets a Java Build Path.
If your class is static there is no need to initialize a new instance of it.
Hope this helps.
I've just done what you're trying to do. I called my first project 'project1'. In this projects i have a package called 'package1' which in turn contains a class called 'Class1' containing a (public) static method called 'staticMethod'. I called my second project 'project2' with a class 'Class2' in 'package2'. I added project1 to the build path of project2 and then inserted the statement import package1.Class1 at the beginning of the class Class2.
Put the Project B on the Build path, then do a Clean project from Project Menu option and then use it.
Click in "A" --> Properties --> Build Path --> Projects ---> Add the Project ---> Ok

Java compiler can not find symbol - public class in same package

I've got a problem with a Main class not finding another class being public, in the same folder and the same package. Both classes are named as their files. Here is the part seeming to contain the problem:
The Interface:
package hanoi;
public interface Stack<E> {
...
}
The Over-Class:
package hanoi;
public class DefaultStack<E> implements Stack<E> {
...
}
The Used class:
package hanoi;
public class HanoiStack extends DefaultStack<HanoiDisk> {
public HanoiStack (int a){
for (int b = a; b > 0; b--){
HanoiDisk disk = new HanoiDisk(b);
this.push(disk);
}
}
...
}
Main Class:
package hanoi;
public class TowersOfHanoi{
HanoiStack stack1 = new HanoiStack(0);
HanoiStack stack2 = new HanoiStack(0);
HanoiStack stack3 = new HanoiStack(0);
...
}
File Directory (of both):
...\eclipse\Hanoi2\src\hanoi
Eclipse error: Main class could either not be found or not be loaded
(there is actually a main method in the main class, but the rest of the code gets very complicated and doesnt seem to be interesting right now)
Java Compiler error: could nor find symbol: class HanoiStack
Another hint: a friend of mine is working on the same project, seeming toi have declared the interesting part same as me but not having any issues.
Update: download link to the full program is here
Looks like either Eclipse playing up, or it cant compile the classes for some reason.
1) Clean the project in Eclipse. (Project -> Clean -> Clean all projects) Then restart Eclipse for good measure.
2) Check the folder where the project is configured to build is writable. To check what this is, view the project build path (right click -> Build Path -> Configure build path) under source tab check the output folder.
If neither of these help, could you provide more info where the main class is. E.g. is it in the TowersOfHanoi class?

Categories