How to know where the class is coming from in Java - java

I faced a very confusing problem today where my email service, I am using simple-java-mail, stopped sending all in a sudden.. and an exception was raised:
Exception in thread "pool-3-thread-1" java.lang.NoSuchMethodError: com.sun.mail.util.TraceInputStream.(Ljava/io/InputStream;Lcom/sun/mail/util/MailLogger;)V
I was going insane as nothing affecting emails was added, I tried to use maven enforcer plugin to discover what was going on but with no clue.
The question was how can I discover which jar or library does a class at runtime come from?

With this snippet you can figure out from where this class is coming from, and so find the source of a class immediately:
Class klass = Class.forName("javax.mail.Service");
URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");
System.out.println(location);
Now you can know javax.mail.Service resides in which JAR.

Related

java.lang.ClassCastException: class sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to class com.sun.net.ssl.HttpsURLConnection

Since I updated to Java 11, I am getting an exception which is proving impossible to pin down.
Please note: The classes the exception mentions are nowhere to be found in my own code.
The stacktrace only shows which bit of my code meets the problem, but gives no context at all for the actual code (third party) that causes the problem.
This exception has been asked about before in Stackoverflow, but I can only solve the problem if I can find which third party code is causing this and upgrade it, which will (hopefully) mean that I can see the back of it.
The exception is as follows:
java.lang.ClassCastException: class sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to class com.sun.net.ssl.HttpsURLConnection (sun.net.www.protocol.https.HttpsURLConnectionImpl and com.sun.net.ssl.HttpsURLConnection are in module java.base of loader 'bootstrap')
I strongly suspected some very old apache HttpClient classes, and have replaced them with the java.net.http classes (available since Java 11), but to no avail.
The question is: is there a way to find which .jar contains code that uses HttpsURLConnectionImpl and HttpsURLConnection?
And does anybody know what the rather unhelpful indication that they are in module java.base of loader 'bootstrap' means?
are in module java.base of loader 'bootstrap'
It means that class sun.net.www.protocol.https.HttpsURLConnectionImpl is loaded by the bootstrap loader and are loaded as part of the module base. This is completely useless information: That module is the core java stuff (java.lang.String is in there too), and that loader is the one that loads that stuff. No wonder; sun.* classes are implementation details that are part of what all JDKs ship with out of the box. It provides nothing useful whatsoever for fixing the problem.
The exception is as follows:
There are only two options.
You forgot to paste the stack trace that follows the stuff you did paste. That stack trace is the important part, and lets you know exactly where to look.
You have broken exception handling.
I'm guessing it's option 2 - it's a common thing and perpetuated by manual tutorials. The fix is unfortunate: Go through all your code and fix the bad code. A search for .getMessage() should get you pretty far. It sure sounds like you are doing something like this:
try {
.. code here ..
} catch (Exception e) {
log.warn(e.getClass() + ": " + e.getMessage());
}
or similar. This is always bad - do not just 'log/print' and forget about an exception: You want any exception to be handled, OR thrown onwards. You never want to take an exception whose meaning is not clear as you write the code, and just 'swallow' it. Update your IDE's template settings for generating try/catch clauses; the proper way to write code that just sort of goes; "Exceptions? Um... I do not want to think about those right now / I dont know what they mean / I do know but there's nothing useful I can do about them at this point or I can't fathom how they could occur", which covers most of the time you need to deal with an exception, is like this:
try {
.. stuff ..
} catch (SomethingIDoNotWantToHandleException e) {
throw new RuntimeException("unhandled", e);
}
This will ensure that the place you see that error now has the actual stack trace which will then let you know which library you need to update.
The error was coming from pd4ml, a java library for producing pdfs.
I updated to the latest version (3.11.5), which solved the problem.

SWI-Prolog: Could not open resource database "../../src/swipl.prc"

I have checked out the SWI-Prolog JPL library, and I am trying to add some functionality. Right now, I am just trying to get the tests to work in the TestJUnit test. However, I am getting the following error:
SWI-Prolog: [FATAL ERROR:
Could not open resource database "../../src/swipl.prc": No error]
I have looked at the code, and I know that path is coming from this declaration:
public static final String startup =
(System.getenv("SWIPL_BOOT_FILE") == null ? "../../src/swipl.prc" :
System.getenv("SWIPL_BOOT_FILE"));
It is looking for a an environment variable %SWIPL_BOOT_FILE%, so I could see how this might fix the problem. However, I don't know which file I should add as the boot file.
I have tried the swipl-win.exe, but that doesn't work.
Any ideas?

Android: Class in source code not found

