We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Load Aspose via Custom ClassLoader

Hello,
I'd like to load the library jar from a file, so I can choose at runtime which aspose.slides version I'll use.

Below is a piece of code, which is working perfect. But, when I replace the "className" with "PresentationEx" to support PPTX files, the ExceptionInInitializerError is thrown.

private void load() throws Exception
{
String className = "com.aspose.slides.Presentation";
String jarName = "aspose.slides-7.6.0.jar";
File jarFile = new File("lib", jarName);
URL jar = jarFile.toURI().toURL();
ClassLoader loader = URLClassLoader.newInstance(
new URL[] {jar},
getClass().getClassLoader());
Class<?> clazz = Class.forName(className, false, loader);
File ppt = new File("examples", "background.ppt");
Constructor<?> con = clazz.getConstructor(InputStream.class);
Object obj = con.newInstance(new FileInputStream(ppt));
Method method = obj.getClass().getMethod("saveToPdf", OutputStream.class);
method.invoke(obj, new FileOutputStream(new File("examples", "out.pdf")));
}

This code doesn't work.

private void load() throws Exception
{
String className = "com.aspose.slides.PresentationEx";
String jarName = "aspose.slides-7.6.0.jar";
File jarFile = new File("lib", jarName);
URL jar = jarFile.toURI().toURL();
ClassLoader loader = URLClassLoader.newInstance(
new URL[] {jar},
getClass().getClassLoader());
Class<?> clazz = Class.forName(className, false, loader);
File ppt = new File("examples", "background.pptx");
Constructor<?> con = clazz.getConstructor(InputStream.class);
Object obj = con.newInstance(new FileInputStream(ppt));
Method method = obj.getClass().getMethod("save", OutputStream.class, Integer.class);
method.invoke(obj, new FileOutputStream(new File("examples", "out.pdf")), 1);
}

Could you tell me what is wrong?

Thank you,
Michal

Hi Michal,

I have worked with the sample code shared and have observed that you are using wrong class name when saving presentation. Instead of using Integer, please try using int. I have tried on my end in Windows 7 x64 with JDK 1.6_45 and have observed no issue at all.


private void loadPptx() throws Exception
{
String className = “com.aspose.slides.PresentationEx”;
String jarName = “aspose.slides-7.6.0.jar”;
File jarFile = new File(“lib”, jarName);
URL jar = jarFile.toURI().toURL();
ClassLoader loader = URLClassLoader.newInstance(
new URL[] {jar},
getClass().getClassLoader());
Class<?> clazz = Class.forName(className, false, loader);
File ppt = new File("D:\\Aspose Data\\", "background.pptx");
Constructor<?> con = clazz.getConstructor(InputStream.class);
Object obj = con.newInstance(new FileInputStream(ppt));
Method method = obj.getClass().getMethod(“save”, OutputStream.class, int.class);
method.invoke(obj, new FileOutputStream(new File(“D:\Aspose Data\”, “out.pdf”)), 1);
}

Many Thanks,