Reputation: 27
I have this small ftp java code using which I am trying to access the files in my ubuntu machine in vmware's directory. But I keep getting this error:
Current directory is /home/username/Documents
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/oro/text/regex/MalformedPatternException
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createUnixFTPEntryParser(DefaultFTPFileEntryParserFactory.java:169)
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:94)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2358)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2141)
at edp_ftp_client.FtpClientMain.main(FtpClientMain.java:54)
Caused by: java.lang.ClassNotFoundException: org.apache.oro.text.regex.MalformedPatternException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
Code:
public class FtpClientMain {
public static void main(String[] args) {
String server = "hostname.example.com";
int port = 21;
String user = "username";
String pass = "password";
String directory = "/home/username/Documents/";
String dwn_directory = "C:/Users/username/Desktop/files/";
String f_name = "image";
String filename;
String extention = ".jpg";
String full_name, dwn_full_name;
int rc = 0;
int dir_found = 0, file_found = 0;
int exit = 0;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
for(int i = 1; i<50 ;i++) {
full_name = directory + f_name + i + extention;
dwn_full_name = dwn_directory + f_name + i + extention;
filename = f_name + i + extention;
ftpClient.changeWorkingDirectory(directory);
rc = ftpClient.getReplyCode();
if(rc == 550) {
System.out.println("Directory not found");
break;
}
System.out.println("Current directory is " + ftpClient.printWorkingDirectory());
//get list of filenames
FTPFile[] ftpFiles = ftpClient.listFiles(ftpClient.printWorkingDirectory());
if (ftpFiles != null && ftpFiles.length > 0) {
//loop thru files
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
System.out.println("Found a file");
System.out.println("File is " + file.getName());
//get output stream
OutputStream output;
output = new FileOutputStream("FtpFiles" + "/" + file.getName());
//get the file from the remote system
ftpClient.retrieveFile(file.getName(), output);
//close output stream
output.close();
//delete the file
// ftp.deleteFile(file.getName());
}
}
/*File download_file = new File(dwn_full_name);
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(download_file));
boolean success = ftpClient.retrieveFile(full_name, outputStream);
outputStream.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}*/
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
I am able to download a file but I am not able to list all the files present in the directory. I am working on windows 8.1 home edition and running a vmware virtual machine with ubuntu operating system.
Upvotes: 0
Views: 4018
Reputation: 403441
The stack trace shows that you're missing a dependency on the ancient and totally obsolete Jakarta ORO library.
Commons-Net 1.4 (which you mentioned you're using) is also ancient, which is why it depends on ORO. The current version is 3.3, which is what you should be using for new stuff. There are plenty of up-to-date examples (including for FTP) on the commons-net website (https://commons.apache.org/proper/commons-net/)
Upvotes: 3