简单的例子:
package mypackage1;
public class Class1
{
public Class1()
{
} public static void main(String[] args){
for(int i=0;i<args.length;i++){
System.out.println(args);
}
}
}
执行命令:
java mypackage1.Class1 aaa bbb ccc ddd "EEE"
执行结果:
aaa
bbb
ccc
ddd
EEE
下面是个jasper的例子:
/* Teodor Danciu
* 173, Calea Calarasilor, Bl. 42, Sc. 1, Ap. 18
* Postal code 030615, Sector 3
* Bucharest, ROMANIA
* Email: teodord@users.sourceforge.net
*/
importdo
ri.jasper.engine.*;
importdo
ri.jasper.engine.export.*;
importdo
ri.jasper.engine.util.*;
importdo
ri.jasper.view.*;
import java.util.*;
import java.sql.*;
import java.io.*;
import java.awt.*;
/**
*
*/
public class JasperApp
{
/**
*
*/
private static final String TASK_COMPILE = "compile";
private static final String TASK_FILL = "fill";
private static final String TASK_PRINT = "print";
private static final String TASK_PDF = "pdf";
private static final String TASK_XML = "xml";
private static final String TASK_XML_EMBED = "xmlEmbed";
private static final String TASK_HTML = "html";
private static final String TASK_XLS = "xls";
private static final String TASK_CSV = "csv";
private static final String TASK_RUN = "run";
/**
*
*/
public static void main(String[] args)
{
String fileName = null;
String taskName = null;
if(args.length == 0)
{
usage();
return;
}
int k = 0;
while ( args.length > k )
{
if ( args[k].startsWith("-T") )
taskName = args[k].substring(2);
if ( args[k].startsWith("-F") )
fileName = args[k].substring(2);
k++;
}
try
{
long start = System.currentTimeMillis();
if (TASK_COMPILE.equals(taskName))
{
JasperCompileManager.compileReportToFile(fileName);
System.err.println("Compile time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_FILL.equals(taskName))
{
//Preparing parameters
Image image = Toolkit.getDefaultToolkit().createImage("dukesign.jpg");
MediaTracker traker = new MediaTracker(new Panel());
traker.addImage(image, 0);
try
{
traker.waitForID(0);
}
catch (Exception e)
{
e.printStackTrace();
}
Map parameters = new HashMap();
parameters.put("ReportTitle", "The First Jasper Report Ever");
parameters.put("MaxOrderID", new Integer(10500));
parameters.put("SummaryImage", image);
JasperFillManager.fillReportToFile(fileName, parameters, getConnection());
System.err.println("Filling time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_PRINT.equals(taskName))
{
JasperPrintManager.printReport(fileName, true);
System.err.println("Printing time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_PDF.equals(taskName))
{
JasperExportManager.exportReportToPdfFile(fileName);
System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_XML.equals(taskName))
{
JasperExportManager.exportReportToXmlFile(fileName, false);
System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_XML_EMBED.equals(taskName))
{
JasperExportManager.exportReportToXmlFile(fileName, true);
System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_HTML.equals(taskName))
{
JasperExportManager.exportReportToHtmlFile(fileName);
System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_XLS.equals(taskName))
{
File sourceFile = new File(fileName);
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporter.exportReport();
System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_CSV.equals(taskName))
{
File sourceFile = new File(fileName);
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".csv");
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.exportReport();
System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
if (TASK_RUN.equals(taskName))
{
//Preparing parameters
Image image = Toolkit.getDefaultToolkit().createImage("dukesign.jpg");
MediaTracker traker = new MediaTracker(new Panel());
traker.addImage(image, 0);
try
{
traker.waitForID(0);
}
catch (Exception e)
{
e.printStackTrace();
}
Map parameters = new HashMap();
parameters.put("ReportTitle", "The First Jasper Report Ever");
parameters.put("MaxOrderID", new Integer(10500));
parameters.put("SummaryImage", image);
JasperRunManager.runReportToPdfFile(fileName, parameters, getConnection());
System.err.println("PDF running time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
{
usage();
System.exit(0);
}
}
catch (JRException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/**
*
*/
private static void usage()
{
System.out.println( "JasperApp usage:" );
System.out.println( "/tjava JasperApp -Ttask -Ffile" );
System.out.println( "/tTasks : compile | fill | print | pdf | xml | xmlEmbed | html | xls | csv | run" );
}
/**
*
*/
private static Connection getConnection() throws ClassNotFoundException, SQLException
{
//Change these settings according to your local configuration
String driver = "org.hsqldb.jdbcDriver";
String connectString = "jdbc:hsqldb:hsql://localhost";
String user = "sa";
String password = "";
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectString, user, password);
return conn;
}
}