Here's my setup:
My server waits for IPlugin-Objects with an ObjectInputStream. The incoming IPlugin-Object is of an unknown class, so first, the class-file is transmitted and loaded by the classloader of the OIS. Then, the IPlugin itself is sent by the client. The cast of the IPlugin seems not to be a problem (when using my own classloader in the OIS). But the IPlugin uses jama and now I got an NoClassDefFoundException when I receive the object. The weird thing for me is, that in the servers classpath the Jama-library is contained. I also tried, to write a dummy-line, so the library will really be imported (and not ignored by the compiler).
Can anybody help me on that? It really bugs me...
Okay, I've got it:
The problem was, that my personal URIClassLoader got every URI possible, except the current classloader. Now the constructor looks like the following:
URLClassLoader loader = new URLClassLoader(new URL[]{pluginFolder.toURI().toURL()},
this.getClass().getClassLoader());
Related
I have been trying to do a kind of plugin-system using the ServiceLoader. There are 2 modules, the first provides the abstract class LoadedRealmPlugin. The second one extends this class. I have added the file corresponding to the full name of the ServiceProvider and added the service-class to it. IntelliJ does not find any errors (but when changing the filename or classname it does). Here is the structure:
MainModule
src
main
java
com.interestingcompany.mainmodule
LoadedRealmPlugin
MainModule.iml
Plugin
META-INF
services
com.interestingcompany.mainmodule (-> Content: "PluginExtension")
src
PluginExtension
Plugin.iml
(This is simplified, I left out classes that (I think) are not important to the ServiceLoader. I can post a screenshot of the actual structure if anyone needs it)
Here is the code I use to load the Service:
File file = new File("Plugins/Plugin.jar");
URLClassLoader c = new URLClassLoader(new URL[]{file.getAbsoluteFile().toURI().toURL()});
ServiceLoader<LoadedRealmPlugin> loader = ServiceLoader.load(LoadedRealmPlugin.class, c);
LoadedRealmPlugin p = loader.iterator().next(); // Throws a java.util.NoSuchElementException
p.Initialize(RealmPath); // Abstract method implemented in the service
return p;
When trying to run it, I always get an empty ServiceLoader. I looked at this post, but I was not quite sure about how to apply that answer since I am trying to load my plugin from a file. In addition, I found this post. Yet, there was no answer, just some comments that did not seem to have answered the question.
As you might have been able to tell, this is my first time working with classloaders. If there is any additional information needed, just ask me. Thank you for reading through my beginner troubles.
package-less classes are in the unnamed package, which is inaccessible to rather a lot of code, notably including here.
Put PluginExtension.java in a package, make sure the content of your META-INF/services/com.ic.mainmodule file reflects this (content should be pkg.PluginExtension), and it'll work fine.
In a Tomcat 7 I have a pretty standar jar file on WEB-INF/lib. Inside this jar I have this class called Parser, and next to it (on the same dir) I have another one called AutomaticLocalLoader. Compilation gives no problem at all. In run time the AutomaticLoader class is found, and when It needs the Parser class, I get a NoClassDefFoundError
The Parser and AutomaticLoader class have been working without this problem for 15 years!! in many diferent vers of java and tomact; and now out of the blue, I am getting this NoClassDefFoundError, only for the Parser class. I already put a copy on a directory inside the WEB-INF/classes path and still got the same error. I already created my own ClassLoader to see if I get some error loading the class from the WEB-INF/classes directory by myself, but I can load it without problems.
log.info("Leer " + aFlInstructions[i].getAbsolutePath());
LoaderTest A = new LoaderTest();
A.test("com.hds.resolve.model.aguila.AutomaticLocalLoader");
LoaderTest B = new LoaderTest();
B.test("com.hds.resolve.model.aguila.Parser");
if(!bOverrideInputDir)
Psr = new Parser(aFlInstructions[i]);
else
Psr = new Parser(aFlInstructions[i], new String[] { StrLocalDirectory } );
The LoaderTest class, try to create the Class Object for the given name using Class.forName. If NoClassDefFoundError, then try to load the class using my own classloader and then create the class.
For the AutomaticLoader, it succed at the first try. For the Parser class if fails, then successfully load it with the custom classloader. Of course when the code reach the "new Parser" part, the old webclassloader still fails and throws the NoClassDefFoundError.
Both Parser and AutomaticLocalLoader belong to the same package and are stored on the same jar inside WEB-LIB.
Funny enough, the error does always happen on production... but never in my machine. I do not use customs classloaders except for doing this debug. Also, trying an old version of the software seems to fix the error. No idea why.
I think I can hack a solution messing with the tomcat's webclassloader, but I really would prefer to understand what is going wrong with this code.
I'm attempting to use an online timestamp authority (rfc3161) with the Digital Signature Service Java library. However, the following snippet (from their test cases, and similar to the one from their Cookbook):
String tspServer = "http://tsa.belgium.be/connect";
OnlineTSPSource otsp = new OnlineTSPSource(tspServer);
/* tried setting otsp.setDataLoader(new TimestampDataLoader());
too, as it defaults to otsp.setDataLoader(new
NativeHTTPDataLoader()); the exception happens in both cases */
byte[] digest = DSSUtils.digest(DigestAlgorithm.SHA1, "Hello world".getBytes());
TimeStampToken timeStampResponse =
otsp.getTimeStampResponse(DigestAlgorithm.SHA1, digest);
always ends with the following exception:
eu.europa.esig.dss.DSSException:
java.util.concurrent.ExecutionException: java.lang.NoSuchMethodError:
org.apache.commons.io.IOUtils.closeQuietly(Ljava/io/Closeable;)V
Already tried many different public rfc3161 servers (some listed here). Sure there's something wrong going on there, but, as a beginner, I cannot understand what is wrong (what method should be there).
If anyone could put me in the right direction to get the snippet working (or even be kind enough to comment a reliable startup guide on cades/xades/pades with Java's bouncycastle) I would be really grateful.
As stated in the comments by Marteen Bodewes and Mark Rotteveel, there was something wrong with the version of Apache Commons-IO in the classpath. The project is set using Apache Maven and there was an old Commons-IO version declared there as a dependency. In this case, it was enough to remove that declaration, so Maven could download the appropriate version that was declared as an esig/DSS dependency.
esig/DSS version was 5.4 at the time.
I have a problem integrating Java code into KNIME. Similar posts on Knime forum (http://tech.knime.org/forum/knime-general/using-external-jar-in-java-snippet-node-workflow-not-able-to-initialize-class-of) were of little help and I also posted a question there but have not got answer so far, so I'm trying my luck here.
I am trying to integrate my code into KNIME workflow using JavaSnippet. I have exported the code into a jar and put it into the KNIME jre/lib/endorsed folder. The code references CDK 1.4.19 and I have also placed the corresponding jar file into the same directory. I do not have CDK node extensions installed in KNIME and using them is also not an option in my case.
The code starts with:
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
SmilesParser sp= new SmilesParser(builder);
When I try to execute JavaSnippet I get the following exception message:
Evaluation of java snippet failed for row "Row0". Exception message: Could not initialize class org.openscience.cdk.smiles.SmilesParser
When I just try
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
It works and I get no exception message. I have checked, the builder is not a null. However, when I try to initialize SmilesParser, it fails. This class is public. It has no default constructor and has one public constructor that takes IChemObjectBuilder as a parameter.
I have tried to use class loader:
URL[] classLoaderUrls = new URL[]{new URL("file:///path on my computer/knime_2.9.4/jre/lib/endorsed/cdk-1.4.19.jar")};
URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);
Class<?> parserClass = urlClassLoader.loadClass("org.openscience.cdk.smiles.SmilesParser");
Class[] classParameters = new Class[] {IChemObjectBuilder.class};
Constructor<?> constructor = parserClass.getConstructor(classParameters); //until this line there are no problems
Object parser = constructor.newInstance(builder); //fails here with the same exception message: Could not initialize class org.openscience.cdk.smiles.SmilesParser
I am sure that this is not a CDK error because I can execute the code in Eclipse.
Why can a constructor of one class be called from KNIME without any problems and a constructor of another class can not??
I would be very grateful if you could suggest a solution or a probable reason why this happens.
Thank you!
Ok, finally solved the problem myself. Cleaned project meta data, cleaned endorsed library directory, switched KNIME workspace to another directory, put all jars into one folder and added them as external libraries. Now it works:)
I have some misterious problem with system proxy detection:
Actually, I have the right code to detect system proxy settings at runtime, it can handle pac files and http proxy settings as well.
It works absolutely correct, when I store and execute everything on the target station.
BUT: I run one little piece of code on the target station and store anything else (jars) on another station, on which apache webserver runs. From the main I load the classes with URLClassloader, so that piece of code loaded via network, which responsible for the proxy detection as well. And in this way the default proxy selector give DIRECT all time, it is not able to find the right settings.
I think, the problem is that I want to set the
System.setProperty("java.net.useSystemProxies","true");
In the loaded class and somehow this does not work...But when I sysout the property value, it is true.
I wrote a little test program and the only difference is that:
A)
Class.forname("a"); -> a is in the classpath
B)
URL[] url = new URL[1];
url[0] = new URL("http://1.2.3.4/dtfw");
URLClassloader u = new URLClassloader(url);
Class.forname("a", true, u);
Both piece of code work, the only different is in the output.
Does anyone have some idea?
Thanks in advance!!
Zsomi
I found the root cause of the problem:
The DefaultProxySelector is singleton per JVM and the behavior of it based on if System.setProperty("java.net.useSystemProxies","true"); is set or not.
As the URLClassloader uses it, this value is false, when it is instantiated. And when in the "a" class I try to use it, it is not able to find system proxy settings, since at instatiation this property was not set.
So I have the cause, but no solution yet.
Regards,
Zsomi