Getting error with sun.net.ftp.FtpClient by using JDK 1.7
Solution 1
You shouldn't use classes in sun.*
- there are no guarantee that they will remain compatible between java-versions.
I would suggest a rewrite to use something like Apache Commons FTP instead. It should be pretty simple to use, so it should be an easy job.
Solution 2
Even though there is truth in that classes in sun.*
should be avoided, the answer does not address the error appropriately, besides suggesting an alternative.
I don't have a scope on the context in which you are trying to instantiate sun.net.ftp.FtpClient
- but it is still possible to do so.
The class is abstract, so you can't instantiate it. (See §8.1.1.1)
All is not lost though.
To get an instance of FtpClient
, you can use the static methods from sun.net.ftp.FtpClientProvider
that is found in the SDK to do so, like in the example below:
...
final FtpClient ftpClient = FtpClientProvider.provider().createFtpClient();
final InetAddress inetAddress = InetAddress.getByName(ftpUrl.getHost());
final int port = ftpUrl.getPort();
final InetSocketAddress socketAddress = new InetSocketAddress(inetAddress, port);
ftpClient.connect(socketAddress);
ftpClient.login(username, password.toCharArray());
...
There is a bit more work involved, but it allows you to avoid adding a new library with heaps of classes that you don't need - it depends on what you want to do.
Solution 3
There are some changes since 1.7. Such as:
* before 1.7
* FtpClient fc=new FtpClient(url,port);
* fc.login(user, pwd);
* fc.binary();
* fc.put(remotefilename);
* fc.closeServer();
*
* since 1.7
* FtpClient fc = FtpClient.create(url)
* fc.login(user, null, pwd);
* fc.setBinaryType();
* fc.put(remotefilename,true);
* fc.close();
Comments
-
michdraft almost 2 years
I have developed a project that uses
sun.net.ftp.FtpClient
class to download a file from an ftp server whil i was using JDK 1.5. Now i have switched to JDK 1.7 and i get the following error.java: sun.net.ftp.FtpClient is abstract; cannot be instantiated
It seams to me JDK 1.7 does not support FtpClient .
Any suggestions to solve this issue are welcome?