Error: Main.class found in top-level directory (unnamed package not allowed in module)
Asked Answered
T

5

8

Trying to update an application to Java 11 and after sorting through hell with modules I thought I had got rid of all the red errors and now I'm getting this one I've never seen before:

enter image description here

Looking around I've seen people suggest it is possible to do with the application structure:

enter image description here

or the module-info.java file:

enter image description here

Can anybody see what I need to do to get rid of this?

Edit: Error after moving Main.java to a package called 'main' and trying to run it:

enter image description here

Tifanytiff answered 23/9, 2018 at 20:3 Comment(4)
Your Main class isn't in any package (or to be precise, it uses unnamed package), and that's not allowed in modular app. Add package declaration to Main.java (e.g. "package main;").Xochitlxp
@Guest21 I get an error "Package main does not correspond to the file path"Tifanytiff
IDE should offer you to move the file - put mouse cursor on package name, hit Alt+Enter, and click on "Move to package main". Or move the file manually - make new package "main" and move the file into it.Xochitlxp
@Guest21 thanks, I created a new package called main and moved it in, I got a new error which again has me confused, mind taking a look? I put it in the OP.Tifanytiff
X
9

In order for a JavaFx to launch your app, it needs access to its main class, so you need to export the package in which the main class is located.

Add export declaration to module-info:

module Game.main {
    ...

    exports main;
}
Xochitlxp answered 23/9, 2018 at 20:47 Comment(0)
T
0

If your class is under (default package) :

Unrelated to the specific question.

You could encounter this error while trying to run a class for example HelloWorld.java inside the package (default package).

To solve the issue create a new package under the src folder (at least in a generic case) and give it a name then move your class into the new package and try running it again:

Right-lick on the class (under the named package) -> Run As -> Java Application.

Trommel answered 6/3, 2023 at 9:46 Comment(0)
H
0

If you encounter an error while trying to run a new file and add modules, and the error persists even after making changes, you can resolve it by following these steps:

Delete the New Main File: If the issue is related to the new Main file inside the src package, deleting this file may resolve the error.

Check in IntelliJ IDEA on MacBook: This solution is especially useful for users working with IntelliJ IDEA on a MacBook.

By removing the problematic Main file, you can often resolve issues related to module errors in your Java project.

Hydrargyrum answered 2/9 at 7:41 Comment(1)
Pardon me, but why should file Main.java be removed when it appears to be the actual class of the application? Do you mean remove it and then re-create it?Conlan
S
-2

I was experiencing this Exception when trying to run my javafx application with the javafx-maven-plugin after adding some new classes.

And executing mvn clean solved the issue. Then I successfully executed the application with mvn javafx:run

Showmanship answered 6/9, 2023 at 5:48 Comment(0)
W
-3

If you move a class to a different folder, before the class declaration line, you need to have a reference to the folder where this class is/should be located - telling the system where to look. For example:

//This line below is important
package main.java.main.Main;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
    }
}
Warfield answered 2/12, 2019 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.