java.lang.ClassNotFoundException: com.example.Main in intelliJ and Maven build - java

Here's my code which sets up the server using Jersey+ Grizzly combination from here. I haven't modified anything. also, all my files in the package com.example.
package com.example;
A rough skeleton of the Main.java which has the main(String[] args) is:
public class Main {
public static void main(String[] args) throws IOException {
code;
}
}
Also, there are other .java classes which don;t have main method. The project is being built by Maven and I use IntelliJ. The two execution profiles I have are:
Working directory : Z:/IdeaProjects/com.example with command line arguements:
clean test and exec:java
I understand that i need to run my Main but I don't know how to do it via Maven+IntelliJ. Can someone please help me?

I'm not sure exactly what you are trying to do, but I usually just right click on the main method and then click on the Run option. If you haven't downloaded all your dependencies, then go ahead and click on the Maven Projects tab, on the right side of the screen and run the compile stage to download all of your dependencies.
If you attached the error message you are getting that might help us narrow down the issue you are having.

You need a directory per package component. The path should be com/example/Main, not com.example/Main.

Related

Class not found - JavaFX Native Packaging

I'm trying to deploy a program that I created but I'm having an error after the installation.
I used Native Packaging to package it as an EXE installer and when I click said installer, it says the main class cannot be found. Here are some screenshots of the error:
Why is it not finding the class?
This is not always the best method.
However, I had the same problem but it was solved.
First, if you are using kotlin,
Create a main class that will be the new entry point as a Java file.
The generated Java file should be as follows:
package {your_package};
import javafx.application.Application;
public class EP extends Main {
public static void main(String... args) {
Application.launch(args);
}
}
The Main should be the class that seems to be the current entry point.
Perhaps the Main is defined as open class Main: Application () {.
Set the EP class generated this time as the entry point (main class).
If exe is generated as -native image,
Please try to execute the generated exe, paying attention to the following points.
○ Make sure the generated directory does not contain spaces or multibytes.
○ Do not execute directly from USB etc. (Copy to PC and execute)
I ran into the exact same issue. I used maven and spring boot for the project. So after using maven to build the jar first then used org.springframework.boot.loader.JarLauncher for the -appclass option. Problem solved.

ECLIPSE : Error: Main method not found in class apple, please define the main method as: public static void main(String[] args)

I am beginner to java. This code is working absolutely fine in command prompt but here in eclipse it is not running !
class apple{
public static void main (String[] args){
System.out.println("Hello , World") ;
}
}
You need to select your Java Application to be run:
Right-click on your project root directory, Run As > Run Configurations.
On the left-side select Java Application, if there is a Configuration under 'Java Application', then select it, if not create it. Then under the 'Main' tab, select search and find the Main class in your project.
Then select Run.
Other reasons why it could be failing:
Project was not saved before running.
Eclipse glitch, try to restart.
This is also happens, when ever some jar file is missing in build path.
This can shown clearly in problems window in eclipse.

Maven and Intellij builds differently

I have a parent pom.
When I am on the parent and write mvn clean install my dropwizard application builds fine.
However, in Intellij it fails because it cannot find my config.yml file.
The problem is that I need to include my module directory in Intellij, but not in Maven. Why is that?
Running mvn install this works
public static void main(String[] args) throws Exception {
new CivilizationApplication().run(new String[]{"server", "src/main/resources/config.yml"});
}
In Intellij I must change to the following
public static void main(String[] args) throws Exception {
new CivilizationApplication().run(new String[]{"server", "civilization-rest/src/main/resources/config.yml"});
}
How can I configure Intellij, so it will work without specifying the module directory?
You could try setting your working directory in your run configuration...
I would also look into your pom.xml configuration to see if you can change the working source sets to use this directory upon importing the maven project.
Set Working directory to $MODULE_DIR$. Maven always uses $MODULE_DIR$, but you probably do not have $MODULE_DIR$ by default for IntelliJ Application executions.
You can just tell Intellij to do exactly the same thing Maven does when you start it.
You can do so by clicking on the drop down menu next to the "Launch App" button. There you can define a new Run configuration that uses Maven by clicking on the + to the left and then choosing Maven.
Then you can just enter clean install exec:java where it says "Command line" and it should work exactly like it does when you execute it manually from the command line. Debugging should work like this as well without any problem.
The problem here is that you are refering to a resource file by using a relative path instead of using the classpath.
So, instead of
new CivilizationApplication().run(new String[]{"server", "src/main/resources/config.yml"});
You better have this
new CivilizationApplication().run("server", getClass().getResourceAsStream("config.yml"));
(obviously, make sure to change the signature of the #run() method as well)
Just a thought but maybe it's to do with the Resource not being copied.
I'm on OSX so the exact terms may differ if you're on Windows but for me... in Preferences, select "Build, Execution, Deployment", then "Compiler", and try adding ";?*.yml" to the Resource patterns.

Error running Java program in Eclipse form Github

So I am very new to using Github with Eclipse. When I import a project into eclipse by using the HTTPS clone URL and try to run it, it gives me the error:
Error: Could not find or load main class UserEnviorment.Main
The project is stored in:
Macintosh HD > Users > Kyle > git
but the Eclipse workspace is somewhere else. Would this cause problems?
Here is the class I am trying to run if that helps any
package UserEnviorment;
import java.util.*;
public class Main
{
public static void main(String[] args) throws Exception
{
// Does stuff until user exits program.
}
}
I apologize if this is a stupid question, but any help is much appreciated!
Nevermind, I seem to have found a solution. If I delete the project in Eclipse but don't remove the files from my hard drive, I can then create a new Java project in Eclipse and uncheck the box "use default location" and navigate to where the Github project is stored.

Eclipse not finding a main method

Firstly, I know that similar questions have been asked and answered before, however none of the solutions worked in my case.
For some reason, eclipse keeps showing a launch error saying that no main method is found despite there being a public static void main(String[] args) in my main class. Could there be an issue in the class path? I'm really unsure whats causing it.
Try putting your java files under the "src" folder. Apparently they are all out of that folder and that could be the cause.
Go to Run->Run Configurations, select your project. In the Main tab, for Main class, give class name along with complete path of the class containing main function. This worked for me :)
Try to say at the beginning of the code:
package yourpackage; //<----------your package is the name of the
package you are in
The class havent automatically put the package comment at the beginning of the Code
because like the other say your structure is false.
Yours:
project -----> classes,src
Should be:
project ---->src ---->package----> class
I got the same error but with different reason. I changed my java build path libraries to 15.0.1 and tried to run and it failed after that I changed them back to 11.0.2 and it worked.

Categories

Resources