0

I am using the following code to Upload a file to an FTP server from my android device

TextView text;
FTPClient con;
BufferedInputStream buffIn;

public class FTPFileUpload extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        con = new FTPClient();
        if (params[0] != null & params[1] != null & params[2] != null & params[3] != null & params[4] != null & params[5] != null)
        {
        try {

            con.connect(InetAddress.getByName(params[0]));

                if (con.login(params[1], params[2])){ 
                con.changeWorkingDirectory(params[3]);
                con.setFileType(FTP.BINARY_FILE_TYPE);
                buffIn = new BufferedInputStream(new FileInputStream(params[4]));
                con.enterLocalPassiveMode();
                con.storeFile(params[5], buffIn);
                    text.setText("upload result: successful");
                }
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
        }

        try {
            con.logout();
            con.disconnect();
        } catch (IOException E) {
            Toast.makeText(getBaseContext(), E.getMessage(),Toast.LENGTH_SHORT).show();
        }
        }
        return null;
    }
}`

which is called inonCreate() method by new FTPClient(args); The libraries that i am using are as following

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

But the logcat shows me the error that it couldn't find a class

07-06 16:17:58.665: D/jdwp(18069): sendBufferedRequest : len=0x3B
07-06 16:17:58.674: W/asset(18069): AssetManager-->addDefaultAssets CIP path not exsit!
07-06 16:17:58.829: E/dalvikvm(18069): Could not find class 'org.apache.commons.net.ftp.FTPClient', referenced from method com.example.test.MainActivity$FTPFileUpload.doInBackground
07-06 16:17:58.829: W/dalvikvm(18069): VFY: unable to resolve new-instance 1120 (Lorg/apache/commons/net/ftp/FTPClient;) in Lcom/example/test/MainActivity$FTPFileUpload;
07-06 16:17:58.829: D/dalvikvm(18069): VFY: replacing opcode 0x22 at 0x0007
07-06 16:17:58.830: D/dalvikvm(18069): DexOpt: unable to opt direct call 0x21f8 at 0x09 in Lcom/example/test/MainActivity$FTPFileUpload;.doInBackground
07-06 16:17:58.840: V/PhoneWindow(18069): DecorView setVisiblity: visibility = 4
07-06 16:17:58.875: W/dalvikvm(18069): threadid=11: thread exiting with uncaught exception (group=0x41e739a8)
07-06 16:17:58.881: V/PhoneWindow(18069): DecorView setVisiblity: visibility = 0
07-06 16:17:58.936: E/AndroidRuntime(18069): FATAL EXCEPTION: AsyncTask #1
07-06 16:17:58.936: E/AndroidRuntime(18069): java.lang.RuntimeException: An error occured while executing doInBackground()
07-06 16:17:58.936: E/AndroidRuntime(18069):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at java.lang.Thread.run(Thread.java:838)
07-06 16:17:58.936: E/AndroidRuntime(18069): Caused by: java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient
07-06 16:17:58.936: E/AndroidRuntime(18069):    at com.example.test.MainActivity$FTPFileUpload.doInBackground(MainActivity.java:27)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at com.example.test.MainActivity$FTPFileUpload.doInBackground(MainActivity.java:1)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-06 16:17:58.936: E/AndroidRuntime(18069):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-06 16:17:58.936: E/AndroidRuntime(18069):    ... 4 more
07-06 16:17:58.993: D/libEGL(18069): loaded /system/lib/egl/libEGL_mali.so
07-06 16:17:59.010: D/libEGL(18069): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-06 16:17:59.013: D/libEGL(18069): loaded /system/lib/egl/libGLESv2_mali.so
07-06 16:17:59.129: D/OpenGLRenderer(18069): Enabling debug mode 0
07-06 16:17:59.133: V/InputMethodManager(18069): onWindowFocus: null softInputMode=288 first=true flags=#1810100
07-06 16:17:59.137: V/InputMethodManager(18069): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{423e67a8 V.E..... R.....ID 0,0-480,800} ic=null tba=android.view.inputmethod.EditorInfo@4240b780 controlFlags=#104
07-06 16:17:59.182: V/InputMethodManager(18069): Starting input: Bind result=InputBindResult{null com.android.inputmethod.latin/.LatinIME #27}
07-06 16:17:59.461: I/InputMethodManager(18069): handleMessage: MSG_SET_ACTIVE true, was false
07-06 16:17:59.971: D/OpenGLRenderer(18069): Flushing caches (mode 0)
07-06 16:18:00.020: D/OpenGLRenderer(18069): Flushing caches (mode 1)
07-06 16:18:00.038: D/OpenGLRenderer(18069): Flushing caches (mode 0)

I have fixed the NULLPOINTException and all but this just does not seem to be removed. Any suggestions to what i can do?

1 Answers1

0

NoClassDefFoundErrors can be a headache to figure out. Here's a handy link for them: link.

Basically, your code compiled properly, but a class file couldn't be loaded properly at runtime. It could be that the jar file is corrupted, was incorrectly signed, or that your gen folder got messed up on the last build. For me, closing out the IDE, reopening it, cleaning your project, trashing your gen folder and rebuilding, and dancing in a circle with your hands held high are all valid responses.

If none of that works and the library you're using never worked in the first place, I would look for a new copy of the Apache commons library; the file might have been corrupted.

Community
  • 1
  • 1
WalkingFood
  • 136
  • 5
  • i am using commons-net-3.3 & commons-net-1.4.1 but for both i get the same error. Can you please suggest some library? Much Thanks! – Owais Cheema Jul 06 '14 at 12:48
  • commons-net-3.3 should be all you need. Just to clarify, you're only using one at a time, right? You can try re-downloading the file, in case it was corrupted, or try a different mirror site. – WalkingFood Jul 06 '14 at 13:17
  • i have downloaded it, deleted it, re-downloaded it... Re-built the project but same error over and over again... any site you could recommend? i am downloading it from http://commons.apache.org/proper/commons-net/download_net.cgi?Preferred=http%3A%2F%2Fwww.eu.apache.org%2Fdist%2F – Owais Cheema Jul 06 '14 at 13:19
  • [commons.apache.org](http://commons.apache.org/proper/commons-net/download_net.cgi) would be where I would get it. – WalkingFood Jul 06 '14 at 13:21
  • Are you just rebuilding, or cleaning too? Have you tried deleting your gen folder? – WalkingFood Jul 06 '14 at 13:22
  • i am using the same...and yes, i have deleted and cleaned it properly. i even created a new project for the whole process. – Owais Cheema Jul 06 '14 at 13:23
  • `07-06 18:32:42.837: E/Trace(15763): error opening trace file: No such file or directory (2)` this also a part of logcat now – Owais Cheema Jul 06 '14 at 13:33
  • The trace error is just your IDE having trouble with the error reporting, it isn't a huge issue in itself. It could potentially be a class path error, but honestly, it sounds like your IDE is imploding on itself, which Eclipse does to me every few months or so. If rebooting your computer (the ultimate last-ditch effort) doesn't do anything, there isn't much more advice I can give, aside from ensuring that the jar isn't corrupted and maybe updating and even reinstalling the IDE. Sorry I couldn't be more help. – WalkingFood Jul 06 '14 at 13:50