.jasper to .jrxml

Java code to generate a .jrxml file from an existing .jasper file.

JAVA CODE:
in the hope of helping those who use JasperReports and for the benefit of those who have similar requirements to what we have, i am posting the java code (JasperToJrxml.java) for the converter/generator that we made for the purpose of generating a .jrxml file using an existing .jasper file. see the attached file. please experiment on it and if anyone happens to come up with improvements (i am sure there is a lot more to be improved) or if the code was able to help you, please post your observations & comments on this thread.

Code:
<br />
package Converters; // my own package<br />
<br />
<br />
import net.sf.jasperreports.engine.JasperReport;<br />
import net.sf.jasperreports.engine.JRException;<br />
import net.sf.jasperreports.engine.util.JRLoader;<br />
import net.sf.jasperreports.engine.xml.JRXmlWriter;<br />
<br />
<br />
class JasperToJrxml<br />
{<br />
   public static String fileName,<br />
                        sourcePath,<br />
                        outputPath;<br />
                        <br />
   public static JasperReport jasperReport; <br />
   <br />
   <br />
   public static void main(String [] args) {<br />
      // base name of the file (no file extension)<br />
      //    refers to the name of the .jasper file and<br />
      //    used as the name for the generated .jrxml file<br />
      fileName   = "nameOfFile";  <br />
         <br />
      // source: .jasper file<br />
      sourcePath = "C:\\xxxx\\yyyy\\zzzz\\jasper\\" <br />
                 + fileName + ".jasper"; <br />
                <br />
      // output: .jrxml file<br />
      outputPath = "C:\\xxxx\\yyyy\\zzzz\\jrxml\\" <br />
                 + fileName + ".jrxml";  <br />
      <br />
      // using the source file on "sourcePath",  <br />
      //    generate the .jrxml file and place it on "outputPath"<br />
      generateJrxmlFile(fileName, sourcePath, outputPath);<br />
   }<br />
   <br />
   <br />
   public static void generateJrxmlFile(String fn, String sp, String op)<br />
   {<br />
      try<br />
      {<br />
         // load the .jasper file on "sourcePath" (cast to JasperReport object)<br />
         jasperReport = (JasperReport) JRLoader.loadObject(sourcePath);<br />
         <br />
         // write the xml code generated from the JasperReport object<br />
         //    into "outputPath" using "UTF-8" encoding<br />
         JRXmlWriter.writeReport(jasperReport, outputPath, "UTF-8");<br />
      }<br />
      catch(JRException e)<br />
      {<br />
         e.printStackTrace();<br />
      }<br />
   }<br />
}<br />
</td></tr></tbody></table><br />
<br />
<br />
<b>OBSERVATIONS:</b><br />
we were initially using the JasperReports v.1.3.3 jar but we couldn't generate the .jrxml file for .jasper file that was quite old using the new jar. we could only generate the .jrxml files from newer .jasper files.<br />
to be able to generate the .jrxml file from a fairly old .jasper file (created with JR v.1.2.4), we had to remove the new jar (v.1.3.3) and replace it with the 1.2.4 jar in our buildpath (eclipse).<br />
<br />
<b>OTHER JARS:</b><br />
i am not sure if this only applies to our case... aside from the Jasper Reports jar (v.1.2.4) that we already have on our buildpath, we also had to include the following jars (Apache Commons) since eclipse was generating several errors that prompted us to search for these jars:<br />
- commons-collections-3.2.jar<br />
- commons-digester-3.8.jar<br />
- commons-logging-1.1.jar<br />
- commons-io-1.3.1.jar<br />
<br />
<b>MODIFICATIONS:</b><br />
this might be an eclipse issue but we are still trying to find out how to make the code compile and run for a .jasper file whose version is not known. we are tring it out by simply using the newest jar to including all the old JasperReports jars. no luck yet. however, i hope this helps someone in need of this information.<br />
<br />
<b>REFERENCE THREAD:</b><br />
the code above was made with the help of <b>Lucian Chirita</b>, senior software engineer of JasperReports, who gave us advise on what should actually appear in there. see the following url:<br />
<a href="http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=&func=view&catid=8&id=25854#25854" target="_blank">http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=&func=view&catid=8&id=25854#25854</a><br><br />
<br />
<br />
Edwin<br>Post edited by: redwine, at: 2007/06/05 16:05
redwine's picture
2319
Joined: Mar 7 2007 - 1:25am
Last seen: 16 years 7 months ago

4 Answers:

Thank you very much Redwine. You saved my day :)
nivanovich's picture
Joined: Jun 6 2007 - 12:32pm
Last seen: 9 years 10 months ago
Hi master
Sir I use sun java studio creator with oracle and for reporting I use ireport


Sir I use this code for creating the pdf file

See my cod


public String button1_action() {
Connection conn = null ;
log("*********** connecting");
try {
javax.naming.Context ctx = new javax.naming.InitialContext() ;
DataSource ds = (DataSource)ctx.lookup(" java:comp/env/jdbc/datasource1" ) ;
conn = ds.getConnection() ;
} catch (Exception ex) {
error(" Error counting rows: " + ex.getMessage() );
log(" Error counting rows: " + ex.getMessage() );
}

JasperReport jasperReport;
JasperPrint jasperPrint;

log("compiling report");
try {
jasperReport = JasperCompileManager.compileReport("C:/mfa.jasper");

} catch (Exception ex) {
log(" ********* Error compiling report: " + ex.getMessage() );
}

return null;
}


but compliemanager give me error


1. Invalid byte 1 of 1-byte UTF-8 sequence


see my error ( Invalid byte 1 of 1-byte UTF-8 sequence ) detail [#|2007-06-16T15:22:44.761+0500|INFO|sun-appserver-pe8.2|javax.enterprise.system.container.web|_ThreadID=21;|WebModule[/WebApplication17] ********* Error compiling report: com.sun.org.apach by mfa



2. Illegal character in path at index 18:

Caused by: java.net.URISyntaxException: Illegal character in path at index 18: file:/C:/Documents and Settings/Administrator/My Documents/Creator/Projects/WebApplication15/build/web/WEB-INF/lib/log4j-core.jar
at java.net.URI$Parser.fail(URI.java:2809)
at java.net.URI$Parser.checkChars(URI.java:2982)
at java.net.URI$Parser.parseHierarchical(URI.java:3066)
at java.net.URI$Parser.parse(URI.java:3014)
at java.net.URI.(URI.java:578)
at com.sun.enterprise.loader.EJBClassLoader$URLEntry.init(EJBClassLoader.java:848)
... 16 more
|#]




Please give me idea how I create pdf file

Thank’s

aamir
mfa's picture
mfa
184
Joined: May 9 2007 - 2:11pm
Last seen: 16 years 4 months ago
aamir,

sorry i was not able to reply sooner... i was on vacation.

regarding your question about the invalid character error, the error may be caused by an unescaped separator character in the file path.

i used two backslashes in my path. it has something to do with java regular expressions requiring double backslashes in the file path... the first one used as an escape character and the second one as the actual separator character.

also note that linux (unix) and windows use different path separator characters. linux uses the forward slash character while windows uses the backslash character.

hope this helps if you have not already found the answer to your question.

edwin
Post edited by: redwine, at: 2007/07/02 15:01
redwine's picture
2319
Joined: Mar 7 2007 - 1:25am
Last seen: 16 years 7 months ago

Hello Mr. Redwine, can you tell me how can i run your scritp, whats changes can i perform please. Im a java noobie.

 

Regards, 

aggmeister's picture
Joined: Dec 12 2018 - 8:04am
Last seen: 4 years 6 months ago
Feedback
randomness