Last active
June 20, 2018 23:55
-
-
Save ngo/9c832119868ff8009b45e783babd2616 to your computer and use it in GitHub Desktop.
SAX sucks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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