Bonjour, j'ai une applet avec une class print et une class interne qui fait du FTP, quand je compil il n'y a pas d'erreur et quand je la charge dans mon navigateur il n'y a pas d'erreur, l'erreur vient kan je clic sur le bouton pour récupérer le fichier et l'imprimer. Voici mon code d'erreur et mon code source si kelkun pe m'aider, merci d'avance.
Exception occurred during event dispatching:
java.lang.NoClassDefFoundError: ftp/FtpObserver
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
.....
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;
import java.awt.print.*;
import javax.print.PrintService;
import java.util.StringTokenizer;
import ftp.*;
public class Test extends Applet implements ActionListener, Printable{
private boolean isStandalone = false;
private static String txt;
private String textToPrint = txt;
FtpBean ftp;
long num_of_bytes = 0;
Button test;
//applet constructor
public Test() {
super();
}
//get parameter
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//get information applet
public String getAppletInfo() {
return "Test";
}
//get information parameter
public String[][] getParameterInfo() {
return null;
}
//initialisation
public void init() {
setBackground(Color.white);
test = new Button("Test");
add(test);
test.addActionListener(this);
}
public class Ftp implements FtpObserver {
public Ftp() {
// Create a new FtpBean object.
ftp = new FtpBean();
}
// Connect to a ftp server.
public void connect() {
try {
ftp.ftpConnect("127.0.0.1", "log", "pas");
}
catch (Exception e) {
System.out.println(e);
}
}
// Close connection
public void close() {
try {
ftp.close();
}
catch (Exception e) {
System.out.println(e);
}
}
// Go to directory.
public void listDirectory()
{
FtpListResult ftplrs = null;
try
{
// Go to directory '/test'.
ftp.setDirectory("/");
// Get its directory content.
ftplrs = ftp.getDirectoryContent();
} catch(Exception e)
{
System.out.println(e);
}
}
// Get the file.
public void getFile()
{
try
{
// Get the file 'test.txt' and save it to
// the name 'test1.txt' in the hard disk.
ftp.getAsciiFile ("test.txt", "test1.txt","\r", this);
} catch(Exception e)
{
System.out.println(e);
}
}
// Implemented for FtpObserver interface.
// To monitor download progress.
public void byteRead(int bytes) {
num_of_bytes += bytes;
//System.out.println(num_of_bytes + " of bytes read already.");
}
// Needed to implements by FtpObserver interface.
public void byteWrite(int bytes) {
}
}
//event
public void actionPerformed(ActionEvent e) {
if (e.getSource() == test) {
Ftp connect = new Ftp();
connect.connect();
connect.listDirectory();
connect.getFile();
connect.close();
setText();
textToPrint = txt;
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();
double ph = paper.getHeight();
double pw = paper.getWidth();
int x = 36;
int y = 36;
paper.setImageableArea( (double) x, (double) y, pw - 2 * (double) x,
ph - 2 * (double) y);
pf.setPaper(paper);
Book book = new Book();
Test psb = new Test();
book.append(psb, pf); // print
pj.setPageable(book);
if (pj.printDialog())
try {
pj.print();
}
catch (PrinterException pe) {
System.out.println("Print Error");
}
}
}
//print
public int print(Graphics g, PageFormat format, int pageIndex) {
if (pageIndex >= 1)
return Printable.NO_SUCH_PAGE;
int ix = (int) format.getImageableX();
int iy = (int) format.getImageableY();
FontMetrics fM = g.getFontMetrics();
int ascent = (int) fM.getAscent();
if (pageIndex < 1) {
g.setColor(Color.black);
g.drawString(textToPrint, ix, iy + ascent);
}
return Printable.PAGE_EXISTS;
}
private String getPrinterInfo(PrinterJob pj) {
PrintService printer = pj.getPrintService(); // get printer
PageFormat pageFormat = pj.defaultPage();
Paper paper = pageFormat.getPaper();
return printer.toString(); // get printername
}
private String getPrinterName(String s) {
StringTokenizer sTok = new StringTokenizer(s, ":");
String printerName = s;
if (sTok.countTokens() > 1) {
while (sTok.hasMoreTokens()) {
printerName = sTok.nextToken();
}
}
return printerName;
}
//read the file
public void setText() {
try {
FileReader text = new FileReader("c://test.txt");
int caractere;
do {
caractere = text.read();
if (caractere != -1) {
txt += (char) caractere;
}
}
while (caractere != -1);
}
catch (FileNotFoundException e) {
System.out.println("Exception fichier non trouvé : " + e.getMessage());
}
catch (IOException e) {
System.out.println("Exception entrée/sortie : " + e.getMessage());
}
}
}