Jump to content
Changes to the Jaspersoft community edition download ×

.jasper to .jrxml


Recommended Posts

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



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.



package Converters; // my own package

import 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,

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)
// 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)




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).



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



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.



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:






Post edited by: redwine, at: 2007/06/05 16:05

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

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





Link to comment
Share on other sites

  • 2 weeks later...



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.



Post edited by: redwine, at: 2007/07/02 15:01

Link to comment
Share on other sites

  • 11 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...