Java 9 compiles fine in maven but not in intellij - java

I wanted to update my project from java 8 to java 10. Hitting the well known problems of java 9 module system. However after some fiddling, researching and back and forth I was able to compile everything in maven. I added a module-info.java to my project. See also this snippet of my pom.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>10</source>
<target>10</target>
<release>10</release>
<optimize>true</optimize>
<debug>true</debug>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.2</version>
</dependency>
</dependencies>
</plugin>
However Intellij is reporting 100 errors saying
unnamed module ready package X from both A and B
and similar. I'm using Intellij Community Edition 1018.1.5. I tested it in eclipse photon and there it compiles fine. I think that IntelliJ is putting the maven dependencies differently on the module path/classpath than how maven or eclipse puts them.
However, I'm at a loss and have no idea how to convince IntelliJ to compile my project.

I'd prefer to put this in a comment, since I can't really give an answer until I have more info, but you can't add screenshots to comments. In any case, did you tweak the appropriate settings under File > Project Structure > Project as in:
And also under File > Project Structure > Modules:

Related

maven compiler in intellij

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
What is the point of using maven in intellij if it dose not work without setting the correct JDK under various intellij options?
What I mean is that now with intellij I have to set the JDK in 3 different places.
File->Setting->Build->Compiler
File->Project Structure->Project
File->Project Structure->Modules
While I aspect expect that when i compiler on the right side where are the maven options it works just by watching the pom file.
i think that depends on what type of project you want to make but personally i find maven nice to use because you can set up several actions in the pom file (for example when compiling Less files, excluding them from the build and just using the resulting css files).
another feature would be the easy way to add dependency's from the maven rep http://mvnrepository.com/

Eclipse Maven Dependency - weaveDependency issue

I am new to maven but not to java or Eclipse. I imported an existing maven project into Eclipse and I am having trouble with one of the dependencies. it is listed in POM.xml with groupid of net.bootstrap.api , version 2.0.1
but maven can't find it on the central repository. it is a weaveDependency. if I change groupid to org.webjars, bootstrap can be found with a different version, downloaded, and listed in Maven Dependencies, but it won't resolve as an import. Can someone tell me how I might get this issue resolved? The POM XML snippets:
`<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<complianceLevel>1.8</complianceLevel>
<Xlint>ignore</Xlint>
<source>1.8</source>
<target>1.8</target>
<weaveDependencies>
<weaveDependency>
<groupId>net.bootstrap.api</groupId>
<artifactId>bootstrap</artifactId>
</weaveDependency>`
<properties>
<dagger.version>2.11</dagger.version>
<build.version>1.0.0</build.version>
<bootstrap.version>2.0.1</bootstrap.version>
The error I receive when I build with clean install, is "Failure to find net.bootstrap.api:bootstrap:jar:2.0.1 in https://repo.maven.apache.org/maven2
I tried getting bootstrap from org.webjars (which does not have version 2.0.1) and changing the imports, but the imports won't resolve. I think I need to understand how weaveDependencies work since all the other Maven Dependencies are downloaded and the imports are resolved--this bootstrap is now the only issue.
This is an open-source project from a vendor and I just discovered that others are having the same issue and is due to missing (propriertary) files. so this is not something anyone else can answer, I need to acquire the source files from the vendor.

Can't resolve the error: java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException

