How to use external libraries managed by Maven in IntelliJ Idea? - intellij-idea

While importing org.apache.commons.math3 library with Maven (editing the pom.xml file), IntelliJ 2019 seems to update correctely the "External Libraries" and "Dependencies" nodes but later, when trying to use the library happens that symbols are not resolved and looks like the program is not able to reference that library even if Maven imported them correctely. Using the "import" keyword, IntelliJ seems to be unable to reference the library.

I sort out that declaring the full path of the class declared inside the external library (see example below), IntelliJ proposes to the library reference to module-info.java, contained inside the browser tree of the IntelliJ project. If the full path is not declared, IntelliJ does not ask to modify the module-info.java file. After the reference is added to module-info.java, than the IntelliJ "intellisense" is able to resolve the symbols.
public class GastTank implements org.apache.commons.math3.ode.FirstOrderDifferentialEquations{
#Override
public int getDimension() {
return 0;
}
#Override
public void computeDerivatives(double v, double[] doubles, double[] doubles1) {
}
}

Related

Error: Could not find or load main class in Maven converted project

I just created a java project and ran it. It ran fine.
Then I converted it into a maven project and resolved all errors in POM.XMl file as well. When I try to run the class file , Am getting the error
Error: Could not find or load main class jhj
Caused by: java.lang.ClassNotFoundException: jhj
My class looks like this:
public class jhj {
public static void main(String[] args)
{
System.out.println("as");
}
}
And my Environment variables are properly set.
I have set the M2_home, MAVEN_HOME and JAVA_HOME as well.
I tried removing the project from the workspace and re-importing them.
Also, I tried with refresh and Maven>>update the project options as well
Check your project build-path and enable specific output folders for each folder. Go one by one though each source-folder of your project and set the output folder that maven would use.
For example, your web project's src/main/java should have target/classes under the web project, test classes should have target/test-classes also under the web project and so.
Using this configuration will allow you to execute unit tests in eclipse.
Just one more advice, if your web project's tests require some configuration files that are under the resources, be sure to include that folder as a source folder and to make the proper build-path configuration.
Hope it helps.

Modify GroovyDSL classpath to include 3rd party libraries

I'm trying to create a GroovyDSL script which references some external libraries. Here's my script:
import com.github.javaparser.ast.Node
import org.reflections.Reflections
def ctx = context(
ctype: 'groovy.util.ObjectGraphBuilder',
paths: ['com/example/scripts/.*'],
filetypes: ["groovy"]
)
Map<String, Class> candidateClasses = new Reflections(Node.packageName).getSubTypesOf(Node)
.collectEntries { Class type -> [(type.simpleName.uncapitalize()): type] }
contributor(ctx) {
candidateClasses.each { String methodName, Class type ->
method name: methodName, params: [props: "java.util.Map", closure: "groovy.lang.Closure"], type: type.name
}
}
Trying to enable it in Intellij, I'm getting:
startup failed: transformDslSyntaxgdsl: 1: unable to resolve class com.github.javaparser.ast.Node
# line 1, column 1.
import com.github.javaparser.ast.Node
Now, I have the proper external dependencies declared in pom.xml, the rest of the code that depends on them is working just fine. I've also put the script inside a source folder (which some other answers here suggested might be relevant).
I have seen some examples for GDSL reference Intellij types like PsiClass, which tells me the classpath for GDSL files seems to be different from the project classpath. Is there any way to make sure project dependencies are appended to that classpath?
I also tried using #Grape only to get this error. Adding Apache Ivy as a dependency doesn't help, because again, project dependencies don't seem to influence the GDSL classpath.
After a bit more digging, I found that it is pretty easy to modify the IDE's classpath itself.
All you need to do is to drop a dependency into Intellij installation directory's lib subfolder, and reference the jar inside classpath.txt.
Initially, I added the jars my GDSL depends on directly, but then I realized I could simply add a dependency on Apache Ivy to classpath.txt instead and #Grab annotations would start working.

using kotlinx serialization inside a gradle plugin that creates a gradle task

I am trying to create a gradle plugin that will generate files (serialized from data classes) from a gradle task that can run in another project.
lets say that the classes that I am serializing are marked with some annotation #Annot and I find all the relevant classes with reflection in the gradle task (I made sure to depend on kotlin compile so that the binaries are created). The problem is that when I try to use
val clazz: Class<*>
clazz.kotlin.serializer()
I get a Serializer for class 'Type' is not found. (Type is the actual class that I found and is annotated with #Serializable and #Annot .
I am using gradle version 7.2, kotlin 1.5.21 (tried with 1.5.31 too)
The project that uses the plugin has a kotlinx serialization plugin enabled
What am I missing? why can’t I access the class serializer with the gradle task?
Note* if I run the above code in the target project (and not in the plugin then the serializer() function doesn't throw an exception
So This didn't work in a the way I wanted it to but I found a way to make it work.
I defined a task that extends JavaExec task:
tasks.create(createFilesTaskName, JavaExec::class.java) {
mainClass.set("package.of.file.SchemaKt")
classpath = sourceSets.getByName("main").runtimeClasspath
group = groupName
}
The code in SchemaKt is in the source set of my kotlin sources or alternatively in a package required by the current project.
The serializer() is accessible and working from there and I can run the schemas creation from a gradle task which is exactly what I needed.
I hope this helps someone in the future.

Kotlin: How to import a class from another IntelliJ module?

I have a simple Kotlin project in IntelliJ with two modules defined as sub-folders under the project root folder. Everything seems to be set up correctly in build settings, each module has its own folder marked as a source directory.
I can run main functions in both modules so IntelliJ is finding everything OK.
What I cannot do is reference a public class in one module in the other. I've tried every form of import statement I can think of, but the compiler always flags the class in module1 as an unresolved reference in module2.

Error while creating a jar through artifacts in Intellij

First of all, i searched for similar questions and i found something, but nothing helps me out.
I'm trying to create a jar file in intellij using the artifact, but everytime i build i have the error: Error: Could not find or load main class com.test.wms.test
This is my test.java file
package com.test.wms;
public class test {
public static void main(String[] args){
// empty
}
}
Then i went into Project Structure -> Artifacts and added a new artifact, selected the type (jar), selected the name (test.jar) and generated the manifest.mf (autogenerated from the artifact page on intelij)
Manifest-Version: 1.0
Main-Class: com.test.wms.test
And this is the artifact edit page
Then i build the project and try to lunch in konsole with java -jar test.jar and the error is always the same: Error: Could not find or load main class com.test.wms.test
This is my project structure:
And this is my artifact edit page
Where is the mistake?
Thanks!
Fixed by myself, when i was creating the jar from the artifact i was selecting empty instead of From modules with dependancy (the output is the same but with this option works, maybe the IDE does something particular)