WSO2ESB Custom Mediator Java Class is not found in the path - wso2-esb

I have created the project using WSO2 Developer Studio.
Under src/main/java I created the package samples.mediators
In that package the class UnzipFileMediator:
package samples.mediators;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
public class UnzipFileMediator extends AbstractMediator {
public boolean mediate(MessageContext context) {
// TODO Implement your mediation logic here
System.out.println("UnzipFile Mediation entered");
return true;
}
}
To deploy the code an followed this instructions:
Right click on the project and select Export Project as Deployable Archive.
It created the jar file named Unzip.jar
I deployed the Jar file in to <ESB_HOME>/repository/components/lib
directory.
In the synapse configuration I call the class like this
<class name="samples.mediators.UnzipFileMediator"></class>
but when i try to save it.It generate the below error:
org.apache.axis2.AxisFault: Class samples.mediators.UnzipFileMediator not found in the path
What am I doing wrong here?

Use a different package name.
Because the namespace (or package) samples.mediators is already used by WSO2 in a different jar. You had deployed now a second jar with the same package name, these classes will not be found in the Classpath by WSO2.
Choose for your own classes a correct package (namespace) with your company name like com.mycompany.mediators.

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.

Kotlin - Import Jar and Use Class

I have created a library in kotlin using gradle init and following the prompts. It compiles and produces a jar file in lib/build/libs. I then have another project that needs to access functions in the library. through intelliJ, I add the dependency by going to Project Settings > Modules > Dependencies and adding the jar file. I then attempt to import the package defined in the library with import DemoLib where DemoLib is the name of the package (and the name of the library). This does not compile as it does not recognize the package name. I have also tried importing as a library rather than a jar, with the same results. How can I achieve the desired result?
EDIT: In case it helps here is the code:
Library:
package DemoLib
class Library {
fun someLibraryMethod(): Boolean {
return true
}
}
Client Code:
package DemoClientAppOne
import DemoLib.*
class App {
val greeting: String
get() {
return "Hello World!"
}
}
fun main() {
println(App().greeting)
}
Not terribly interesting, but the point is that DemoLib is an unresolved reference even after adding the jar as a dependency
You have three options
Combine two projects under same IDE project and use Gradle dependencies on project:
settings.gradle:
include 'project-lib'
build.gradle:
implementation project(':project-lib')
See https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:project_dependencies
Install the library into local repository and add it as a Gradle dependency:
apply plugin: 'maven'
then run gradle publishToMavenLocal
and add mavenLocal() repository in gradle.build file.
See How to install a compiled by Gradle jar into the local Gradle Repository files-2.1 instead of the Maven repository?
Use this jar as a flat library Gradle dependency (assuming this jar is located in the libs directory:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation name: 'lib-jar-name'
}
If DemoLib is a package,
you need to
import DemoLib.*
to import all its names into scope. Just import DemoLib wouldn't do anything. Or better,
import DemoLib.SomeClass
to import only one specific name. Actually, if you start typing SomeClass (assuming that's a name in your DemoLib package), IDEA should suggest adding the import.
Also, it's better to follow naming conventions from the beginning, and DemoLib is not a good package name:
Names of packages are always lower case and do not use underscores (org.example.project). Using multi-word names is generally discouraged, but if you do need to use multiple words, you can either simply concatenate them together or use camel case (org.example.myProject).

How to use external libraries managed by Maven in 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) {
}
}

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)