Trying to use Maven to organize my project and I keep running into the following error. I know that this error means the file is present at compile time but for some reason it can't be found at runtime.
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
So I'm working on a project in Java that will take a user query, search Google Images and then download some of the results onto my computer. To that end I've had to use some third party libraries like JSoup, Json-Simple, and Gson.
I initially added Jsoup to my classpath manually and it worked, but then I heard about Maven and started using it instead. My issue is that when I try to run my code I get the error above.
I'm just not sure how to resolve this. I've seen a bunch of other posts about similar errors and I've tried to modify my pom.xml accordingly but
I just can't get it to work. I've tried removing the ~/.m2 file, ran mvn clean, mvn install, mvn package, mvn compile, and it all works fine. But when it comes time to run, I keep getting that error.
Here's most of my pom.xml file.
<repositories>
<repository>
<id>central</id>
<name>Maven repository</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- jsoup HTML parser library # https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
FOUND SOLUTION: So I left out some parts of the pom.xml file to make it easier to read, and because all the other parts were generated by Maven itself so I figured there couldn't be an issue with anything there. But it adds a tag called "pluginManagement" that encloses all other plugins and apparently this does not allow the Shade plugin to run.
Between ngueno's guidance and this post Maven shade plugin is not called automatically for goal "package"
I was able to figure out my issue, though I'm still not entirely sure why it is an issue. Anyways, I figured I'd update this post in case someone else with a similar problem stumbles across it. This was on Mac OS btw, in case it makes a difference. Thanks for your help everyone.
Usually NoClassDefFound errors are related to missing libraries at runtime.
Since you are running using the terminal I supose you are building your project using Maven, and running the generated JAR,
I would recommend to you to use the maven-shade-plugin and generate an uber-jar as I explained on this question.
The purpose generating a uber-jar is to carry all the needed dependencies inside of it (available on the application classpath).
Implement the plugin and try to run using the new JAR.
PS: Remember to check this section related to Executable Jars
UPDATE: Remove the <scope>provided</scope> of your jsoup dependency, to enforce Maven to package it along your app, with the provided scope you are saying that this dependency will be provided by the JDK at runtime.
The jars that you identify in your dependencies must be present in the Runtime classpath.
This is not the classpath that is available when you compile the code;
it is the classpath on the host where you run the application.
You must install these jars on the target host.
Edit: More details
You must do the following:
Identify the runtime host.
Create a directory on the runtime host into which you will install the dependent jar files.
Include every jar in the classpath.
Consider abandoning the "roll-your-own" path.
If you use Spring Boot
(I like it,
I don't work for them).
One feature of spring boot is a reinvented "Fat Jar" that will include the dependencies inside one deliverable artifact (the fat jar) and will add them to the classpath at startup.
Edit:
The Spring boot executable jar file is not a "Fat Jar",
instead it includes the dependencies in a directory in the
executable jar and adds said jars to the classpath on startup.

Correct way to set up a Maven POM to compile mixed Java/Groovy code in Intellij IDEA

I have a very simple test project in Intellij IDEA in which I try to mix Java 9 and Groovy code. Additionally, this project has Maven Support, i.e. it is organized according to a Maven archetype and has a POM.
Now I have two classes in the default package calling each other (though not cyclically as compilation for that fails) and all is working well, i.e. Build > Build Project and Run > Run 'Main' are working.
However, this compilation seems to be independent of Maven configuration. There is no Groovy support in the POM for one. If I just compile from the command line with mvn compile compilation fails as the linker cannot find any object that would result from Groovy compilation.
The POM just contains configuration for the maven-compiler-plugin and I added that by hand with source and target nodes under configuration set to 1.9 to have IDEA compile from/to Java 9 instead of from/to Java 5. So there is some interaction between what is in the POM and what IDEA does when I select Rebuild Project.
What is correct way to configure IDEA and/or configure the POM so that compilation succeeds both in IDEA and on the command line. And if anyone knows, what are the interactions between what's in the POM and IDEA?
Do I have to configure the Groovy Eclipse Maven plugin in the POM? (I will try to do that)
This is what I have working for Java 8 and Eclipse, for a project with both Java and Groovy code. I know I found the basis for this via Google at one point but did not save the URL, alas. Not sure if it will work with Java 9 and IntelliJ but worth a shot?
<properties>
<groovy.eclipse.compiler.plugin.version>2.9.1-01</groovy.eclipse.compiler.plugin.version>
</properties>
....
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher -->
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<!-- set verbose to be true if you want lots of uninteresting messages -->
<!-- <verbose>true</verbose> -->
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy.eclipse.compiler.plugin.version}</version>
</dependency>
<!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.3.7-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy.eclipse.compiler.plugin.version}</version>
<extensions>true</extensions> <!-- required to get plugin to compile tests when no src/main/java dir exists -->
</plugin>

Created new maven GAE project, Eclipse importing as three different projects

I created a new GAE project using the appengine-skeleton-archetype by following the steps given at Creating App Engine applications or backend APIs using the archetypes.
The layout of the project is now different than when I did the same thing 5-6 months ago: there are subsidiary myapp-war and myapp-ear sub-projects (is that the right terminology?)/sub-directories, each with their own pom.xml (in addition to the pom.xml created in the root myapp directory).
When I import this maven project into Eclipse (File > Import > Maven > Existing Maven Proects and point it at the project root directory myapp), it creates three different projects, titled myapp, myapp-war and myapp-ear.
So my questions are:
Is this normal/ correct/ expected?
I know to add my classes to myapp-war/src/main/java, but what about the template files? Do they go under myapp-war/src/main/webapp?
Why doesn't the appengine-skeleton-archetype specify the Java version (e.g., 1.7) to use? The project I created a few months back specifies org.apache.maven.plugins:maven-compiler-plugin in the <plugin> section and under <configuration>, <source> and <target> can be set to 1.7.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.5.1</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
To add RythmEngine to my project I had to add the following in my earlier project:
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
...
<dependency>
<groupId>org.rythmengine</groupId>
<artifactId>rythm-engine</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
Where does the <parent> go now?
Where do libraries like Objectify and Guava go now? In the myapp-war/pom.xml?
Configuration:
OS: Mac OS X 10.9.4
Eclipse: Kepler (m2e plugin 1.4)
GAE SDK 1.9.8

Categories