Sun.Net FTP vs Apache Commons FTP

There are two main ways to do FTP in Java, either you can use the built-in Sun.Net FTP packages, or you can use the Apache Commons library to do FTP.

The second choice (Apache Commons) seems more popular, and more used then the Sun.Net package, lets take a look at why and the differences with the two packages

Reasons not to use Sun.Net packages

A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform. 1)

First off, why may you program not work? It may not work because if the user has a JRE/JDK that is not from Sun, then they will not have the Sun.Net packages in their JRE. Because they do not have the packages that will obviously cause you program to either work incorrectly, not start, or crash.

Because they are internal APIs: they are subject to change in a undocumented or unsupported way and they are bound to a specific JRE/JDK (Sun in your case), limiting portability of your programs.

Try to avoid uses of such APIs, always prefer a public documented and specified class. 2)

So you should definitely go with the Apache Commons library, as you can redistribute it with your application, so you will not run into issues having to do with the user’s JRE. Of course You can still use the Sun.Net packages, but it is highly unrecommended as you ideally don’t want to inconvenience the user by having to install a different Java distribution.

Also if you are in competition with another individual/company to have the better program, requiring a specific JRE could be the decision between the user buying/using your product or the other person’s product.




First of all, to authenticate to the FTP server using the Sun.Net packages you need to convert your password to a char array before sending it:

Apache Commons

To authenticate with Commons, you specify the username and password in strings:

Welcome Message


To get the welcome message using the Sun packages, you can really easily call FtpClient.getWelcomeMessage();

But this will only return the first line of the welcome message, not the whole message.

Apache Commons

To get the welcome message in Commons is a about the same, just instead of calling ftp,getWelcomeMessage(); you callftp.getReplyString();3)I tried to use getReply() initially, and it hung the thread, so defiantly use getReplyString() directly after connecting, this will return the whole welcome message, with line-breaks (\n).

Creating the FTP Client


To create the FTP client using Sun.Net, you need to pass it a InetSocketAdress to connect to like so:

Apache Commons

To connect to the FTP server in Commons, you can either pass it a InetAddress or a string and an integer to connect to:

String and Integer



In the end the Apache Commons Net is a lot better to use then Sun.Net for FTP. Not only will Apache Commons work across JRE distributions, but in some was it is also more efficient to use over Sun.Net FTP.

At the end of the day, if your making a consumer grade program you’ll want to go with Commons to provide as much of a hassle free experience as you can for your users. Having JRE specifications could be a turning point between a consumer buying your solution or a competitor’s solution.

Footnotes   [ + ]

3. I tried to use getReply() initially, and it hung the thread, so defiantly use getReplyString()