I have a class FtpExample that calls a bean.
However for some reason when I package this class
as part of the "ftp" folder and I run the program it always gives me errors. Please note in the example FtpExample is inside a folder called "ftp".
Below is error:
C:\java-coding\ftpbean\FTP>java FtpExample
Exception in thread "main" java.lang.NoClassDefFoundError: FtpExample (wrong nam
e: ftp/FtpExample)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:509)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:246)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:262)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:322)
C:\java-coding\ftpbean-package-Example-inside\FTP>
It is strange the error is not given when I run the FtpExample class outside of the "ftp" package.
and thus instead of
package ftp;
I have statement
import ftp.*;
I want to use the "FtpExample.java" class inside the ftp package.
Code:
package ftp;
class FtpExample implements FtpObserver
{
FtpBean ftp;
long num_of_bytes = 0;
public FtpExample()
{
// Create a new FtpBean object.
ftp = new FtpBean();
}
// Connect to a ftp server.
public void connect()
{
try
{
ftp.ftpConnect("whale.wxx.ac.uk", "w***", "****");
} 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 pub and list its content.
public void listDirectory()
{
FtpListResult ftplrs = null;
try
{
// Go to directory
ftp.setDirectory("/home/eland/u6/k3074/w0109699/simple-bean");
// Get its directory content.
ftplrs = ftp.getDirectoryContent();
} catch(Exception e)
{
System.out.println(e);
}
// Print out the type and file name of each row.
while(ftplrs.next())
{
int type = ftplrs.getType();
if(type == FtpListResult.DIRECTORY)
System.out.print("DIR\t");
else if(type == FtpListResult.FILE)
System.out.print("FILE\t");
else if(type == FtpListResult.LINK)
System.out.print("LINK\t");
else if(type == FtpListResult.OTHERS)
System.out.print("OTHER\t");
System.out.println(ftplrs.getName());
}
}
// Get the file.
public void getFile()
{
try
{
// Get the binary file and save it to
// the name 'local_file_name' in the hard disk.
ftp.getBinaryFile("accessbean.jsp", "local_file_name", 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)
{
}
// Main
public static void main(String[] args)
{
FtpExample example = new FtpExample();
example.connect();
example.listDirectory();
example.getFile();
example.close();
}
}