Skip to content

Quick start

Eliecer Hernandez edited this page Jul 17, 2019 · 6 revisions

In this short steps you will be able to start using the library in your Java project using Maven or Gradle.

First of all install MEGAcmd

Install MEGAcmd. Available packages for MEGAcmd in all supported platforms should be found here. You will know you have it installed when you get an answer from

MegaClient help

You can check also any help of any command using

MegaClient <MEGAcmd> --help

Setup the environment

The most common ways to setup you credentials are:

  • Using environment variables:
    • MEGA_EMAIL: Email used as username (lowercase)
    • MEGA_PWD: Corresponding password
  • Using an existing session, which can be rehused as long as it don't be closed.

Add the dependency of the library to your project

Using maven you just need to add the dependency

<dependency>
   <groupId>com.github.eliux</groupId>
   <artifactId>megacmd4j</artifactId>
   <version>${version}</version>
<dependency>

Or if you use Gradle add

compile group: 'com.github.eliux', name: 'megacmd4j', version: '${version}'

You can check the version in the Maven Central Repository.

Start writing code

Initiate a session

The entry point (facade class) of the library is com.github.eliux.mega.Mega.

If you want to start the session from scratch then use com.github.eliux.mega.Mega#login it will authenticate using multiple ways of credentials which derivates from the com.github.eliux.mega.auth.MegaAuth class which creates a com.github.eliux.mega.MegaSession which the login function, which refers to the MEGAcmd command with the same name. If you execute MegaClient login --help you will get as response:

Logs into a mega
You can log in either with email and password, with session ID,
or into a folder (an exported/public folder)
If loging into a folder indicate url#key

The most common way to authenticate as mentioned is using the environment variables and that can be done with

 MegaSession sessionMega = Mega.login(MegaAuthCredentials.createFromEnvVariables());

but if you rather using an existing session then use

 MegaSession sessionMega = Mega.currentSession();

But the most of the cases the developer wants to use an existing session if exists, otherwise he would create a new one using the environment variables. For that you use

 MegaSession sessionMega = Mega.init();

Execute commands with the initiated MegaSession

When you start a session with any of the options com.github.eliux.mega.Mega provides you will get always a com.github.eliux.mega.MegaSession instance. Use it to execute the same commands you find in MEGAcmd. Of course, not all will be available, but thanks to an IDE you will get it easy because the class inself contains all what you need to do, e.g.:

 //Starts the session or use an existing one
 MegaSession sessionMega = Mega.init();

 //Uploads a local file to a remote folder which might not exist
 sessionMega.uploadFile("target/yolo-infinite.txt", "megacmd4j/")
                .createRemoteIfNotPresent()
                .run();

 //List all files in the remote folder
 final List<FileInfo> files = sessionMega.ls("megacmd4j/").call();

 //Prints the name of any file it founds: there should only one
 files.stream().filter(FileInfo::isFile)
               .map(FileInfo::getName)
               .findAny()
               .ifPresent(System.out::println);

It should output the name of the file:

 yolo-infinite.txt

You will find more code like this in the tests of the application. E.g. BasicActionsTest.

You might have noticed all commands are not executed until you call a run or a call function at the end. This is because some of then can be configured with options or additional parameters as their MEGAcmd equivalent commands. They were designed to be flexible with threads, by implementing java.util.concurrent.Callable if the command returns something or java.lang.Runnable if it doesnt.

Finish the session

In case you want to finish the session at the end it would be easy to suppose you have to use a function of the com.github.eliux.mega.MegaSession instance:

  sessionMega.logout();  

Catch the exceptions

If any command does not work in a proper way it will throw a instance of com.github.eliux.mega.error.MegaException. You will find derived self-explained classes like MegaLoginException, which is thrown when an error ocurred during the login.


With this info you should be able to start playing with this library. Dont forget to report any issue or collaborate with any Pull Request to improve and extend the library, even for refactoring.