3

I have included the required JAR downloaded from here: www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm in the Build Path of the project.

Yet, unable to resolve this error. What else could cause the problem?

My stack trace is shown below:

java.lang.NoSuchMethodError: org.json.JSONObject.getNames(Lorg/json/JSONObject;)[Ljava/lang/String;
at com.comp.cloud.portal.zRPCEngine.zCompRPC(zRPCEngine.java:641)
at com.comp.cloud.portal.zCompManager.CompSubscribe(zCompManager.java:1480)
at com.comp.cloud.portal.zCompManager.AddCompHD(zCompManager.java:310)
at com.comp.cloud.portal.zCompManager.LoadInventory(zCompManager.java:96)
at com.comp.cloud.portal.zCompCloudPortalUI.Login(zCompCloudPortalUI.java:333)
at com.comp.cloud.portal.portlets.Login.ExecuteLogin(Login.java:333)
at com.comp.cloud.portal.portlets.Login$button_login_clicked.buttonClick(Login.java:234)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:984)
at com.vaadin.ui.Button.fireClick(Button.java:393)
at com.vaadin.ui.Button$1.click(Button.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:295)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:188)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:93)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:237)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)
0

2 Answers 2

6

About java.lang.NoSuchMethodError :

This error is a runTime error which occurs as a result of missing method definition in the classes available in the ClassPath. After the classLoader loads the class, linking phase fails to link the method call as the requested method is not available in the ClassPath classes This error generally occurs when you compile and test your code in one environment (generally local machine) and you deploy your code in another environment. We will consider the following example for our use case : java.lang.NoSuchMethodError: org.json.JSONTokener

Why the classes are not available in the ClassPath despite creating a uber jar? :

It’s because classes are loaded at runtime and if loader finds multiple classes with same signature, it loads the first one it encounters. If that class doesn’t have the required method, we will get the NoSuchMethodError. Gist is that classLoader fails to find the exact class which we want it to load.

How to find which class is getting load?

We can use following code snippet in the main method to find out the class which is getting loaded.

ClassLoader classloader = org.json.JSONTokener.class.getClassLoader();
URL res = classloader.getResource("org/json/JSONTokener.class");
String path = res.getPath();
System.out.println("Core JSONTokener came from " + path)

What to do after finding the path of class from which it is getting loaded?

If you find that the library that is getting loaded is coming from some other path in the machine, you have to make sure that the desired library gets loaded. The best method to do so is to shade your jar and rename the references in your project.

How to shade a jar and rename references in Scala/Java?

In Scala, you can add

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.7")

to ./project/plugins.sbt and then use the sbt commandsbt assembly to generate a fat jar-file. Now to shade the jar use following code in your sbt:

assemblyShadeRules in assembly := Seq(
ShadeRule.rename(“org.json.**” -> “shadedJson.@1”)
.inLibrary(“org.json” % “json” % “20180813”)
.inAll
)

What this will do is, rename all the references of regex org.json.** to shadedJson.** in your code as well as in the library that you have included in your dependencies and shade the jar with changes. Now when you run your program, it will find both the jars in the classPath, but since you have renamed the references in your code and your jar, that will be the one which will be loaded by classLoader and not the other ones.

In Java, you can use maven-jar-plugin and maven-shade-plugin to create a shaded jar in java. https://examples.javacodegeeks.com/enterprise-java/maven/maven-jar-plugin-example/

<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</build>

now to rename reference in java, you have to use relocation pattern in maven-shade-problem as mentioned in the following link: http://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html How to check if jar is shaded or not? Simple, just explode the jar and search for class name as per your rename. eg in our case you can search for shadedJson Command to explode : jar -tf *redirect the output to a file and search in that file.

Sign up to request clarification or add additional context in comments.

Comments

1

May be that you added the jar to your project build path, but you are not bundling it into your war deployed on Tomcat. Edit: You either bundled an older version of the jar or an older version of the jar is visible from your application classpath.

2 Comments

I think there is jar in the classpath. If not, you will get ClassNotFoundException instead of NoSuchMethodError.
Yes, wrong jar and not absent. Still 2 versions, one older at runtime and the correct one at build time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.