I am trying to access the PhoneInterfaceManager.java on android.
As it is not part of the sdk, I need to use reflection. (I know I shouldn't do this, but as far as I know there is no other way).
The full name of the class is "com.android.phone.PhoneInterfaceManager".
Now when I run this code...
Object getPhoneInterfaceManager() throws ClassNotFoundException{
return Class.forName("com.android.phone.PhoneInterfaceManager");
}
...I get:
java.lang.ClassNotFoundException: com.android.phone.PhoneInterfaceManager
Note:
I checked the grep repository (which I find to be more reliable sometimes) and noticed that while the package for classes which worked with reflection were in the project "com.google.android / android", this class was to be found in "com.google.android / android-apps ". I don't know if this might mean something, but I thought I'd mention it anyway.
Thanks for your help in advance!

Netty NIO Udt Attempt Giving Error and I cannot find the answer in the Docs

Sorry for the possibly bad title but it is exactly as it says. I am new to Netty and this is my second attempt at a networked solution to what I am trying to do. The first solution straight java.nio UDP works but is inefficient and slower than I can stand.
I keep getting an error on start up and cannot find the solution. The error tells me that there is no such field Rendezvous in the class io.netty.channel.udt.nio.NioUdtProvider. From the javadocs it seems that this specifies what the thread does. Am I missing a jar? What happened? Can I get any pointers on how to solve this?
I am using Netty 4.0.0.1 CR1. It is the only release I found with that works with the examples. I also have barchart-udt-core-2.2.0, and jsch in my classpath.
The problem is occurring in my server's main class (not named main). At the following line.
final ThreadFactory connectionFactory=new UtilThreadFactory("connect");
final ThreadFactory acceptFactory=new UtilThreadFactory("accept");
final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1,acceptFactory,NioUdtProvider.MESSAGE_PROVIDER);
final NioEventLoopGroup connectGroup=new NioEventLoopGroup(1,connectionFactory,NioUdtProvider.MESSAGE_PROVIDER);
More specifically, the error occurs in the last two lines.
I receive the following error code when working with both the MsgEchoServer example and my NettyServer.
Nov 07, 2013 5:07:53 PM netty.NettyServer main
INFO: init
Exception in thread "main" java.lang.NoSuchFieldError: RENDEZVOUS
at io.netty.channel.udt.nio.NioUdtProvider.<clinit>(NioUdtProvider.java:68)
at netty.NettyServer.run(NettyServer.java:103)
at netty.NettyServer.main(NettyServer.java:191)
I have tried to find a netty 4x jar that contains these definitions, specifically a full Netty release but get the same or different errors.
My UtilThreadFactory code is below and is pretty much the same as from http://grepcode.com/file/repo1.maven.org/maven2/io.netty/netty-example/4.0.0.CR1/io/netty/example/udt/echo/message/MsgEchoServer.java?av=f
Thanks
UtilThread
package Netty;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
public class UtilThreadFactory implements ThreadFactory{
private static final AtomicInteger counter=new AtomicInteger();
private final String name;
public UtilThreadFactory(final String name)
{
this.name=name;
}
public Thread newThread(final Runnable runnable)
{
return new Thread(runnable,name+'-'+counter.getAndIncrement());
}
}
*UPDATE*
I upgraded like requested and get the exact same error. I am now running Netty 4.0.12 with the jars 4.0.12 and 4.0.12-FINAL in my path. The same error occurs whether I have one or other jars and I cleared my eclipse cache. The same problem happens in STS spring tools. Any help is appreciated. Thanks
Thanks for all of the help. I found the culprit after moving all of the jars to my classpath and reading through their contents. Something was missing from the udt-core as apparently I had a few older versions. I really wish wildfly/the netty guys would document this a little better. I downloaded the udt-core jar and this solved the problem. The latest jars are difficult to find. They are at http://repo1.maven.org/maven2/com/barchart/udt/
Please upgrade to netty 4.0.12.Final ... Your version is quite old and we have working examples in there with UDT.

NoClassDefFoundError while accessing GraphicsEnvironment.getLocalGraphicsEnvironment on Tomcat

