1

I have this simple java code:

package com.androiddesktoptest.testtest;

public class AndroidDesktopTestMain
{
    public static void main(String[] args)
    {
        callFromAndroid();
    }

    public static void callFromAndroid()
    {
        System.out.println("DESKTOP CODE CALLED FROM ANDROID");
    }
}

And I want to call it from Android like this:

import com.androiddesktoptest.testtest.AndroidDesktopTestMain;

public void onCreate(Bundle savedInstanceState)
{
    //...
    AndroidDesktopTestMain.callFromAndroid();
}

and I get java.lang.NoClassDefFoundError

I am using Eclipse and my project AndroidDesktopTest has checked to export AndroidDesktopTest/src. Also, eclipse autocomplete my code in Android, so it see correcly my code in desktop project.

What I am doing wrong? I did something like this in my previous-previous-previous project, from this time, maybe ADT has chaged or something...

* UPDATE * entire error 07-20 14:02:56.452: E/AndroidRuntime(30233): java.lang.NoClassDefFoundError: com.androiddesktoptest.testtest.AndroidDesktopTestMain

SuitUp
  • 3,112
  • 5
  • 28
  • 41

4 Answers4

3

Order & Export isn't the most well built feature of the Android ADT.

If you instead include the linked src folder from the Desktop project to your Android project (Right Click Project > Build Path > Link Source Folder) then the Class will be used as if it belongs to the Android Project.

I think the prefered way of doing this would be to create a jar file or a Library Project but these might be too cumbersome to try until you have stable Deskptop code.

Hope that helps!

Graeme
  • 25,714
  • 24
  • 124
  • 186
2

Sounds like you don't get the class over to the phone when u run your android.

Make sure that:

right click project - properties - java build path - Order and Export (is it checked here?)

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
0

This article about including Java libraries into Android projects suggests replacing the "JRE System Library" with the android.jar for your API level in your linked Java project's build path dialog.

Jan Gerlinger
  • 7,361
  • 1
  • 44
  • 52
-1

try this

new AndroidDesktopTestMain.callFromAndroid();

The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.

MicroEyes
  • 3,680
  • 4
  • 26
  • 35