InvocationTargetException for ClassLoaders.callSta

2020-05-09 00:57发布

问题:

I have created a program to convert text to xml by using ReverseXSL API.

This program is to be executed by an application by calling static method (static int transformXSL).

I am able to execute and produce output with running from Eclipse. However, When I ran program (jar) by using application it stuck somewhere and I couldnt find anything.

Then, I debugged by "Debug as...-> Remote Java Application" in Eclipse from Application and found "InvocationTargetException" at ClassLoaders.callStaticFunction.

Below Static method is called by application.

public class MyTest4 {

public MyTest4()
{

}

public static int transformXSL(String defFile, String inputFile, String XSLFile, String OutputFile) {

      System.out.println("Dheeraj's method is called");

    // start time

      FileWriter fw=null;
    try {
        fw = new FileWriter("D://Countime.txt");
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
        BufferedWriter output=new BufferedWriter(fw);
        DateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date dt= new Date();
        System.out.println("Date is calculated");

        try {
            output.write("Start Time:"+sd.format(dt).toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        System.out.println(sd.format(dt));

    FileReader myDEFReader=null, myXSLReader=null;
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t=null;
    FileInputStream inStream = null;
    ByteArrayOutputStream outStream = null;

    // Step 1:

    //instantiate a transformer with the specified DEF and XSLT

    if (new File(defFile).canRead())
    {
        try {
            myDEFReader = new FileReader(defFile);
            System.out.println("Definition file is read");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

   }
    else myDEFReader = null;

    if (new File(XSLFile).canRead())
        try {
            myXSLReader = new FileReader(XSLFile);
            System.out.println("XSL file is read");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
    else myXSLReader = null;

    try {

        t = tf.newTransformer(myDEFReader, myXSLReader);
    } catch (IOException e) {

        e.printStackTrace();
    }

    System.out.println("Step 1: DEF AND XSLT Transformation completed");

    // Step 2:
    // Read Input data

    try {
        inStream = new FileInputStream(inputFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    outStream = new ByteArrayOutputStream();

    System.out.println("Step 2: Reading Input file:  completed");

    // Step 3:

    // Transform Input

                try {
                    try (BufferedReader br = new BufferedReader(new FileReader("D://2.txt"))) {
                           String line = null;
                           while ((line = br.readLine()) != null) {
                               System.out.println("Content: "+line);
                           }
                        }

                    System.out.println("File: "+inputFile.toString());
                    System.out.println("\n content: \n"+ inStream.toString());
                    System.out.println("Calling Transform Function");
                    t.transform(inStream, outStream);
                    System.out.println("Transformation is called");
                    outStream.close();
                    try(OutputStream outputStream = new FileOutputStream(OutputFile)) {
                        outStream.writeTo(outputStream);

                       System.out.println("Outstream is generated; Output file is creating");
                    }
                    System.out.println(outStream.toString());
                } catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FactoryConfigurationError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (TransformerFactoryConfigurationError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (javax.xml.transform.TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                   System.out.println("output file is created");
    // End time

                Date dt2= new Date();
                System.out.println(sd.format(dt2));

                    System.out.println("End time:"+dt2.toString());
                try {
                    output.append("End Time:"+sd.format(dt2).toString());
                    output.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            return 0;
      } 

}