0

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.

InvocationTargetException

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;
      } 

}

Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80
  • Please add code and stack trace. The bug is likely not in ClassLoaders but in your code (another line in the stack trace) – Daniel Alder Aug 04 '16 at 11:17
  • I have added code. For stack trace: "ClassLoaders.callStaticFunction(String, String, String, String, String, String[], Object[]) line: not available " This is all I get – Dheeraj Kumar Aug 04 '16 at 11:26
  • Can anyone help me with this?? – Dheeraj Kumar Aug 08 '16 at 07:37
  • I'm afraid no. It's just not enough. If it's really the only one entry in the stack trace it's hard to find out. But I believe the exception has more information than you provide. Normally, an exception has at least a message... (https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getMessage()), maybe it's a good idea to catch the exception once and run printStackTrace on it – Daniel Alder Aug 08 '16 at 10:32
  • This exception doesnt occur when I run my code in Eclipse, it only occurs when I run code in Eclipse's remote debugging mode from my application. And this is all I get as error – Dheeraj Kumar Aug 08 '16 at 10:36
  • Whenever I debug a project in eclipse and there's an exception, the execution gets suspended (similar to your screenshot above). Then I click Resume (F8) and the stack trace gets printed to the console. Unfortunately, I don't have any chance for trying remote debugging, but I'm quite sure it behaves similar. Alternatively, you can run your application without debugging once... – Daniel Alder Aug 08 '16 at 12:14
  • On F8, I get nothing, It gets stuck and does nothing further :( Running from application doesn't throw any exception but stops at the point where I get exception while remote debugging. I had to do remote debugging because running from application doesn't throw any error. – Dheeraj Kumar Aug 08 '16 at 12:30
  • @DanielAlder Can you please check this, I have reasked this with in more detail. http://stackoverflow.com/questions/38829942/invocationtargetexception-at-classloaders-callstaticfunction-java-eclipse.. – Dheeraj Kumar Aug 09 '16 at 07:04
  • What technology/commands do you use for the remote call? maybe the following helps (on caller side): `try{}catch(InvocationTargetException e){e. getTargetException().printStackTrace();}` – Daniel Alder Aug 09 '16 at 08:22
  • I have added -Xdebug -Xrunjdwp:transport=dt_socket,address=192.168.1.102:8889,server+ =y in ini file of application which listens at port and from eclipse I attach and start debuging – Dheeraj Kumar Aug 09 '16 at 08:37
  • Could it be issue with classpath or manifest etc?? – Dheeraj Kumar Aug 09 '16 at 10:02

0 Answers0