Skip to content

Instantly share code, notes, and snippets.

@ngo
Last active June 20, 2018 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngo/9c832119868ff8009b45e783babd2616 to your computer and use it in GitHub Desktop.
Save ngo/9c832119868ff8009b45e783babd2616 to your computer and use it in GitHub Desktop.
SAX sucks
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class Main {
public static void main(String[] args) {
try {
URL[] classUrls = {new URL("file:///path/to/jar/with/separate-main.jar")};
ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
URLClassLoader urlcl = new URLClassLoader(classUrls, oldcl);
Class sepMain = Class.forName("SeparateMain", true, urlcl);
Object instance = sepMain.newInstance();
Method do_stuff = sepMain.getDeclaredMethod("do_stuff");
do_stuff.invoke(instance);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
SeparateMain.do_stuff
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at Main.main(Main.java:43)
Caused by: org.xml.sax.SAXException: SAX2 driver class org.apache.xerces.parsers.SAXParser not found
java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser
at java.xml/org.xml.sax.helpers.XMLReaderFactory.loadClass(XMLReaderFactory.java:200)
at java.xml/org.xml.sax.helpers.XMLReaderFactory.createXMLReader(XMLReaderFactory.java:191)
at SeparateMain.do_stuff(SeparateMain.java:9)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.xml/org.xml.sax.helpers.NewInstance.newInstance(NewInstance.java:91)
at java.xml/org.xml.sax.helpers.XMLReaderFactory.loadClass(XMLReaderFactory.java:198)
... 7 more
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;
import static java.lang.Class.forName;
public class SeparateMain {
public void do_stuff() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SAXException {
System.out.println("SeparateMain.do_stuff");
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
}
}
// This should be compiled to a jar which should also include classes from xercesImpl.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment