7

I want to create hello world java 9 application and start it in intellij idea.

Now I have following structure:
enter image description here

content of inner module-info.java:

module my.module.Second {
    requires my.module.First;
}

content of outer module-info.java:

module my.module.First {
    exports my.pack;
}

But idea complains about my project:

Error:(1, 1) java: too many module declarations found

I don't understand why it happens and what really wrong. So

Question:

My question is how to force idea to accept my hello world.

P.S. From the first glance error looks obvious but I have project which I downloaded from github with the same structure but it works properly and idea doesn't complain about it:

enter image description here

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • A simple google on the error turned up this which seems to match your problem: https://www.logicbig.com/tutorials/core-java-tutorial/modules/modes.html – Gimby May 24 '19 at 13:13
  • @Gimby, according that link idea doesn't add **--module-source-path** How can ask idea to do it? – gstackoverflow May 24 '19 at 19:00
  • @gstackoverflow Just to be on the same page, can you link a GitHub URL of your project to reproduce this error? I wasn't able to reproduce it while trying to build a project from scratch. It's also, worth mentioning the version of IntelliJ which I suspect could possibly resolve things under the cover as well in terms of reproducing the error. – Naman May 27 '19 at 15:30
  • @Naman, https://github.com/gredwhite/jigsaw_first – gstackoverflow May 28 '19 at 09:14
  • @Naman, my intellij version is 2019.1.1 – gstackoverflow May 28 '19 at 09:15
  • @Naman, looks like my idea is not ready for modules. I tried to remove files from local PC and cloned them from github and now I don't see any error until moment of application start.But When I start app I see: **Error occurred during initialization of boot layer java.lang.module.FindException: Module my.module.Second not found** – gstackoverflow May 28 '19 at 09:44
  • @gstackoverflow maybe for once Invalidate Cache and Restart IJ and then try afresh. Though both the exceptions are valid enough, to be sure that they are reproducible at least. – Naman May 28 '19 at 09:47
  • 1
    @Naman i tried it and it doesn't help. Eventually I was able to find a solution. I was comparing 2 projects and tried to find difference. So the root cause that file jigsaw_first\.idea\misc.xml has to has ** inside java 11 * – gstackoverflow May 28 '19 at 10:07
  • @Naman what about your experience? Have you tried to import -> start application? – gstackoverflow May 28 '19 at 10:10
  • @gstackoverflow Not yet, but if its resolved, doesn't seem much worth as well. Yet, would try it out once I get some time. – Naman May 28 '19 at 10:11
  • @Naman, I think my solution looks like a dirty hack so I can't consider it as 'good solution'. Ít would be worth to know concrete steps how to import project from sources – gstackoverflow May 28 '19 at 10:15
  • I found that the last mentioned error happened because of unfilled project compiler output: http://dl4.joxi.net/drive/2019/05/28/0005/3037/338909/09/4cf054271a.jpg (it was unfilled initially) But it looks like intellij bug because from my point of view it should be filled by default – gstackoverflow May 28 '19 at 12:36
  • @gstackoverflow I did finally spend some time cloning the repository. The problem looked like marking sources as root and then ensuring that the module created other than the src folder was actually registered with IntelliJ. Not really sure still if this is IntelliJ which is the cause or the way the project was initialized. – Naman May 29 '19 at 04:33
  • @Naman thax for your effort. So you had to do some non obvious manipulation with settings before application was able to start. It looks like redundant action and Intellij may implement it in a better way – gstackoverflow May 29 '19 at 16:47

2 Answers2

4

According to Oracle Jar file specification

A modular JAR file is a JAR file that has a module descriptor, module-info.class, in the top-level directory (or root) directory.

One Jar could contain only one module.

To fix you problem you have to split your project (create several maven modules)

Peter Gyschuk
  • 919
  • 8
  • 12
  • I don't use maven at this example. Please, read comments to the topic – gstackoverflow Jun 03 '19 at 07:18
  • Creating several maven modules was additional option. Main idea is creating sepatate jar for each module – Peter Gyschuk Jun 03 '19 at 20:18
  • @gstackoverflow in general, better use maven for the build infrastructure and "best" practices. Instead you could make separate IDEA projects, with more than one jar. Or look in your working project how they make different jars. – Joop Eggen Apr 25 '22 at 06:33
0

Just import your Java modules.

From the main menu, select File | New | Module from Existing Sources.

Select the directory in which your sources, libraries, and other assets are located and click Open.

In the dialog that opens, select Create module from existing sources if you want to create a new module from the existing source code.

Reference: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html#import-module-from-sources

Danny Fang
  • 3,843
  • 1
  • 19
  • 25