java.net.MalformedURLException

2

I'm having a horrible problem and worst of all is that on the web I find almost nothing about this! I'm doing a report in java with jaspersoft studio. I made the template already (it has two parameters) and followed the guide of the following page .

Ok, my code is this:

public class Reportes{
private Administrador admin;

public Reportes() throws ClassNotFoundException, SQLException{
    admin = new Administrador();
}

public JRViewer obtenerPaciente(String ced) throws JRException{
    HashMap parameters = new HashMap();
    InputStream is = getClass().getResourceAsStream("assets/PerfilPaciente.jrxml");
    JRDesignQuery design = new JRDesignQuery();
    design.setText("SELECT * FROM Pacientes");
    JasperDesign jd = JRXmlLoader.load(is);
    jd.setQuery(design);
    parameters.put("Imagen", ClassLoader.getSystemResource("assets/perfil-azul.png").getPath());
    parameters.put("Cedula",ced);
    JasperReport report = JasperCompileManager.compileReport(jd);
    JasperPrint jp = JasperFillManager.fillReport(report, parameters, admin.obtenerConexion());
    JRViewer jrv = new JRViewer(jp);             
    return jrv;
}
}

And when I call this function in my JDialog, I get the following error:

  

net.sf.jasperreports.engine.JRException: java.net.MalformedURLException       at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML (JRXmlLoader.java:251)       at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML (JRXmlLoader.java:230)       at net.sf.jasperreports.engine.xml.JRXmlLoader.load (JRXmlLoader.java:218)

The following line of code marks me:

  

InputStream is = getClass (). getResourceAsStream ("assets / Patient Profile.jrxml");

I tried to put it "/ProfilePatient.jrml", "ProfilePatient.jrxml" and nothing. Nor because it is in the project folder.

How could I solve this?

PS: My resources I put everything inside a folder of the project that creates called "assets", from there some images of my program in java come out also, and it has not given me problems like this file jrxml.

    
asked by TwoDent 01.11.2016 в 19:14
source

1 answer

2

Already with what you came up I understood better ... there are two ways to make it one is to copy the jrxml in the jar but for that it must be inside the folder src / assets / ProfilePaciente.jrxml and with the code you have it would work .

Since what you are trying to do is that the resource is inside the jar.

Or you could also make this as a resource instead of loading it like this:

 InputStream is = getClass().getResourceAsStream("assets/PerfilPaciente.jrxml");

Load it in the following way, taking into account that when you upload the application there will be a folder where your program called assets is executed:

InputStream is = new FileInputStream("assets/PerfilPaciente.jrxml");

Remember to add the FileInputStream import.

 import java.io.FileInputStream; 

And as a cultural fact you are using ant.

    
answered by 01.11.2016 / 19:46
source