redwine Posted June 5, 2007 Share Posted June 5, 2007 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:package Converters; // my own packageimport net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.util.JRLoader;import net.sf.jasperreports.engine.xml.JRXmlWriter;class JasperToJrxml{ public static String fileName, sourcePath, outputPath; public static JasperReport jasperReport; public static void main(String [] args) { // base name of the file (no file extension) // refers to the name of the .jasper file and // used as the name for the generated .jrxml file fileName = "nameOfFile"; // source: .jasper file sourcePath = "C:\xxxx\yyyy\zzzz\jasper\" + fileName + ".jasper"; // output: .jrxml file outputPath = "C:\xxxx\yyyy\zzzz\jrxml\" + fileName + ".jrxml"; // using the source file on "sourcePath", // generate the .jrxml file and place it on "outputPath" generateJrxmlFile(fileName, sourcePath, outputPath); } public static void generateJrxmlFile(String fn, String sp, String op) { try { // load the .jasper file on "sourcePath" (cast to JasperReport object) jasperReport = (JasperReport) JRLoader.loadObject(sourcePath); // write the xml code generated from the JasperReport object // into "outputPath" using "UTF-8" encoding JRXmlWriter.writeReport(jasperReport, outputPath, "UTF-8"); } catch(JRException e) { e.printStackTrace(); } }} OBSERVATIONS: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.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). OTHER JARS: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:- commons-collections-3.2.jar- commons-digester-3.8.jar- commons-logging-1.1.jar- commons-io-1.3.1.jar MODIFICATIONS: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. REFERENCE THREAD:the code above was made with the help of Lucian Chirita, senior software engineer of JasperReports, who gave us advise on what should actually appear in there. see the following url:http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=&func=view&catid=8&id=25854#25854 EdwinPost edited by: redwine, at: 2007/06/05 16:05 Link to comment Share on other sites More sharing options...
nivanovich Posted June 12, 2007 Share Posted June 12, 2007 Thank you very much Redwine. You saved my day :) Link to comment Share on other sites More sharing options...
mfa Posted June 18, 2007 Share Posted June 18, 2007 Hi masterSir 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.jarat 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 Link to comment Share on other sites More sharing options...
redwine Posted July 2, 2007 Author Share Posted July 2, 2007 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. edwinPost edited by: redwine, at: 2007/07/02 15:01 Link to comment Share on other sites More sharing options...
aggmeister Posted March 11, 2019 Share Posted March 11, 2019 Hello Mr. Redwine, can you tell me how can i run your scritp, whats changes can i perform please. Im a java noobie. Regards, Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now