r/javahelp • • Sep 16 '22

Intellij is super confusing

I've been having difficulty making java applications in IntelliJ , even something as simple as a Hello World. I'm trying to figure out why this message "java: class HelloMarsApp is public, should be declared in a file named Hello MarsApp.java" keep appearing when I build it. I have the whole project saved in a folder name "HelloMarsApp.java, and have had no luck building it. any help is appreciated

0 Upvotes

18 comments sorted by

•

u/AutoModerator Sep 16 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

7

u/NautiHooker Software Engineer Sep 16 '22

Whats the file called that the class HelloMarsApp is declared in?

0

u/legal_lemons55 Sep 16 '22

it goes

src (folder)

>Main.java (class)

>> HelloMarsApp (class)

11

u/NautiHooker Software Engineer Sep 16 '22

You can only have one public class per file.

You should create another file called HelloMarsApp.java in your src folder and put the HelloMarsApp class inside it.

1

u/codereign fallible moderator Sep 16 '22

I think he's describing a static inner class which is fine. Maybe his syntax is wrong 🤔

8

u/wildjokers Sep 16 '22

I'm trying to figure out why this message "java: class HelloMarsApp is public, should be declared in a file named Hello MarsApp.java" keep appearing when I build it.

The error message seems to be pretty clear. Your class named HelloMarsApps needs to be in a file named HelloMarsApp.java. Note that IntelliJ should be offering to rename the file for you. Not in a folder named that, but the actual file.

3

u/[deleted] Sep 16 '22

The name of the class MUST match the name of the source file.

A file named Main.java can only contain a class named Main.

7

u/SlashdotDiggReddit Sep 16 '22

Try using Microsoft Visual Studio ... you'll be thankful for Intellij.

2

u/ThunderEcho100 Sep 16 '22

I’m not a developer but do some maven support. Thought I was the only one who didn’t like VS over IntelliJ.

1

u/stevesobol Sep 16 '22

Yeah, Microsoft has never been particularly good with UI/UX. Visual Studio makes my head hurt. So does Office

2

u/otondonicolas Sep 17 '22

Wash your mouth before talk about IntelliJ! 😂.

Yes, it’s a little bit confusing at the begging but I promise with time you will understand it and love it.

1

u/Orffyreus Sep 17 '22

Except this unresolved (11 years old) issue ;-)

https://youtrack.jetbrains.com/issue/IDEA-68854

1

u/otondonicolas Sep 17 '22

Well nothing is perfect. Just live with that. It’s not for nothing the first IDE around Java/Kotlin developers

1

u/SmallPlayz Sep 17 '22

File name should equal class name. Edit: error

1

u/hey-im-root Sep 16 '22

try some java tutorials before trying to just jump in on an IDE

1

u/SmallPlayz Sep 17 '22

Yeah using notepad ofc

0

u/hey-im-root Sep 17 '22

eh, maybe vscode or vim, unless notepad has java extensions lol, and it probably does tbh. been a few years since i’ve used it. regardless OP should do some basic java tutorials before trying to do anything with compiling his own code