we have a project using jasperreport4.7 and jdk6, and now we have to upgrade jdk6 to jdk8.
we noticed that jdk8 doen't support jasperreport4.7 , so we use the code below to avoid security check, and it worked.print is ok.
AccessController.doPrivileged(new PrivilegedAction()[/code]
but when using preview, it doen't work, the preview window can be opened but there is noting on it, and the whole window sized up. any suggustiong??
sorce:
package smp.print;
import java.awt.Container;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.Policy;
import java.security.PrivilegedAction;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.util.JRLoader;
public class PrinterApplet
extends JApplet
{
private static final long serialVersionUID = 3682154937205202208L;
private String url;
private JasperPrint jasperPrint;
private JPanel pnlMain;
public PrinterApplet()
{
initComponents();
}
public void init()
{
System.out.println("init-start");
String pathMessage = WritePolicy.writeFile();
if ("".equals(pathMessage)) {
System.out.println("init-writeFile sucess!!");
} else {
System.out.println("init-writeFile error!! errorMsg->" + pathMessage);
}
Policy.getPolicy().refresh();
System.out.println("init-end");
}
private void initComponents()
{
System.out.println("initComponents-start");
this.pnlMain = new JPanel();
getContentPane().add(this.pnlMain, "West");
System.out.println("initComponents-start");
}
public void print()
{
System.out.println("print-start");
URL url = null;
try
{
System.out.println("print-1");
url = new URL(getCodeBase(), getUrl());
}
catch (MalformedURLException e1)
{
System.out.println("print-2");
JOptionPane.showMessageDialog(this, e1.getMessage());
}
if (url != null)
{
if (this.jasperPrint == null) {
try
{
System.out.println("print-3");
this.jasperPrint = ((JasperPrint)JRLoader.loadObject(url));
}
catch (Exception e)
{
System.out.println("print-4");
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
if (this.jasperPrint != null)
{
final JasperPrint print = this.jasperPrint;
try
{
System.out.println("printWithNameCount-printing");
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
JasperPrintManager.printReport(print, true);
return null;
} catch (Exception ex) {
return ex.getStackTrace();
}
}
});
this.jasperPrint = null;
}
catch (Exception e)
{
System.out.println("print-5");
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(null, swriter.toString());
}
}
else
{
System.out.println("print-6");
JOptionPane.showMessageDialog(this, "Empty report.");
}
}
else
{
System.out.println("print-7");
JOptionPane.showMessageDialog(this, "Source URL not specified");
}
System.out.println("print-end");
}
public void printWithNameCount(final String printerName, final String copies)
{
System.out.println("printWithNameCount-start");
URL url = null;
try
{
System.out.println("printWithNameCount-1");
url = new URL(getCodeBase(), getUrl());
}
catch (MalformedURLException e1)
{
System.out.println("printWithNameCount-2");
JOptionPane.showMessageDialog(this, e1.getMessage());
}
if (url != null)
{
if (this.jasperPrint == null) {
try
{
System.out.println("printWithNameCount-3");
this.jasperPrint = ((JasperPrint)JRLoader.loadObject(url));
}
catch (Exception e)
{
System.out.println("printWithNameCount-4");
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
if (this.jasperPrint != null)
{
final JasperPrint print = this.jasperPrint;
try
{
System.out.println("printWithNameCount-printing");
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
JasperPrintManagerEx.printReport(print, printerName, Integer.parseInt(copies), true);
return null;
} catch (Exception ex) {
return ex.getStackTrace();
}
}
});
this.jasperPrint = null;
}
catch (Exception e)
{
System.out.println("printWithNameCount-5");
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(null, swriter.toString());
}
}
else
{
System.out.println("printWithNameCount-6");
JOptionPane.showMessageDialog(this, "Empty report.");
}
}
else
{
System.out.println("printWithNameCount-7");
JOptionPane.showMessageDialog(this, "Source URL not specified");
}
System.out.println("printWithNameCount-end");
}
public void printWithPageSize(final String printerName, final boolean pageKind)
{
System.out.println("printWithPageSize-start");
URL url = null;
try
{
System.out.println("printWithPageSize-1");
url = new URL(getCodeBase(), getUrl());
}
catch (MalformedURLException e1)
{
System.out.println("printWithPageSize-2");
JOptionPane.showMessageDialog(this, e1.getMessage());
}
if (url != null)
{
if (this.jasperPrint == null) {
try
{
System.out.println("printWithPageSize-3");
this.jasperPrint = ((JasperPrint)JRLoader.loadObject(url));
}
catch (Exception e)
{
System.out.println("printWithPageSize-4");
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
if (this.jasperPrint != null)
{
final JasperPrint print = this.jasperPrint;
try
{
System.out.println("printWithPageSize-printing");
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
JasperPrintManagerEx.printReportWithPgeSize(print, printerName, pageKind, true);
return null;
} catch (Exception ex) {
return ex.getStackTrace();
}
}
});
this.jasperPrint = null;
}
catch (Exception e)
{
System.out.println("printWithPageSize-5");
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(null, swriter.toString());
}
}
else
{
System.out.println("printWithPageSize-6");
JOptionPane.showMessageDialog(this, "Empty report.");
}
}
else
{
System.out.println("printWithPageSize-7");
JOptionPane.showMessageDialog(this, "Source URL not specified");
}
System.out.println("printWithPageSize-end");
}
public void view()
{
System.out.println("view-start");
URL url = null;
try
{
System.out.println("view-1");
url = new URL(getCodeBase(), getUrl());
}
catch (MalformedURLException e1)
{
System.out.println("view-2");
JOptionPane.showMessageDialog(this, e1.getMessage());
}
if (url != null)
{
try
{
if (this.jasperPrint == null)
{
System.out.println("view-3");
this.jasperPrint = ((JasperPrint)JRLoader.loadObject(url));
}
if (this.jasperPrint != null)
{
System.out.println("printWithNameCount-viewing");
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
ViewerFrame viewerFrame = new ViewerFrame(PrinterApplet.this.getAppletContext(), PrinterApplet.this.jasperPrint);
viewerFrame.setVisible(true);
return null;
} catch (Exception ex) {
return ex.getStackTrace();
}
}
});
this.jasperPrint = null;
}
else
{
System.out.println("view-4");
JOptionPane.showMessageDialog(this, "Empty report.");
}
}
catch (Exception e)
{
System.out.println("view-5");
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
else
{
System.out.println("view-6");
JOptionPane.showMessageDialog(this, "Source URL not specified");
}
System.out.println("view-end");
}
public String getUrl()
{
return this.url;
}
public void setUrl(String url)
{
this.url = url;
}
}