I have an application which is running on tomcat, one of the methods is, creating a simple thumbnail from an jpeg image. The functions works fine offline and a week ago also on tomcat. But now i get the following error:
java.lang.NoClassDefFoundError
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:164)
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1141)
eval.impl.ImageEval.getThumbnail(ImageEval.java:155)
eval.impl.ImageServlet.doGet(ImageServlet.java:79)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
I don't think that i have change anything what should influence this (actually i didn't change the function at all according to the svn repository), so it must be a library problem. But i can't figure out what is missing.
Here are the actual lines from the getThumbnail function, where the error occures:
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(simage, 0, 0, thumbWidth, thumbHeight, null);
[edit] I decided to update the problem description a little.
Yes it seems that he can not find some class from java.awt or one related to that. But they do exist on the server in the jvm. Java headless mode doesn't solve the problem.
In another project the exact same code, but inside an axis2 webservice on this server is working fine.
[/edit]
It seems like you've change the configuration of Tomcat.
Either you've changed to a l{0,1}[iu]n[iu]x box or installed on a virtual machine with different security control than the one where you test it.
Apparently the
GraphicsEnvironment.getLocalGraphicsEnvironment()
Is trying to access the property: java.awt.graphicsenv
Which may return null or some non existing class name which is then loaded and throws the ClassNotFoundException. 1
The solution seems to be specifying the "java.awt.headless" property.
This is a similar question: java.awt.Color error
Try this search , it shows similar situations as your.
I remember there was something in the sun bugs database too.
Post the solution when you find it!
1.GraphicsEnvironment.java
EDIT
It is not eclipse!!
In my original post there is a link to the source code of the class which is throwing the exception.
Since I looks like you miss it, I'll post it here for you:
public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() {
if (localEnv == null) {
// Y O U R E R R O R O R I G I N A T E S H E R E !!!
String nm = (String) java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction
("java.awt.graphicsenv", null));
try {
// long t0 = System.currentTimeMillis();
localEnv =
(GraphicsEnvironment) Class.forName(nm).newInstance();
// long t1 = System.currentTimeMillis();
// System.out.println("GE creation took " + (t1-t0)+ "ms.");
if (isHeadless()) {
localEnv = new HeadlessGraphicsEnvironment(localEnv);
}
} catch (ClassNotFoundException e) {
throw new Error("Could not find class: "+nm);
} catch (InstantiationException e) {
throw new Error("Could not instantiate Graphics Environment: "
+ nm);
} catch (IllegalAccessException e) {
throw new Error ("Could not access Graphics Environment: "
+ nm);
}
}
return localEnv;
}
That's what gets executed.
And in the original post which you don't seem to have read, I said the code is accessing the property "java.awt.graphicsenv"
If that other project using axis doesn't have the same problem it may be because it may be running in a different tomcat configuration or the axis library allowed the access to that property. But we cannot be sure. That's pure speculation. So why don't you test the following and see what gets printed:
String nm = (String) java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction
("java.awt.graphicsenv", null));
System.out.println("java.awt.graphicsenv = " + nm );
It it prints null then you now what the problem is. You don't have that property in your system, or the security forbids you do use it.
It is very hard to tell you from here: "Go and edit file xyz and add : fail = false" So you have to do your work and try to figure out what's the real reason.
Start by researching what's the code being executed is ( which I have just posted ) and follow by understand what it does and how does all that "AccessController.doPrivileged" works. (You may use Google + StackOverflow for that).
We had a similar issue and after much trouble shooting it was identified to be related to the java.awt.headless property. The issue was resolved by explicitly setting the JVM option to
-Djava.awt.headless=true
It was running a week ago, and now it is not.
THEREFORE, YOU CHANGED SOMETHING BETWEEN "working" and "not working".
Go back to the working config (if you can), and rigorously track what you changed. If you don't have a backup of the working config, then meticulously go back through what you've done between working and non-working until you find what you changed.
It may not be code - it could be a config file, etc.
Best of luck,
-R
Is this server running java in server mode - I hear that doesn't load in the AWT classes.
If you are deploying this on *nix, and you don't have an X window system running anymore, that could explain it. Even if you do, if you aren't exporting the DISPLAY system variable to the process that starts the JVM, or if you are but it is not actually valid, it could cause such an issue.
That would at least explain why you didn't change any configuration in tomcat, but still have a problem.
If your NoClassDefFoundError has no message at all, then this means two things:
The JVM has already tried and failed to load a class. Usually, this means the JVM was unable to complete static initialization for that class, i.e. assign values to any static fields and run any static { } blocks. Often, this is because the classes necessary to do this static initialization are missing.
You're using Java 5, not Java 6. (In Java 6, you get a 'Could not initialize class xyz' message instead.)
The problem class appears to be the one whose name is the value of the system property java.awt.graphicsenv. I would start by finding out the value of this property. What happens when you try to instantiate this class?
Since you're getting NoClassDefFoundError from inside the AWT code, it looks like Java is failing to load the X Windows libraries. Note that even if you're running in headless mode ($DISPLAY not pointing to an X Windows server), AWT still needs some subset of the X11 libraries in order to render images. See, for example, this reference:
http://javatechniques.com/blog/linux-x11-libraries-for-headless-mode
If something stopped working and your Java code didn't change, it's possible that the X11 libraries got moved or uninstalled on your machine, or that for some other reason your LD_LIBRARY_PATH environment variable doesn't point to them anymore.

Categories

Resources