0

I am trying to transform my XML file using XSLT in java application. Below is my code but it is giving error for classNot found. I tried to resolve this but the method is not in use after jdk 5. Can you please give me better approach to transform XML using XSLT in java program. Below is my code :

import java.io.File;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


class Transform {
    public static void main(String[] args) throws TransformerException {
    String stylesheetPathname = "C:/abc.xml";
    String inputPathname = "C:/scripts/transform.xslt";
    String outputPathname = "C:/abc_transformed.xml";

    TransformerFactory factory = TransformerFactory.newInstance();
    Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
    Transformer transformer = factory.newTransformer(stylesheetSource);
    Source inputSource = new StreamSource(new File(inputPathname).getAbsoluteFile());
    Result outputResult = new StreamResult(new File(outputPathname).getAbsoluteFile());
    transformer.transform(inputSource, outputResult);
   }
}

Error :

Exception in thread "main" javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.processor.TransformerFactoryImpl not found
at javax.xml.transform.TransformerFactory.newInstance(TransformerFactory.java:107)
at Transform.main(Transform.java:18)


Caused by: java.lang.ClassNotFoundException:org.apache.xalan.processor.TransformerFactoryImpl
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at javax.xml.transform.FactoryFinder.getProviderClass(FactoryFinder.java:119)
at javax.xml.transform.FactoryFinder.newInstance(FactoryFinder.java:182)
at javax.xml.transform.FactoryFinder.findJarServiceProvider(FactoryFinder.java:364)
at javax.xml.transform.FactoryFinder.find(FactoryFinder.java:286)
at javax.xml.transform.TransformerFactory.newInstance(TransformerFactory.java:101
3
  • Which java version are you using? Commented Sep 25, 2014 at 14:15
  • I am using jdk 1.7 . Java SE 7 Commented Sep 25, 2014 at 14:22
  • Any other option to transform xml using xslt in java will also do my work. Commented Sep 25, 2014 at 15:39

3 Answers 3

2

I have added xml-apis.jar,xercesImpl.jar and the error is resolved. Thanks for your efforts and help.

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure the jar file for that particular class file is included in your classpath.

It is part of the Xalan Jar

You can find the latest jar file here

http://xml.apache.org/xalan-j/

This should solve the ClassNotFoundException

1 Comment

I have added Xalan jar file but it was giving the same error. I tried using different versions of the file but it is not working.
0

It should be a service declaration somewhere in you code base, or it should have been falled back to the default xml factory.

A short answer would be to provide the xalan jar as classpath property. More informations can be found here.

4 Comments

I tried using jar but it is still giving same error.
Could you please add more details on the environment then, what kind of application? How do you launch it? Is there any dependencies that you are providing to the application?
It is single java program which i have created in eclipse. I have added external jar of xalan. When i am running this program it gives error. It dont have any other dependancy.
Try adding the classpath property to your manifest file, package it and run from a command line, don't run it within Eclipse IDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.